-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: TsImportType - support import attributes #9796
Open
dsherret
wants to merge
2
commits into
swc-project:main
Choose a base branch
from
dsherret:feat_support_import_attributes_ts_import_type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
crates/swc_ecma_codegen/tests/fixture/typescript/ts_import_type/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Vite = typeof import("vite", { with: { "resolution-mode": "import" } }); |
5 changes: 5 additions & 0 deletions
5
crates/swc_ecma_codegen/tests/fixture/typescript/ts_import_type/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type Vite = typeof import("vite", { | ||
with: { | ||
"resolution-mode": "import" | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
crates/swc_ecma_codegen/tests/fixture/typescript/ts_import_type/output.min.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Vite=typeof import("vite",{with:{"resolution-mode":"import"}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,18 @@ impl<I: Tokens> Parser<I> { | |
} | ||
}; | ||
|
||
// the "assert" keyword is deprecated and this syntax is niche, so | ||
// don't support it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should support the "assert" keyword for these reasons. |
||
let with = | ||
if eat!(self, ',') && self.input.syntax().import_attributes() && is!(self, '{') { | ||
match *self.parse_object::<Box<Expr>>()? { | ||
Expr::Object(v) => Some(Box::new(v)), | ||
_ => unreachable!(), | ||
} | ||
} else { | ||
None | ||
}; | ||
|
||
expect!(self, ')'); | ||
|
||
let qualifier = if eat!(self, '.') { | ||
|
@@ -335,6 +347,7 @@ impl<I: Tokens> Parser<I> { | |
arg, | ||
qualifier, | ||
type_args, | ||
with, | ||
}) | ||
} | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
crates/swc_ecma_parser/tests/typescript/ts-import-type/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type Vite = typeof import("vite", { | ||
with: { | ||
"resolution-mode": "import" | ||
} | ||
}); | ||
type Vite2 = typeof import("vite",); | ||
type Vite3 = typeof import("vite"); |
197 changes: 197 additions & 0 deletions
197
crates/swc_ecma_parser/tests/typescript/ts-import-type/input.ts.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
{ | ||
"type": "Script", | ||
"span": { | ||
"start": 1, | ||
"end": 161 | ||
}, | ||
"body": [ | ||
{ | ||
"type": "TsTypeAliasDeclaration", | ||
"span": { | ||
"start": 1, | ||
"end": 88 | ||
}, | ||
"declare": false, | ||
"id": { | ||
"type": "Identifier", | ||
"span": { | ||
"start": 6, | ||
"end": 10 | ||
}, | ||
"ctxt": 0, | ||
"value": "Vite", | ||
"optional": false | ||
}, | ||
"typeParams": null, | ||
"typeAnnotation": { | ||
"type": "TsTypeQuery", | ||
"span": { | ||
"start": 13, | ||
"end": 87 | ||
}, | ||
"exprName": { | ||
"type": "TsImportType", | ||
"span": { | ||
"start": 20, | ||
"end": 87 | ||
}, | ||
"argument": { | ||
"type": "StringLiteral", | ||
"span": { | ||
"start": 27, | ||
"end": 33 | ||
}, | ||
"value": "vite", | ||
"raw": "\"vite\"" | ||
}, | ||
"qualifier": null, | ||
"typeArguments": null, | ||
"with": { | ||
"type": "ObjectExpression", | ||
"span": { | ||
"start": 35, | ||
"end": 86 | ||
}, | ||
"properties": [ | ||
{ | ||
"type": "KeyValueProperty", | ||
"key": { | ||
"type": "Identifier", | ||
"span": { | ||
"start": 39, | ||
"end": 43 | ||
}, | ||
"value": "with" | ||
}, | ||
"value": { | ||
"type": "ObjectExpression", | ||
"span": { | ||
"start": 45, | ||
"end": 84 | ||
}, | ||
"properties": [ | ||
{ | ||
"type": "KeyValueProperty", | ||
"key": { | ||
"type": "StringLiteral", | ||
"span": { | ||
"start": 53, | ||
"end": 70 | ||
}, | ||
"value": "resolution-mode", | ||
"raw": "\"resolution-mode\"" | ||
}, | ||
"value": { | ||
"type": "StringLiteral", | ||
"span": { | ||
"start": 72, | ||
"end": 80 | ||
}, | ||
"value": "import", | ||
"raw": "\"import\"" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"typeArguments": null | ||
} | ||
}, | ||
{ | ||
"type": "TsTypeAliasDeclaration", | ||
"span": { | ||
"start": 89, | ||
"end": 125 | ||
}, | ||
"declare": false, | ||
"id": { | ||
"type": "Identifier", | ||
"span": { | ||
"start": 94, | ||
"end": 99 | ||
}, | ||
"ctxt": 0, | ||
"value": "Vite2", | ||
"optional": false | ||
}, | ||
"typeParams": null, | ||
"typeAnnotation": { | ||
"type": "TsTypeQuery", | ||
"span": { | ||
"start": 102, | ||
"end": 124 | ||
}, | ||
"exprName": { | ||
"type": "TsImportType", | ||
"span": { | ||
"start": 109, | ||
"end": 124 | ||
}, | ||
"argument": { | ||
"type": "StringLiteral", | ||
"span": { | ||
"start": 116, | ||
"end": 122 | ||
}, | ||
"value": "vite", | ||
"raw": "\"vite\"" | ||
}, | ||
"qualifier": null, | ||
"typeArguments": null, | ||
"with": null | ||
}, | ||
"typeArguments": null | ||
} | ||
}, | ||
{ | ||
"type": "TsTypeAliasDeclaration", | ||
"span": { | ||
"start": 126, | ||
"end": 161 | ||
}, | ||
"declare": false, | ||
"id": { | ||
"type": "Identifier", | ||
"span": { | ||
"start": 131, | ||
"end": 136 | ||
}, | ||
"ctxt": 0, | ||
"value": "Vite3", | ||
"optional": false | ||
}, | ||
"typeParams": null, | ||
"typeAnnotation": { | ||
"type": "TsTypeQuery", | ||
"span": { | ||
"start": 139, | ||
"end": 160 | ||
}, | ||
"exprName": { | ||
"type": "TsImportType", | ||
"span": { | ||
"start": 146, | ||
"end": 160 | ||
}, | ||
"argument": { | ||
"type": "StringLiteral", | ||
"span": { | ||
"start": 153, | ||
"end": 159 | ||
}, | ||
"value": "vite", | ||
"raw": "\"vite\"" | ||
}, | ||
"qualifier": null, | ||
"typeArguments": null, | ||
"with": null | ||
}, | ||
"typeArguments": null | ||
} | ||
} | ||
], | ||
"interpreter": null | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this name because this is the full
{ with: { "module-resolution": "import" } }
(which I need for dprint-plugin-typescript). Maybe something else would be better here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll wait to update the failing tests until getting feedback on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
annotation
? I think we may need to use separate type for this kind of annotations. How do you think? cc @swc-project/core-esThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
with
hardcoded?I hope there is no situation like
{ with: { "foo": "bar" }, other: "value" }
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html
And typescript playground throws
Object literal may only specify known properties, and 'xxx' does not exist in type 'ImportCallOptions'.
So I think some names related to
ImportCallOptions
or justattributes
are ok as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a breaking change anyway if there's a situation like
{ with: { "foo": "bar" }, other: "value" }
, and I like the idea of