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

feat(typescript-estree): throw errors for object methods without function bodies #6589

Merged
merged 3 commits into from Mar 13, 2023
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
@@ -0,0 +1 @@
({get foo();})
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-getter-body TSESTree - Error 1`] = `
"TSError
> 1 | ({get foo();})
| ^ '{' expected.
2 |"
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-getter-body Babel - Error 1`] = `[SyntaxError: Unexpected token, expected "{" (1:11)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-getter-body Error Alignment 1`] = `"Both errored"`;
@@ -0,0 +1 @@
({method();})
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-method-body TSESTree - Error 1`] = `
"TSError
> 1 | ({method();})
| ^ '{' expected.
2 |"
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-method-body Babel - Error 1`] = `[SyntaxError: Unexpected token, expected "{" (1:10)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-method-body Error Alignment 1`] = `"Both errored"`;
@@ -0,0 +1 @@
({set foo(value);})
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-setter-body TSESTree - Error 1`] = `
"TSError
> 1 | ({set foo(value);})
| ^ '{' expected.
2 |"
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-setter-body Babel - Error 1`] = `[SyntaxError: Unexpected token, expected "{" (1:16)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures expression ObjectExpression _error_ missing-setter-body Error Alignment 1`] = `"Both errored"`;
Expand Up @@ -113,7 +113,7 @@ class Foo {
'const x = new Set<any>();',
'const x = { y: 1 };',
'const x = { y = 1 };',
noFormat`const x = { y(); };`,
noFormat`const x = { y(){} };`,
'const x: { y: number } = { y: 1 };',
'const x = [...[1, 2, 3]];',
'const [{ [`x${1}`]: x }] = [{ [`x`]: 1 }] as [{ [`x`]: any }];',
Expand Down
Expand Up @@ -290,7 +290,7 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
new (arg: readonly string[]): void;
}
`, // TSConstructSignatureDeclaration
noFormat`const x = { foo(arg: readonly string[]): void; };`, // TSEmptyBodyFunctionExpression
noFormat`class Foo { foo(arg: readonly string[]): void; };`, // TSEmptyBodyFunctionExpression
'function foo(arg: readonly string[]);', // TSDeclareFunction
'type Foo = (arg: readonly string[]) => void;', // TSFunctionType
`
Expand Down Expand Up @@ -667,7 +667,7 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
},
{
// TSEmptyBodyFunctionExpression
code: noFormat`const x = { foo(arg: string[]): void; };`,
code: noFormat`class Foo { foo(arg: string[]): void; };`,
errors: [
{
messageId: 'shouldBeReadonly',
Expand Down
39 changes: 31 additions & 8 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -1027,12 +1027,26 @@ export class Converter {
properties: node.properties.map(el => this.convertPattern(el)),
typeAnnotation: undefined,
});
} else {
return this.createNode<TSESTree.ObjectExpression>(node, {
type: AST_NODE_TYPES.ObjectExpression,
properties: node.properties.map(el => this.convertChild(el)),
});
}

const properties: TSESTree.Property[] = [];
for (const property of node.properties) {
if (
(property.kind === SyntaxKind.GetAccessor ||
property.kind === SyntaxKind.SetAccessor ||
property.kind === SyntaxKind.MethodDeclaration) &&
!property.body
) {
this.#throwUnlessAllowInvalidAST(property.end - 1, "'{' expected.");
}

properties.push(this.convertChild(property) as TSESTree.Property);
}

return this.createNode<TSESTree.ObjectExpression>(node, {
type: AST_NODE_TYPES.ObjectExpression,
properties,
});
}

case SyntaxKind.PropertyAssignment: {
Expand Down Expand Up @@ -3075,15 +3089,24 @@ export class Converter {
}

#throwUnlessAllowInvalidAST(
node: ts.Node,
node: ts.Node | number,
message: string,
): asserts node is never {
if (!this.options.allowInvalidAST) {
this.#throwError(node, message);
}
}

#throwError(node: ts.Node, message: string): asserts node is never {
throw createError(message, this.ast, node.getStart(), node.getEnd());
#throwError(node: ts.Node | number, message: string): asserts node is never {
let start;
let end;
if (typeof node === 'number') {
start = end = node;
} else {
start = node.getStart(this.ast);
end = node.getEnd();
}

throw createError(message, this.ast, start, end);
}
}