Skip to content

fix(typescript-estree): use TSEmptyBodyFunctionExpression for body-less nodes #1289

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

Merged
merged 4 commits into from
May 10, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ export default createRule<Options, MessageIds>({
'FunctionDeclaration, FunctionExpression'(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
) {
const closingParen = sourceCode.getTokenBefore(node.body!)!;
const closingParen = sourceCode.getTokenBefore(node.body)!;
const openingParen = sourceCode.getTokenBefore(
node.params.length ? node.params[0] : closingParen,
)!;
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/no-unsafe-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ export default util.createRule({
},
// object pattern props are checked via assignments
':not(ObjectPattern) > Property'(node: TSESTree.Property): void {
if (node.value.type === AST_NODE_TYPES.AssignmentPattern) {
if (
node.value.type === AST_NODE_TYPES.AssignmentPattern ||
node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression
) {
// handled by other selector
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function getReporLoc(
)!.loc.end;
}

return sourceCode.getTokenBefore(node.body!)!.loc.end;
return sourceCode.getTokenBefore(node.body)!.loc.end;
}

return {
Expand Down
16 changes: 0 additions & 16 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ParserServices,
TSESTreeOptions,
TSESTree,
simpleTraverse,
visitorKeys,
} from '@typescript-eslint/typescript-estree';
import { analyzeScope } from './analyze-scope';
Expand Down Expand Up @@ -95,21 +94,6 @@ export function parseForESLint(
const { ast, services } = parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;

simpleTraverse(ast, {
enter(node) {
switch (node.type) {
// Function#body cannot be null in ESTree spec.
case AST_NODE_TYPES.FunctionExpression:
if (!node.body) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
node.type = `TSEmptyBody${node.type}` as any;
}
break;
// no default
}
},
});

const scopeManager = analyzeScope(ast, options);
return { ast, services, scopeManager, visitorKeys };
}
Expand Down
20 changes: 14 additions & 6 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import {
isComputedProperty,
isESTreeClassMember,
isOptional,
unescapeStringLiteralText,
TSError,
unescapeStringLiteralText,
} from './node-utils';
import { ParserWeakMap, ParserWeakMapESTreeToTSNode } from './parser-options';
import {
AST_NODE_TYPES,
TSESTree,
TSNode,
TSESTreeToTSNode,
} from './ts-estree';
import { ParserWeakMap, ParserWeakMapESTreeToTSNode } from './parser-options';

const SyntaxKind = ts.SyntaxKind;

Expand Down Expand Up @@ -995,8 +995,12 @@ export class Converter {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodDeclaration: {
const method = this.createNode<TSESTree.FunctionExpression>(node, {
type: AST_NODE_TYPES.FunctionExpression,
const method = this.createNode<
TSESTree.TSEmptyBodyFunctionExpression | TSESTree.FunctionExpression
>(node, {
type: !node.body
? AST_NODE_TYPES.TSEmptyBodyFunctionExpression
: AST_NODE_TYPES.FunctionExpression,
id: null,
generator: !!node.asteriskToken,
expression: false, // ESTreeNode as ESTreeNode here
Expand Down Expand Up @@ -1105,8 +1109,12 @@ export class Converter {
(lastModifier && findNextToken(lastModifier, node, this.ast)) ||
node.getFirstToken()!;

const constructor = this.createNode<TSESTree.FunctionExpression>(node, {
type: AST_NODE_TYPES.FunctionExpression,
const constructor = this.createNode<
TSESTree.TSEmptyBodyFunctionExpression | TSESTree.FunctionExpression
>(node, {
type: !node.body
? AST_NODE_TYPES.TSEmptyBodyFunctionExpression
: AST_NODE_TYPES.FunctionExpression,
id: null,
params: this.convertParameters(node.parameters),
generator: false,
Expand Down
7 changes: 6 additions & 1 deletion packages/typescript-estree/src/ts-estree/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,11 @@ interface MethodDefinitionNonComputedNameBase extends MethodDefinitionBase {
interface PropertyBase extends BaseNode {
type: AST_NODE_TYPES.Property;
key: PropertyName;
value: Expression | AssignmentPattern | BindingName;
value:
| Expression
| AssignmentPattern
| BindingName
| TSEmptyBodyFunctionExpression;
computed: boolean;
method: boolean;
shorthand: boolean;
Expand Down Expand Up @@ -927,6 +931,7 @@ export interface FunctionDeclaration extends FunctionDeclarationBase {

export interface FunctionExpression extends FunctionDeclarationBase {
type: AST_NODE_TYPES.FunctionExpression;
body: BlockStatement;
}

export interface Identifier extends BaseNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ Object {
63,
66,
],
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down Expand Up @@ -1216,7 +1216,7 @@ Object {
},
},
},
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down Expand Up @@ -2433,7 +2433,7 @@ Object {
68,
71,
],
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down Expand Up @@ -4034,7 +4034,7 @@ Object {
},
},
},
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down Expand Up @@ -24208,7 +24208,7 @@ Object {
18,
21,
],
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
Object {
Expand Down Expand Up @@ -24304,7 +24304,7 @@ Object {
"type": "TSStringKeyword",
},
},
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
Object {
Expand Down Expand Up @@ -24401,7 +24401,7 @@ Object {
"type": "TSStringKeyword",
},
},
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down Expand Up @@ -38261,7 +38261,7 @@ Object {
"type": "TSAnyKeyword",
},
},
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
},
},
],
Expand Down