-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Fix #62179: Restrict import attribute values to string literals #62638
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
Open
sdotson
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
sdotson:bug/do-not-allow-js-expressions-in-attrs
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
5 commits
Select commit
Hold shift + click to select a range
ae81b7f
Fix #62179: Restrict import attribute values to string literals
sdotson 97d864d
rethink things to improve error messages/
sdotson 784aacb
actually do something now
sdotson f80cada
Validate import attribute values before type checking to avoid perfor…
sdotson 58da68f
remove unused file
sdotson 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 hidden or 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
124 changes: 60 additions & 64 deletions
124
tests/baselines/reference/importAssertionNonstring.errors.txt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,78 +1,74 @@ | ||
| mod.mts(1,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'number' is not assignable to type 'string'. | ||
| mod.mts(1,52): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(3,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(3,52): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(5,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(5,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'RegExp' is not assignable to type 'string'. | ||
| mod.mts(5,52): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(7,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(7,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'string[]' is not assignable to type 'string'. | ||
| mod.mts(7,52): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(9,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(9,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type '{ a: number; }' is not assignable to type 'string'. | ||
| mod.mts(9,52): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(11,37): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| mod.mts(11,66): error TS2837: Import assertion values must be string literal expressions. | ||
| mod.mts(5,53): error TS1005: ',' expected. | ||
| mod.mts(5,54): error TS1005: ':' expected. | ||
| mod.mts(5,55): error TS1005: ',' expected. | ||
| mod.mts(5,56): error TS1005: ':' expected. | ||
| mod.mts(5,57): error TS1005: ',' expected. | ||
| mod.mts(7,8): error TS1005: ':' expected. | ||
| mod.mts(7,10): error TS1005: ',' expected. | ||
| mod.mts(7,13): error TS1005: ':' expected. | ||
| mod.mts(7,20): error TS1005: ',' expected. | ||
| mod.mts(7,25): error TS1005: ':' expected. | ||
| mod.mts(7,37): error TS1005: ',' expected. | ||
| mod.mts(7,44): error TS1005: ':' expected. | ||
| mod.mts(7,45): error TS1005: ',' expected. | ||
| mod.mts(7,53): error TS1005: ',' expected. | ||
| mod.mts(7,56): error TS1005: ':' expected. | ||
| mod.mts(9,54): error TS1005: ',' expected. | ||
| mod.mts(9,60): error TS1128: Declaration or statement expected. | ||
| mod.mts(11,68): error TS1005: ',' expected. | ||
| mod.mts(11,77): error TS1005: ':' expected. | ||
| mod.mts(11,78): error TS1005: ',' expected. | ||
|
|
||
|
|
||
| ==== mod.mts (16 errors) ==== | ||
| ==== mod.mts (20 errors) ==== | ||
| import * as thing1 from "./mod.mjs" assert {field: 0}; | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
| ~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
|
|
||
| import * as thing2 from "./mod.mjs" assert {field: `a`}; | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
|
|
||
| import * as thing3 from "./mod.mjs" assert {field: /a/g}; | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'RegExp' is not assignable to type 'string'. | ||
| ~~~~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
|
|
||
| import * as thing4 from "./mod.mjs" assert {field: ["a"]}; | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~~~~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~~~~~~~~~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'string[]' is not assignable to type 'string'. | ||
| ~~~~~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
|
|
||
| import * as thing5 from "./mod.mjs" assert {field: { a: 0 }}; | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. | ||
| ~~~~~~~~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1128: Declaration or statement expected. | ||
|
|
||
| import * as thing6 from "./mod.mjs" assert {type: "json", field: 0..toString()} | ||
| ~~~~~~ | ||
| !!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'. | ||
| ~~~~~~~~~~~~~ | ||
| !!! error TS2837: Import assertion values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
This file contains hidden or 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 hidden or 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 hidden or 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
108 changes: 61 additions & 47 deletions
108
tests/baselines/reference/importAttributes6(module=node18).errors.txt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,56 +1,70 @@ | ||
| mod.mts(1,37): error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'number' is not assignable to type 'string'. | ||
| mod.mts(1,51): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(2,51): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(3,37): error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'RegExp' is not assignable to type 'string'. | ||
| mod.mts(3,51): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(4,37): error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type 'string[]' is not assignable to type 'string'. | ||
| mod.mts(4,51): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(5,37): error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. | ||
| Property 'field' is incompatible with index signature. | ||
| Type '{ a: number; }' is not assignable to type 'string'. | ||
| mod.mts(5,51): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(6,65): error TS2858: Import attribute values must be string literal expressions. | ||
| mod.mts(3,52): error TS1005: ',' expected. | ||
| mod.mts(3,53): error TS1005: ':' expected. | ||
| mod.mts(3,54): error TS1005: ',' expected. | ||
| mod.mts(3,56): error TS1005: ':' expected. | ||
| mod.mts(3,57): error TS1005: ',' expected. | ||
| mod.mts(4,8): error TS1005: ':' expected. | ||
| mod.mts(4,10): error TS1005: ',' expected. | ||
| mod.mts(4,13): error TS1005: ':' expected. | ||
| mod.mts(4,20): error TS1005: ',' expected. | ||
| mod.mts(4,25): error TS1005: ':' expected. | ||
| mod.mts(4,37): error TS1005: ',' expected. | ||
| mod.mts(4,42): error TS1005: ':' expected. | ||
| mod.mts(4,44): error TS1005: ',' expected. | ||
| mod.mts(4,52): error TS1005: ',' expected. | ||
| mod.mts(4,55): error TS1005: ':' expected. | ||
| mod.mts(5,53): error TS1005: ',' expected. | ||
| mod.mts(5,60): error TS1128: Declaration or statement expected. | ||
| mod.mts(6,67): error TS1005: ',' expected. | ||
| mod.mts(6,76): error TS1005: ':' expected. | ||
| mod.mts(6,77): error TS1005: ',' expected. | ||
|
|
||
|
|
||
| ==== mod.mts (10 errors) ==== | ||
| ==== mod.mts (20 errors) ==== | ||
| import * as thing1 from "./mod.mjs" with { field: 0 }; | ||
| ~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: 0; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
| ~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| import * as thing2 from "./mod.mjs" with { field: `a` }; | ||
| ~~~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| import * as thing3 from "./mod.mjs" with { field: /a/g }; | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: RegExp; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'RegExp' is not assignable to type 'string'. | ||
| ~~~~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| import * as thing4 from "./mod.mjs" with { field: ["a"] }; | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: string[]; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type 'string[]' is not assignable to type 'string'. | ||
| ~~~~~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~~~~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~~~~~~~~~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~~~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~~~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| import * as thing5 from "./mod.mjs" with { field: { a: 0 } }; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| !!! error TS2322: Type '{ field: { a: number; }; }' is not assignable to type 'ImportAttributes'. | ||
| !!! error TS2322: Property 'field' is incompatible with index signature. | ||
| !!! error TS2322: Type '{ a: number; }' is not assignable to type 'string'. | ||
| ~~~~~~~~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1128: Declaration or statement expected. | ||
| import * as thing6 from "./mod.mjs" with { type: "json", field: 0..toString() }; | ||
| ~~~~~~~~~~~~~ | ||
| !!! error TS2858: Import attribute values must be string literal expressions. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
| ~ | ||
| !!! error TS1005: ':' expected. | ||
| ~ | ||
| !!! error TS1005: ',' expected. | ||
|
|
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,4 +10,5 @@ import * as thing6 from "./mod.mjs" with { type: "json", field: 0..toString() }; | |
|
|
||
|
|
||
| //// [mod.mjs] | ||
| ; | ||
| export {}; | ||
This file contains hidden or 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
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.
The error messages in this test are much worse now
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 they should be better now. Thank you for the review.