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

fix: throw error on invalid ThrowStatement #6561

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
throw

// dummy comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument-2 TSESTree - Error 1`] = `
"TSError
> 1 | throw
| ^ Expression expected.
2 |
3 | // dummy comment
4 |"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument-2 Babel - Error 1`] = `[SyntaxError: Illegal newline after throw. (1:5)]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument-2 Error Alignment 1`] = `"Both errored"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function a() {throw}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument TSESTree - Error 1`] = `
"TSError
> 1 | function a() {throw}
| ^ Expression expected.
2 |"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument Babel - Error 1`] = `[SyntaxError: Unexpected token (1:19)]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument Error Alignment 1`] = `"Both errored"`;
13 changes: 11 additions & 2 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,20 @@ export class Converter {

// Exceptions

case SyntaxKind.ThrowStatement:
case SyntaxKind.ThrowStatement: {
const { expression } = node;
if (
expression.kind === SyntaxKind.Identifier &&
!(expression as ts.Identifier).text
) {
throw createError(this.ast, node.end, 'Expression expected.');
}

armano2 marked this conversation as resolved.
Show resolved Hide resolved
return this.createNode<TSESTree.ThrowStatement>(node, {
type: AST_NODE_TYPES.ThrowStatement,
argument: this.convertChild(node.expression),
argument: this.convertChild(expression),
});
}

case SyntaxKind.TryStatement:
return this.createNode<TSESTree.TryStatement>(node, {
Expand Down