Skip to content

Commit

Permalink
Fix parsing of import type <keyword> with flow
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 17, 2023
1 parent 15a4308 commit 352b18f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 8 deletions.
8 changes: 6 additions & 2 deletions packages/babel-parser/src/parser/statement.ts
@@ -1,6 +1,7 @@
import type * as N from "../types";
import {
tokenIsIdentifier,
tokenIsKeywordOrIdentifier,
tokenIsLoop,
tokenIsTemplate,
tt,
Expand Down Expand Up @@ -2940,7 +2941,7 @@ export default abstract class StatementParser extends ExpressionParser {
const phaseIdentifier = this.parseIdentifier(true);

const { type } = this.state;
const isImportPhase = tokenIsIdentifier(type)
const isImportPhase = tokenIsKeywordOrIdentifier(type)
? // OK: import <phase> x from "foo";
// OK: import <phase> from from "foo";
// NO: import <phase> from "foo";
Expand Down Expand Up @@ -3258,7 +3259,10 @@ export default abstract class StatementParser extends ExpressionParser {
this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"),
);
return true;
} else if (tokenIsIdentifier(this.state.type)) {
} else if (
// We allow keywords, and parseImportSpecifierLocal will report a recoverable error
tokenIsKeywordOrIdentifier(this.state.type)
) {
this.parseImportSpecifierLocal(
node,
this.startNode<N.ImportDefaultSpecifier>(),
Expand Down
@@ -1,4 +1,3 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected \"{\" (1:7)"
"sourceType": "module"
}
@@ -0,0 +1,40 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"errors": [
"SyntaxError: Unexpected keyword 'default'. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":14,"index":14}},
"local": {
"type": "Identifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":14,"index":14},"identifierName":"default"},
"name": "default"
}
}
],
"source": {
"type": "StringLiteral",
"start":20,"end":25,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":25,"index":25}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,40 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"errors": [
"SyntaxError: Unexpected keyword 'default'. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":25,"index":25}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":14,"index":14}},
"local": {
"type": "Identifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":14,"index":14},"identifierName":"default"},
"name": "default"
}
}
],
"source": {
"type": "StringLiteral",
"start":20,"end":25,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":25,"index":25}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
],
"directives": []
}
}
Expand Up @@ -4,5 +4,5 @@
"flow"
],
"sourceType": "module",
"throws": "Unexpected token, expected \"{\" (1:14)"
"throws": "Unexpected token, expected \"from\" (1:21)"
}
@@ -0,0 +1 @@
import type switch from 'foo';
@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"program": {
"type": "Program",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"importKind": "type",
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":12,"end":18,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":18,"index":18}},
"local": {
"type": "Identifier",
"start":12,"end":18,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":18,"index":18},"identifierName":"switch"},
"name": "switch"
}
}
],
"source": {
"type": "StringLiteral",
"start":24,"end":29,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":29,"index":29}},
"extra": {
"rawValue": "foo",
"raw": "'foo'"
},
"value": "foo"
}
}
],
"directives": []
}
}

0 comments on commit 352b18f

Please sign in to comment.