-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add auto-import for the package.json
imports
field
#55015
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
Merged
andrewbranch
merged 34 commits into
microsoft:main
from
emmatown:auto-import-package-json-imports
Dec 21, 2023
Merged
Changes from 22 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7c0fef4
Fix extension replacement in auto-import for `package.json#exports`
emmatown 60ec33b
Add auto-import for package.json `imports` field
emmatown faef5fd
Fix using incorrect directory and add more tests
emmatown 1d49909
Merge branch 'main' into auto-import-package-json-imports
emmatown 4e676a3
Use JSON.parse
emmatown 747e769
Merge branch 'main' into auto-import-package-json-imports
emmatown 01ceb65
Use readJson again so they can all be replaced at the same time
emmatown 864b9a3
Add test with conditions
emmatown d0407a9
Merge branch 'main' into auto-import-package-json-imports
emmatown e092943
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist 8f781c5
Revert "Use readJson again so they can all be replaced at the same time"
Andarist a2aa5ae
wrap `JSON.parse` calls with try/catch
Andarist eaa8e2e
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist 051239b
Add support for output file remapping
Andarist b3c5ca4
limit the behavior to imports
Andarist ea67766
add extra test case
Andarist 7abf4a2
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist 6a72a51
fixed conflict with the recent changes on main
Andarist 9903d72
Remove the `emitDeclarationOnly` check
Andarist 9e2df94
Move `getCommonSourceDirectory` to the `ModuleSpecifierResolutionHost`
Andarist c4df857
Reuse `getCommonSourceDirectory` available in the `Program`
Andarist 566e543
support declaration remapping
Andarist 1f9f214
reprioritize checks
Andarist 7a5c164
Deduplicate some emitter code
andrewbranch 45194b2
Fix lints
andrewbranch 8f2d0c3
Add failing test for when full path contains capital letters
andrewbranch 8bef690
Move tryParseJson to be usable in compiler
andrewbranch cd84162
import `tryParseJson` after it got moved
Andarist 8d68392
Fixed casing comparisons in `MatchingMode.Exact` and `MatchingMode.Di…
Andarist a9634ed
fixed the casing issue in the `MatchingMode.Pattern` case
Andarist 7f52af2
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist 4aab191
fixed `endsWith`
Andarist c435528
Merge branch 'main' into auto-import-package-json-imports
andrewbranch 4f29d3a
Fix bad merge artifacts
andrewbranch 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
357 changes: 357 additions & 0 deletions
357
tests/baselines/reference/tsserver/fourslashServer/autoImportProvider_importsMap1.js
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 |
---|---|---|
@@ -0,0 +1,357 @@ | ||
currentDirectory:: / useCaseSensitiveFileNames: false | ||
Info seq [hh:mm:ss:mss] Provided types map file "/typesMap.json" doesn't exist | ||
//// [/lib.d.ts] | ||
lib.d.ts-Text | ||
|
||
//// [/lib.decorators.d.ts] | ||
lib.decorators.d.ts-Text | ||
|
||
//// [/lib.decorators.legacy.d.ts] | ||
lib.decorators.legacy.d.ts-Text | ||
|
||
//// [/package.json] | ||
{ | ||
"type": "module", | ||
"imports": { | ||
"#is-browser": { | ||
"browser": "./dist/env/browser.js", | ||
"default": "./dist/env/node.js" | ||
} | ||
} | ||
} | ||
|
||
//// [/src/a.ts] | ||
isBrowser | ||
|
||
//// [/src/env/browser.ts] | ||
export const isBrowser = true; | ||
|
||
//// [/src/env/node.ts] | ||
export const isBrowser = false; | ||
|
||
//// [/tsconfig.json] | ||
{ | ||
"compilerOptions": { | ||
"module": "nodenext", | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
} | ||
} | ||
|
||
|
||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 0, | ||
"type": "request", | ||
"arguments": { | ||
"file": "/tsconfig.json" | ||
}, | ||
"command": "open" | ||
} | ||
Info seq [hh:mm:ss:mss] Search path: / | ||
Info seq [hh:mm:ss:mss] For info: /tsconfig.json :: Config file name: /tsconfig.json | ||
Info seq [hh:mm:ss:mss] Creating configuration project /tsconfig.json | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /tsconfig.json 2000 undefined Project: /tsconfig.json WatchType: Config file | ||
Info seq [hh:mm:ss:mss] event: | ||
{ | ||
"seq": 0, | ||
"type": "event", | ||
"event": "projectLoadingStart", | ||
"body": { | ||
"projectName": "/tsconfig.json", | ||
"reason": "Creating possible configured project for /tsconfig.json to open" | ||
} | ||
} | ||
Info seq [hh:mm:ss:mss] Config: /tsconfig.json : { | ||
"rootNames": [ | ||
"/lib.d.ts", | ||
"/lib.decorators.d.ts", | ||
"/lib.decorators.legacy.d.ts", | ||
"/src/a.ts", | ||
"/src/env/browser.ts", | ||
"/src/env/node.ts" | ||
], | ||
"options": { | ||
"module": 199, | ||
"rootDir": "/src", | ||
"outDir": "/dist", | ||
"configFilePath": "/tsconfig.json" | ||
} | ||
} | ||
Info seq [hh:mm:ss:mss] DirectoryWatcher:: Added:: WatchInfo: 1 undefined Config: /tsconfig.json WatchType: Wild card directory | ||
Info seq [hh:mm:ss:mss] Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: 1 undefined Config: /tsconfig.json WatchType: Wild card directory | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.d.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.decorators.d.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /lib.decorators.legacy.d.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /src/a.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /src/env/browser.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /src/env/node.ts 500 undefined WatchType: Closed Script info | ||
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /tsconfig.json | ||
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /tsconfig.json Version: 1 structureChanged: true structureIsReused:: Not Elapsed:: *ms | ||
Info seq [hh:mm:ss:mss] Project '/tsconfig.json' (Configured) | ||
Info seq [hh:mm:ss:mss] Files (6) | ||
/lib.decorators.d.ts Text-1 lib.decorators.d.ts-Text | ||
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text | ||
/lib.d.ts Text-1 lib.d.ts-Text | ||
/src/a.ts Text-1 "isBrowser" | ||
/src/env/browser.ts Text-1 "export const isBrowser = true;" | ||
/src/env/node.ts Text-1 "export const isBrowser = false;" | ||
|
||
|
||
lib.decorators.d.ts | ||
Library referenced via 'decorators' from file 'lib.d.ts' | ||
Matched by default include pattern '**/*' | ||
lib.decorators.legacy.d.ts | ||
Library referenced via 'decorators.legacy' from file 'lib.d.ts' | ||
Matched by default include pattern '**/*' | ||
lib.d.ts | ||
Matched by default include pattern '**/*' | ||
src/a.ts | ||
Matched by default include pattern '**/*' | ||
File is ECMAScript module because 'package.json' has field "type" with value "module" | ||
src/env/browser.ts | ||
Matched by default include pattern '**/*' | ||
File is ECMAScript module because 'package.json' has field "type" with value "module" | ||
src/env/node.ts | ||
Matched by default include pattern '**/*' | ||
File is ECMAScript module because 'package.json' has field "type" with value "module" | ||
|
||
Info seq [hh:mm:ss:mss] ----------------------------------------------- | ||
Info seq [hh:mm:ss:mss] event: | ||
{ | ||
"seq": 0, | ||
"type": "event", | ||
"event": "projectLoadingFinish", | ||
"body": { | ||
"projectName": "/tsconfig.json" | ||
} | ||
} | ||
Info seq [hh:mm:ss:mss] event: | ||
{ | ||
"seq": 0, | ||
"type": "event", | ||
"event": "configFileDiag", | ||
"body": { | ||
"triggerFile": "/tsconfig.json", | ||
"configFile": "/tsconfig.json", | ||
"diagnostics": [] | ||
} | ||
} | ||
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /dev/null/inferredProject1* | ||
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /dev/null/inferredProject1* Version: 1 structureChanged: true structureIsReused:: Not Elapsed:: *ms | ||
Info seq [hh:mm:ss:mss] Project '/dev/null/inferredProject1*' (Inferred) | ||
Info seq [hh:mm:ss:mss] Files (4) | ||
/lib.d.ts Text-1 lib.d.ts-Text | ||
/lib.decorators.d.ts Text-1 lib.decorators.d.ts-Text | ||
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text | ||
/tsconfig.json SVC-1-0 "{\n \"compilerOptions\": {\n \"module\": \"nodenext\",\n \"rootDir\": \"src\",\n \"outDir\": \"dist\"\n }\n}" | ||
|
||
|
||
lib.d.ts | ||
Default library for target 'es5' | ||
lib.decorators.d.ts | ||
Library referenced via 'decorators' from file 'lib.d.ts' | ||
lib.decorators.legacy.d.ts | ||
Library referenced via 'decorators.legacy' from file 'lib.d.ts' | ||
tsconfig.json | ||
Root file specified for compilation | ||
|
||
Info seq [hh:mm:ss:mss] ----------------------------------------------- | ||
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /package.json 250 undefined WatchType: package.json file | ||
Info seq [hh:mm:ss:mss] Project '/tsconfig.json' (Configured) | ||
Info seq [hh:mm:ss:mss] Files (6) | ||
|
||
Info seq [hh:mm:ss:mss] ----------------------------------------------- | ||
Info seq [hh:mm:ss:mss] Project '/dev/null/inferredProject1*' (Inferred) | ||
Info seq [hh:mm:ss:mss] Files (4) | ||
|
||
Info seq [hh:mm:ss:mss] ----------------------------------------------- | ||
Info seq [hh:mm:ss:mss] Open files: | ||
Info seq [hh:mm:ss:mss] FileName: /tsconfig.json ProjectRootPath: undefined | ||
Info seq [hh:mm:ss:mss] Projects: /dev/null/inferredProject1* | ||
After Request | ||
watchedFiles:: | ||
/lib.d.ts: *new* | ||
{"pollingInterval":500} | ||
/lib.decorators.d.ts: *new* | ||
{"pollingInterval":500} | ||
/lib.decorators.legacy.d.ts: *new* | ||
{"pollingInterval":500} | ||
/package.json: *new* | ||
{"pollingInterval":250} | ||
/src/a.ts: *new* | ||
{"pollingInterval":500} | ||
/src/env/browser.ts: *new* | ||
{"pollingInterval":500} | ||
/src/env/node.ts: *new* | ||
{"pollingInterval":500} | ||
/tsconfig.json: *new* | ||
{"pollingInterval":2000} | ||
|
||
watchedDirectoriesRecursive:: | ||
: *new* | ||
{} | ||
|
||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 1, | ||
"type": "request", | ||
"arguments": { | ||
"preferences": { | ||
"includeCompletionsForModuleExports": true, | ||
"includeCompletionsWithInsertText": true | ||
} | ||
}, | ||
"command": "configure" | ||
} | ||
Info seq [hh:mm:ss:mss] response: | ||
{ | ||
"seq": 0, | ||
"type": "response", | ||
"command": "configure", | ||
"request_seq": 1, | ||
"success": true | ||
} | ||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 2, | ||
"type": "request", | ||
"arguments": { | ||
"file": "/src/a.ts", | ||
"includeLinePosition": true | ||
}, | ||
"command": "syntacticDiagnosticsSync" | ||
} | ||
Info seq [hh:mm:ss:mss] response: | ||
{ | ||
"seq": 0, | ||
"type": "response", | ||
"command": "syntacticDiagnosticsSync", | ||
"request_seq": 2, | ||
"success": true, | ||
"body": [] | ||
} | ||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 3, | ||
"type": "request", | ||
"arguments": { | ||
"file": "/src/a.ts", | ||
"includeLinePosition": true | ||
}, | ||
"command": "semanticDiagnosticsSync" | ||
} | ||
Info seq [hh:mm:ss:mss] response: | ||
{ | ||
"seq": 0, | ||
"type": "response", | ||
"command": "semanticDiagnosticsSync", | ||
"request_seq": 3, | ||
"success": true, | ||
"body": [ | ||
{ | ||
"message": "Cannot find name 'isBrowser'.", | ||
"start": 0, | ||
"length": 9, | ||
"category": "error", | ||
"code": 2304, | ||
"startLocation": { | ||
"line": 1, | ||
"offset": 1 | ||
}, | ||
"endLocation": { | ||
"line": 1, | ||
"offset": 10 | ||
} | ||
} | ||
] | ||
} | ||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 4, | ||
"type": "request", | ||
"arguments": { | ||
"file": "/src/a.ts", | ||
"includeLinePosition": true | ||
}, | ||
"command": "suggestionDiagnosticsSync" | ||
} | ||
Info seq [hh:mm:ss:mss] response: | ||
{ | ||
"seq": 0, | ||
"type": "response", | ||
"command": "suggestionDiagnosticsSync", | ||
"request_seq": 4, | ||
"success": true, | ||
"body": [] | ||
} | ||
Info seq [hh:mm:ss:mss] request: | ||
{ | ||
"seq": 5, | ||
"type": "request", | ||
"arguments": { | ||
"file": "/src/a.ts", | ||
"startLine": 1, | ||
"startOffset": 1, | ||
"endLine": 1, | ||
"endOffset": 10, | ||
"errorCodes": [ | ||
2304 | ||
] | ||
}, | ||
"command": "getCodeFixes" | ||
} | ||
Info seq [hh:mm:ss:mss] response: | ||
{ | ||
"seq": 0, | ||
"type": "response", | ||
"command": "getCodeFixes", | ||
"request_seq": 5, | ||
"success": true, | ||
"body": [ | ||
{ | ||
"fixName": "import", | ||
"description": "Add import from \"./env/browser.js\"", | ||
"changes": [ | ||
{ | ||
"fileName": "/src/a.ts", | ||
"textChanges": [ | ||
{ | ||
"start": { | ||
"line": 1, | ||
"offset": 1 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"offset": 1 | ||
}, | ||
"newText": "import { isBrowser } from \"./env/browser.js\";\r\n\r\n" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"fixName": "import", | ||
"description": "Add import from \"#is-browser\"", | ||
"changes": [ | ||
{ | ||
"fileName": "/src/a.ts", | ||
"textChanges": [ | ||
{ | ||
"start": { | ||
"line": 1, | ||
"offset": 1 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"offset": 1 | ||
}, | ||
"newText": "import { isBrowser } from \"#is-browser\";\r\n\r\n" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.