Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ast-spec): correct some incorrect ast types #6257

Merged
merged 2 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/ast-spec/src/expression/ArrayExpression/spec.ts
Expand Up @@ -5,5 +5,8 @@ import type { Expression } from '../../unions/Expression';

export interface ArrayExpression extends BaseNode {
type: AST_NODE_TYPES.ArrayExpression;
elements: (Expression | SpreadElement)[];
/**
* an element will be `null` in the case of a sparse array: `[1, ,3]`
*/
elements: (Expression | SpreadElement | null)[];
}
3 changes: 1 addition & 2 deletions packages/ast-spec/src/expression/MemberExpression/spec.ts
Expand Up @@ -2,11 +2,10 @@ import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { PrivateIdentifier } from '../../special/PrivateIdentifier/spec';
import type { Expression } from '../../unions/Expression';
import type { LeftHandSideExpression } from '../../unions/LeftHandSideExpression';
import type { Identifier } from '../Identifier/spec';

interface MemberExpressionBase extends BaseNode {
object: LeftHandSideExpression;
object: Expression;
property: Expression | Identifier | PrivateIdentifier;
computed: boolean;
optional: boolean;
Expand Down
3 changes: 1 addition & 2 deletions packages/ast-spec/src/unions/ObjectLiteralElement.ts
@@ -1,8 +1,7 @@
import type { MethodDefinition } from '../element/MethodDefinition/spec';
import type { Property } from '../element/Property/spec';
import type { SpreadElement } from '../element/SpreadElement/spec';

export type ObjectLiteralElement = MethodDefinition | Property | SpreadElement;
export type ObjectLiteralElement = Property | SpreadElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I guess they're not the same in objects. Cool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah objects use Property with method: true

There's definitely scope to work on the Property type to better discriminate the union based on the flags.
I might raise a PR to do that soon as it's a pretty nice property to have hyper-specific types for this sort of case.

I actually did it when I wrote the flow-estree types
https://github.com/facebook/hermes/blob/main/tools/hermes-parser/js/hermes-estree/src/types.js#L448-L536
(looks like I didn't do method types, but that's a natural next step)


// TODO - breaking change remove this
export type ObjectLiteralElementLike = ObjectLiteralElement;
Expand Up @@ -197,8 +197,11 @@ export default createRule<Options, MessageIds>({
}
}

function checkExpression(node: TSESTree.Node, isErrorTest: boolean): void {
switch (node.type) {
function checkExpression(
node: TSESTree.Node | null,
isErrorTest: boolean,
): void {
switch (node?.type) {
case AST_NODE_TYPES.Literal:
checkLiteral(node, isErrorTest);
break;
Expand Down Expand Up @@ -478,7 +481,7 @@ export default createRule<Options, MessageIds>({

function checkValidTest(tests: TSESTree.ArrayExpression): void {
for (const test of tests.elements) {
switch (test.type) {
switch (test?.type) {
case AST_NODE_TYPES.ObjectExpression:
// delegate object-style tests to the invalid checker
checkInvalidTest(test, false);
Expand Down Expand Up @@ -546,7 +549,7 @@ export default createRule<Options, MessageIds>({

case 'invalid':
for (const element of prop.value.elements) {
if (element.type === AST_NODE_TYPES.ObjectExpression) {
if (element?.type === AST_NODE_TYPES.ObjectExpression) {
checkInvalidTest(element);
}
}
Expand Down Expand Up @@ -575,7 +578,7 @@ export default createRule<Options, MessageIds>({
}

for (const errorElement of testProp.value.elements) {
if (errorElement.type !== AST_NODE_TYPES.ObjectExpression) {
if (errorElement?.type !== AST_NODE_TYPES.ObjectExpression) {
continue;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -568,9 +568,7 @@ export default createRule<Options, MessageId>({
return false;
}

function isOptionableExpression(
node: TSESTree.LeftHandSideExpression,
): boolean {
function isOptionableExpression(node: TSESTree.Expression): boolean {
const type = getNodeType(node);
const isOwnNullable =
node.type === AST_NODE_TYPES.MemberExpression
Expand Down
Expand Up @@ -47,7 +47,7 @@ export default createRule({
* Check if a given node is a string.
* @param node The node to check.
*/
function isStringType(node: TSESTree.LeftHandSideExpression): boolean {
function isStringType(node: TSESTree.Expression): boolean {
const objectType = typeChecker.getTypeAtLocation(
service.esTreeNodeToTSNodeMap.get(node),
);
Expand Down
Expand Up @@ -50,7 +50,7 @@ export default util.createRule<Options, MessageIds>({
* Check if a given node is an array which all elements are string.
* @param node
*/
function isStringArrayNode(node: TSESTree.LeftHandSideExpression): boolean {
function isStringArrayNode(node: TSESTree.Expression): boolean {
const type = checker.getTypeAtLocation(
service.esTreeNodeToTSNodeMap.get(node),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/semanticInfo.test.ts
Expand Up @@ -365,7 +365,7 @@ function testIsolatedFile(
const declaration = (parseResult.ast.body[0] as TSESTree.VariableDeclaration)
.declarations[0];
const arrayMember = (declaration.init! as TSESTree.ArrayExpression)
.elements[0];
.elements[0]!;
expect(parseResult).toHaveProperty('services.esTreeNodeToTSNodeMap');

// get corresponding TS node
Expand Down