Skip to content

Commit

Permalink
fix(typescript-estree): use TSEmptyBodyFunctionExpression for body-…
Browse files Browse the repository at this point in the history
…less nodes (#1289)
  • Loading branch information
G-Rath authored and bradzacher committed May 21, 2020
1 parent ee8dd8f commit 82e7163
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
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
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
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
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
22 changes: 15 additions & 7 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -14,21 +14,21 @@ import {
getTextForTokenKind,
getTSNodeAccessibility,
hasModifier,
isChildOptionalChain,
isComma,
isComputedProperty,
isESTreeClassMember,
isOptional,
isChildOptionalChain,
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 @@ -999,8 +999,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 @@ -1109,8 +1113,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
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 @@ -928,6 +932,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
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

0 comments on commit 82e7163

Please sign in to comment.