Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(ast-spec): correct some incorrect ast types (#6257)
* fix(ast-spec): correct some ast types

* type error
  • Loading branch information
bradzacher committed Dec 21, 2022
1 parent ccd45d4 commit 0f3f645
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
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;

// 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

0 comments on commit 0f3f645

Please sign in to comment.