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

Add auto-import for the package.json imports field #55015

Merged
merged 34 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
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 Jul 14, 2023
60ec33b
Add auto-import for package.json `imports` field
emmatown May 27, 2023
faef5fd
Fix using incorrect directory and add more tests
emmatown Aug 16, 2023
1d49909
Merge branch 'main' into auto-import-package-json-imports
emmatown Aug 16, 2023
4e676a3
Use JSON.parse
emmatown Aug 16, 2023
747e769
Merge branch 'main' into auto-import-package-json-imports
emmatown Aug 16, 2023
01ceb65
Use readJson again so they can all be replaced at the same time
emmatown Aug 17, 2023
864b9a3
Add test with conditions
emmatown Sep 11, 2023
d0407a9
Merge branch 'main' into auto-import-package-json-imports
emmatown Sep 11, 2023
e092943
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist Oct 10, 2023
8f781c5
Revert "Use readJson again so they can all be replaced at the same time"
Andarist Oct 10, 2023
a2aa5ae
wrap `JSON.parse` calls with try/catch
Andarist Oct 10, 2023
eaa8e2e
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist Oct 30, 2023
051239b
Add support for output file remapping
Andarist Oct 30, 2023
b3c5ca4
limit the behavior to imports
Andarist Oct 30, 2023
ea67766
add extra test case
Andarist Oct 30, 2023
7abf4a2
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist Oct 30, 2023
6a72a51
fixed conflict with the recent changes on main
Andarist Oct 30, 2023
9903d72
Remove the `emitDeclarationOnly` check
Andarist Oct 30, 2023
9e2df94
Move `getCommonSourceDirectory` to the `ModuleSpecifierResolutionHost`
Andarist Oct 31, 2023
c4df857
Reuse `getCommonSourceDirectory` available in the `Program`
Andarist Oct 31, 2023
566e543
support declaration remapping
Andarist Oct 31, 2023
1f9f214
reprioritize checks
Andarist Oct 31, 2023
7a5c164
Deduplicate some emitter code
andrewbranch Nov 15, 2023
45194b2
Fix lints
andrewbranch Nov 15, 2023
8f2d0c3
Add failing test for when full path contains capital letters
andrewbranch Nov 15, 2023
8bef690
Move tryParseJson to be usable in compiler
andrewbranch Nov 15, 2023
cd84162
import `tryParseJson` after it got moved
Andarist Nov 15, 2023
8d68392
Fixed casing comparisons in `MatchingMode.Exact` and `MatchingMode.Di…
Andarist Nov 15, 2023
a9634ed
fixed the casing issue in the `MatchingMode.Pattern` case
Andarist Nov 16, 2023
7f52af2
Merge remote-tracking branch 'origin/main' into auto-import-package-j…
Andarist Nov 16, 2023
4aab191
fixed `endsWith`
Andarist Nov 16, 2023
c435528
Merge branch 'main' into auto-import-package-json-imports
andrewbranch Dec 21, 2023
4f29d3a
Fix bad merge artifacts
andrewbranch Dec 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50411,7 +50411,7 @@ export function signatureHasLiteralTypes(s: Signature) {
return !!(s.flags & SignatureFlags.HasLiteralTypes);
}

function createBasicNodeBuilderModuleSpecifierResolutionHost(host: TypeCheckerHost): ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string; } {
function createBasicNodeBuilderModuleSpecifierResolutionHost(host: TypeCheckerHost): ModuleSpecifierResolutionHost {
return {
getCommonSourceDirectory: !!(host as Program).getCommonSourceDirectory ? () => (host as Program).getCommonSourceDirectory() : () => "",
getCurrentDirectory: () => host.getCurrentDirectory(),
Expand Down
176 changes: 137 additions & 39 deletions src/compiler/moduleSpecifiers.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9671,6 +9671,7 @@ export interface ModuleSpecifierResolutionHost {
getProjectReferenceRedirect(fileName: string): string | undefined;
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
getCommonSourceDirectory(): string;
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,7 @@ export function createModuleSpecifierResolutionHost(program: Program, host: Lang
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
getNearestAncestorDirectoryWithPackageJson: maybeBind(host, host.getNearestAncestorDirectoryWithPackageJson),
getFileIncludeReasons: () => program.getFileIncludeReasons(),
getCommonSourceDirectory: () => program.getCommonSourceDirectory(),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
currentDirectory:: / useCaseSensitiveFileNames: false
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
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"
}
]
}
]
}
]
}
Loading
Loading