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

Throw errors for invalid TSEmptyBodyFunctionExpression node #12982

Merged
merged 6 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions changelog_unreleased/typescript/12982.md
@@ -0,0 +1,15 @@
#### Stop parsing invalid code (#12982 by @fisker)

<!-- prettier-ignore -->
```tsx
// Input
const object = ({ methodName() });

// Prettier stable
const object = { methodName(); };

// Prettier main
SyntaxError: Unexpected token. (1:29)
> 1 | const object = ({ methodName() });
| ^^
```
19 changes: 18 additions & 1 deletion src/language-js/parse/postprocess/index.js
Expand Up @@ -5,7 +5,10 @@ const isTsKeywordType = require("../../utils/is-ts-keyword-type.js");
const isTypeCastComment = require("../../utils/is-type-cast-comment.js");
const getLast = require("../../../utils/get-last.js");
const visitNode = require("./visit-node.js");
const { throwErrorForInvalidNodes } = require("./typescript.js");
const {
throwErrorForInvalidNodes,
throwSyntaxError,
} = require("./typescript.js");

function postprocess(ast, options) {
if (
Expand Down Expand Up @@ -102,6 +105,20 @@ function postprocess(ast, options) {
};
}
break;
case "ObjectExpression":
// #12963
if (options.parser === "typescript") {
const invalidProperty = node.properties.find(
(property) =>
property.type === "Property" &&
property.value.type === "TSEmptyBodyFunctionExpression"
);
if (invalidProperty) {
throwSyntaxError(invalidProperty.value, "Unexpected token.");
}
}
break;

case "SequenceExpression": {
// Babel (unlike other parsers) includes spaces and comments in the range. Let's unify this.
const lastExpression = getLast(node.expressions);
Expand Down
3 changes: 2 additions & 1 deletion src/language-js/parse/postprocess/typescript.js
Expand Up @@ -73,9 +73,10 @@ function throwErrorForInvalidNodes(ast, options) {
if (esTreeNode !== node) {
return;
}

throwErrorForInvalidDecorator(tsNode, esTreeNode, tsNodeToESTreeNodeMap);
throwErrorForInvalidAbstractProperty(tsNode, esTreeNode);
});
}

module.exports = { throwErrorForInvalidNodes };
module.exports = { throwErrorForInvalidNodes, throwSyntaxError };
Copy link
Member

Choose a reason for hiding this comment

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

Can we move throwSyntaxError to other file like a parse/postprocess/throw-syntax-error.js?

Copy link
Member

Choose a reason for hiding this comment

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

Can we move throwSyntaxError to other file like a parse/postprocess/throw-syntax-error.js?

144 changes: 144 additions & 0 deletions tests/format/misc/errors/invalid/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -156,92 +156,236 @@ exports[`snippet: #1 [typescript] format 1`] = `
| ^"
`;

exports[`snippet: #2 [acorn] format 1`] = `
"Unexpected token (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [babel] format 1`] = `
"Unexpected token, expected "{" (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [babel] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a = 1) = 2
| ^"
`;

exports[`snippet: #2 [babel-flow] format 1`] = `
"Unexpected token, expected "{" (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [babel-flow] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a = 1) = 2
| ^"
`;

exports[`snippet: #2 [babel-ts] format 1`] = `
"Unexpected token, expected "{" (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [babel-ts] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a = 1) = 2
| ^"
`;

exports[`snippet: #2 [espree] format 1`] = `
"Unexpected token } (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [flow] format 1`] = `
"Unexpected token \`}\`, expected the token \`{\` (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [flow] format 2`] = `
"Invalid left-hand side in assignment (1:2)
> 1 | (a = 1) = 2
| ^^^^^"
`;

exports[`snippet: #2 [meriyah] format 1`] = `
"Expected '{' (1:13)
> 1 | ({ method() })
| ^"
`;

exports[`snippet: #2 [meriyah] format 2`] = `
"Invalid left-hand side in assignment (1:9)
> 1 | (a = 1) = 2
| ^"
`;

exports[`snippet: #2 [typescript] format 1`] = `
"Unexpected token. (1:10)
> 1 | ({ method() })
| ^^"
`;

exports[`snippet: #3 [acorn] format 1`] = `
"Unexpected token (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [babel] format 1`] = `
"Unexpected token, expected "{" (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [babel] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a += b) = 1
| ^"
`;

exports[`snippet: #3 [babel-flow] format 1`] = `
"Unexpected token, expected "{" (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [babel-flow] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a += b) = 1
| ^"
`;

exports[`snippet: #3 [babel-ts] format 1`] = `
"Unexpected token, expected "{" (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [babel-ts] format 2`] = `
"Invalid parenthesized assignment pattern. (1:1)
> 1 | (a += b) = 1
| ^"
`;

exports[`snippet: #3 [espree] format 1`] = `
"Unexpected token } (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [flow] format 1`] = `
"Unexpected token \`}\`, expected the token \`{\` (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [flow] format 2`] = `
"Invalid left-hand side in assignment (1:2)
> 1 | (a += b) = 1
| ^^^^^^"
`;

exports[`snippet: #3 [meriyah] format 1`] = `
"Expected '{' (1:15)
> 1 | ({ method({}) })
| ^"
`;

exports[`snippet: #3 [meriyah] format 2`] = `
"Invalid left-hand side in assignment (1:10)
> 1 | (a += b) = 1
| ^"
`;

exports[`snippet: #3 [typescript] format 1`] = `
"Unexpected token. (1:10)
> 1 | ({ method({}) })
| ^^^^"
`;

exports[`snippet: #4 [acorn] format 1`] = `
"Unexpected token (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [babel] format 1`] = `
"Unexpected token, expected "{" (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [babel] format 2`] = `
"Invalid left-hand side in parenthesized expression. (1:2)
> 1 | (a = b) += 1
| ^"
`;

exports[`snippet: #4 [babel-flow] format 1`] = `
"Unexpected token, expected "{" (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [babel-flow] format 2`] = `
"Invalid left-hand side in parenthesized expression. (1:2)
> 1 | (a = b) += 1
| ^"
`;

exports[`snippet: #4 [babel-ts] format 1`] = `
"Unexpected token, expected "{" (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [babel-ts] format 2`] = `
"Invalid left-hand side in parenthesized expression. (1:2)
> 1 | (a = b) += 1
| ^"
`;

exports[`snippet: #4 [espree] format 1`] = `
"Unexpected token } (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [flow] format 1`] = `
"Unexpected token \`}\`, expected the token \`{\` (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [flow] format 2`] = `
"Invalid left-hand side in assignment (1:2)
> 1 | (a = b) += 1
| ^^^^^"
`;

exports[`snippet: #4 [meriyah] format 1`] = `
"Expected '{' (1:23)
> 1 | ({ method(parameter,) })
| ^"
`;

exports[`snippet: #4 [meriyah] format 2`] = `
"Invalid left-hand side in assignment (1:10)
> 1 | (a = b) += 1
| ^"
`;

exports[`snippet: #4 [typescript] format 1`] = `
"Unexpected token. (1:10)
> 1 | ({ method(parameter,) })
| ^^^^^^^^^^^^"
`;
8 changes: 7 additions & 1 deletion tests/format/misc/errors/invalid/jsfmt.spec.js
@@ -1,7 +1,13 @@
run_spec(
{
dirname: __dirname,
snippets: ["for each (a in b) {}", "class switch() {}"],
snippets: [
"for each (a in b) {}",
"class switch() {}",
"({ method() })",
"({ method({}) })",
"({ method(parameter,) })",
],
},
[
"babel",
Expand Down