diff --git a/packages/babel-parser/src/tokenizer/types.ts b/packages/babel-parser/src/tokenizer/types.ts index 1bf21dd323da..3b06b0c90be5 100644 --- a/packages/babel-parser/src/tokenizer/types.ts +++ b/packages/babel-parser/src/tokenizer/types.ts @@ -328,6 +328,9 @@ export const tt = { _opaque: createKeywordLike("opaque", { startsExpr }), // end: isFlowInterfaceOrTypeOrOpaque name: createToken("name", { startsExpr }), + + // placeholder plugin + placeholder: createToken("%%", { startsExpr: true }), // end: isIdentifier string: createToken("string", { startsExpr }), @@ -345,13 +348,10 @@ export const tt = { jsxText: createToken("jsxText", { beforeExpr: true }), jsxTagStart: createToken("jsxTagStart", { startsExpr: true }), jsxTagEnd: createToken("jsxTagEnd"), - - // placeholder plugin - placeholder: createToken("%%", { startsExpr: true }), } as const; export function tokenIsIdentifier(token: TokenType): boolean { - return token >= tt._as && token <= tt.name; + return token >= tt._as && token <= tt.placeholder; } export function tokenKeywordOrIdentifierIsKeyword(token: TokenType): boolean { @@ -361,7 +361,7 @@ export function tokenKeywordOrIdentifierIsKeyword(token: TokenType): boolean { } export function tokenIsKeywordOrIdentifier(token: TokenType): boolean { - return token >= tt._in && token <= tt.name; + return token >= tt._in && token <= tt.placeholder; } export function tokenIsLiteralPropertyName(token: TokenType): boolean { diff --git a/packages/babel-parser/test/fixtures/placeholders/expression/object/input.js b/packages/babel-parser/test/fixtures/placeholders/expression/object/input.js new file mode 100644 index 000000000000..f8a717f8c5d6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/placeholders/expression/object/input.js @@ -0,0 +1,4 @@ +const a = { + %%key%%: 1, + [%%key2%%]: 2, +} diff --git a/packages/babel-parser/test/fixtures/placeholders/expression/object/output.json b/packages/babel-parser/test/fixtures/placeholders/expression/object/output.json new file mode 100644 index 000000000000..0aa3dbc86b22 --- /dev/null +++ b/packages/babel-parser/test/fixtures/placeholders/expression/object/output.json @@ -0,0 +1,90 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":44}}, + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":44}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":44}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":44,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":4,"column":1,"index":44}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"a"}, + "name": "a" + }, + "init": { + "type": "ObjectExpression", + "start":10,"end":44,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":4,"column":1,"index":44}}, + "properties": [ + { + "type": "ObjectProperty", + "start":14,"end":24,"loc":{"start":{"line":2,"column":2,"index":14},"end":{"line":2,"column":12,"index":24}}, + "method": false, + "key": { + "type": "Placeholder", + "start":14,"end":21,"loc":{"start":{"line":2,"column":2,"index":14},"end":{"line":2,"column":9,"index":21}}, + "name": { + "type": "Identifier", + "start":16,"end":19,"loc":{"start":{"line":2,"column":4,"index":16},"end":{"line":2,"column":7,"index":19},"identifierName":"key"}, + "name": "key" + }, + "expectedNode": "Identifier" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start":23,"end":24,"loc":{"start":{"line":2,"column":11,"index":23},"end":{"line":2,"column":12,"index":24}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ObjectProperty", + "start":28,"end":41,"loc":{"start":{"line":3,"column":2,"index":28},"end":{"line":3,"column":15,"index":41}}, + "method": false, + "computed": true, + "key": { + "type": "Placeholder", + "start":29,"end":37,"loc":{"start":{"line":3,"column":3,"index":29},"end":{"line":3,"column":11,"index":37}}, + "name": { + "type": "Identifier", + "start":31,"end":35,"loc":{"start":{"line":3,"column":5,"index":31},"end":{"line":3,"column":9,"index":35},"identifierName":"key2"}, + "name": "key2" + }, + "expectedNode": "Expression" + }, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start":40,"end":41,"loc":{"start":{"line":3,"column":14,"index":40},"end":{"line":3,"column":15,"index":41}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + ], + "extra": { + "trailingComma": 41 + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/input.ts b/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/input.ts new file mode 100644 index 000000000000..bdc1733e2584 --- /dev/null +++ b/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/input.ts @@ -0,0 +1 @@ +export namespace %%foo%% { } diff --git a/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/output.json b/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/output.json new file mode 100644 index 000000000000..1450bca97477 --- /dev/null +++ b/packages/babel-parser/test/fixtures/placeholders/typescript/export-namespace/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}}, + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start":7,"end":28,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":28,"index":28}}, + "kind": "namespace", + "id": { + "type": "Placeholder", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":24,"index":24}}, + "name": { + "type": "Identifier", + "start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22},"identifierName":"foo"}, + "name": "foo" + }, + "expectedNode": "Identifier" + }, + "body": { + "type": "TSModuleBlock", + "start":25,"end":28,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":28,"index":28}}, + "body": [] + } + } + } + ], + "directives": [], + "extra": { + "topLevelAwait": false + } + } +}