Skip to content

Commit

Permalink
Allow import attributes for TSImportType (#16277)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Feb 27, 2024
1 parent 8c3f035 commit e44d8d7
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -566,6 +566,19 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>

// For compatibility to estree we cannot call parseLiteral directly here
node.argument = super.parseExprAtom() as N.StringLiteral;
if (
this.hasPlugin("importAttributes") ||
this.hasPlugin("importAssertions")
) {
node.options = null;
}
if (this.eat(tt.comma)) {
this.expectImportAttributesPlugin();
if (!this.match(tt.parenR)) {
node.options = super.parseMaybeAssignAllowIn();
this.eat(tt.comma);
}
}
this.expect(tt.parenR);

if (this.eat(tt.dot)) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/types.d.ts
Expand Up @@ -1543,6 +1543,7 @@ export interface TsImportType extends TsTypeBase {
argument: StringLiteral;
qualifier?: TsEntityName;
typeParameters?: TsTypeParameterInstantiation;
options?: Expression | null;
}

// ================
Expand Down
@@ -0,0 +1 @@
let x: typeof import("foo.json", <ImportOptionBagJSX type="json"/>)
@@ -0,0 +1,8 @@
{
"plugins": [
"typescript",
"jsx",
"importAttributes"
],
"throws": "Unexpected token, expected \"jsxTagEnd\" (1:65)"
}
@@ -0,0 +1 @@
let x: typeof import('./x', { with: { type: "json" }});
@@ -0,0 +1,6 @@
{
"plugins": [
"typescript",
"importAttributes"
]
}
@@ -0,0 +1,96 @@
{
"type": "File",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":55,"index":55}},
"program": {
"type": "Program",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":55,"index":55}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":55,"index":55}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":54,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":54,"index":54}},
"id": {
"type": "Identifier",
"start":4,"end":54,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":54,"index":54},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":5,"end":54,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":54,"index":54}},
"typeAnnotation": {
"type": "TSTypeQuery",
"start":7,"end":54,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":54,"index":54}},
"exprName": {
"type": "TSImportType",
"start":14,"end":54,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":54,"index":54}},
"argument": {
"type": "StringLiteral",
"start":21,"end":26,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":26,"index":26}},
"extra": {
"rawValue": "./x",
"raw": "'./x'"
},
"value": "./x"
},
"options": {
"type": "ObjectExpression",
"start":28,"end":53,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":53,"index":53}},
"properties": [
{
"type": "ObjectProperty",
"start":30,"end":52,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":52,"index":52}},
"method": false,
"key": {
"type": "Identifier",
"start":30,"end":34,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":34,"index":34},"identifierName":"with"},
"name": "with"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":36,"end":52,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":1,"column":52,"index":52}},
"properties": [
{
"type": "ObjectProperty",
"start":38,"end":50,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":50,"index":50}},
"method": false,
"key": {
"type": "Identifier",
"start":38,"end":42,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":42,"index":42},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":44,"end":50,"loc":{"start":{"line":1,"column":44,"index":44},"end":{"line":1,"column":50,"index":50}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
}
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}

0 comments on commit e44d8d7

Please sign in to comment.