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

Allow import attributes for TSImportType #16277

Merged
Merged
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
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();
Copy link
Member Author

Choose a reason for hiding this comment

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

I implemented the same logic as parseImportCall (

parseImportCall(
this: Parser,
node: Undone<N.ImportExpression>,
): N.ImportExpression {
this.next(); // eat tt.parenL
node.source = this.parseMaybeAssignAllowIn();
if (
this.hasPlugin("importAttributes") ||
this.hasPlugin("importAssertions")
) {
node.options = null;
}
if (this.eat(tt.comma)) {
this.expectImportAttributesPlugin();
if (!this.match(tt.parenR)) {
node.options = this.parseMaybeAssignAllowIn();
this.eat(tt.comma);
}
}
this.expect(tt.parenR);
return this.finishNode(node, "ImportExpression");
}
). But I'm not sure what we should actually allow here.

Copy link
Contributor

Choose a reason for hiding this comment

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

That looks good to me. We can add a new test case for integration with the JSX plugin, e.g.

typeof import("foo.json", <ImportOptionBagJSX type="json"/>)

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added tests but it is invalid ( 950a377 )

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, import("foo.json", <ImportOptionBagJSX type="json"/>) works when we don't have typescript plugin. It is probably a parser bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we fix it in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessary. The input is a purely imaginative case so I think we can defer the fix to the future.

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": []
}
}