From 1fb2b2d70fcd97c14a4fa1c74c28cf53497fc205 Mon Sep 17 00:00:00 2001 From: Huy Date: Wed, 25 May 2022 12:07:38 -0700 Subject: [PATCH 1/3] fix(47562): Add option to suppress type hint if variable name matches type name (#48529) * fix(47562): Add option to suppress type hint if variable name matches type * Remove the unnecessary debug code * Re-run gulp runtests * Use equateStringsCaseInsensitive to compare strings --- src/compiler/types.ts | 1 + src/server/protocol.ts | 9 +++---- src/services/inlayHints.ts | 4 ++++ .../reference/api/tsserverlibrary.d.ts | 2 ++ tests/baselines/reference/api/typescript.d.ts | 1 + tests/cases/fourslash/fourslash.ts | 1 + .../cases/fourslash/inlayHintsShouldWork67.ts | 24 +++++++++++++++++++ 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/inlayHintsShouldWork67.ts diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 714112664a1..41162d9b3e4 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -8813,6 +8813,7 @@ namespace ts { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean, readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; diff --git a/src/server/protocol.ts b/src/server/protocol.ts index 121b69895f9..623699de435 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -728,7 +728,7 @@ namespace ts.server.protocol { } // All we need is the `success` and `message` fields of Response. - export interface ApplyCodeActionCommandResponse extends Response {} + export interface ApplyCodeActionCommandResponse extends Response { } export interface FileRangeRequestArgs extends FileRequestArgs { /** @@ -1067,7 +1067,7 @@ namespace ts.server.protocol { readonly arguments: JsxClosingTagRequestArgs; } - export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs {} + export interface JsxClosingTagRequestArgs extends FileLocationRequestArgs { } export interface JsxClosingTagResponse extends Response { readonly body: TextInsertion; @@ -2390,7 +2390,7 @@ namespace ts.server.protocol { /** * Human-readable description of the `source` from the CompletionEntry. */ - sourceDisplay?: SymbolDisplayPart[]; + sourceDisplay?: SymbolDisplayPart[]; } /** @deprecated Prefer CompletionInfoResponse, which supports several top-level fields in addition to the array of entries. */ @@ -3415,7 +3415,7 @@ namespace ts.server.protocol { /** * Allows completions to be formatted with snippet text, indicated by `CompletionItem["isSnippet"]`. */ - readonly includeCompletionsWithSnippetText?: boolean; + readonly includeCompletionsWithSnippetText?: boolean; /** * If enabled, the completion list will include completions with invalid identifier names. * For those entries, The `insertText` and `replacementSpan` properties will be set to change from `.x` property access to `["x"]`. @@ -3465,6 +3465,7 @@ namespace ts.server.protocol { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean, readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index 110e9718405..ca95df202b8 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -137,6 +137,10 @@ namespace ts.InlayHints { const typeDisplayString = printTypeInSingleLine(declarationType); if (typeDisplayString) { + const isVariableNameMatchesType = preferences.includeInlayVariableTypeHintsWhenTypeMatchesName === false && equateStringsCaseInsensitive(decl.name.getText(), typeDisplayString); + if (isVariableNameMatchesType) { + return; + } addTypeHints(typeDisplayString, decl.name.end); } } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 97b3a541820..ab7ae0cde49 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -4120,6 +4120,7 @@ declare namespace ts { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean; readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; @@ -9711,6 +9712,7 @@ declare namespace ts.server.protocol { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean; readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 2b3ffba10cb..658786b63f1 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4120,6 +4120,7 @@ declare namespace ts { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean; readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 79e0f52cb58..ea690be4014 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -666,6 +666,7 @@ declare namespace FourSlashInterface { readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean; readonly includeInlayFunctionParameterTypeHints?: boolean; readonly includeInlayVariableTypeHints?: boolean; + readonly includeInlayVariableTypeHintsWhenTypeMatchesName?: boolean; readonly includeInlayPropertyDeclarationTypeHints?: boolean; readonly includeInlayFunctionLikeReturnTypeHints?: boolean; readonly includeInlayEnumMemberValueHints?: boolean; diff --git a/tests/cases/fourslash/inlayHintsShouldWork67.ts b/tests/cases/fourslash/inlayHintsShouldWork67.ts new file mode 100644 index 00000000000..2b35b152b79 --- /dev/null +++ b/tests/cases/fourslash/inlayHintsShouldWork67.ts @@ -0,0 +1,24 @@ +/// + +//// type Client = {}; +//// function getClient(): Client { return {}; }; +//// const client/**/ = getClient(); + +const markers = test.markers(); + +verify.getInlayHints([ + { + text: ': Client', + position: markers[0].position, + kind: ts.InlayHintKind.Type, + whitespaceBefore: true + } +], undefined, { + includeInlayVariableTypeHints: true, + includeInlayVariableTypeHintsWhenTypeMatchesName: true +}); + +verify.getInlayHints([], undefined, { + includeInlayVariableTypeHints: true, + includeInlayVariableTypeHintsWhenTypeMatchesName: false +}); From 0ad5f82d6243db80d42bc0abb7a191dd380e980e Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 25 May 2022 12:30:40 -0700 Subject: [PATCH 2/3] Fix the implicit glob key so that recursive keys are not differing just directory separator for wildcard directory (#49246) * Add failing test * Fix the implicit glob key so that recursive keys are not differing just by directory seperator Fixes #49078 * Reset the reload level once program is loaded --- src/compiler/commandLineParser.ts | 2 +- src/compiler/watchPublic.ts | 1 + .../unittests/config/tsconfigParsing.ts | 9 + .../unittests/tscWatch/programUpdates.ts | 33 ++ ...keys-differ-only-in-directory-seperator.js | 388 ++++++++++++++++++ 5 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/tscWatch/programUpdates/correctly-parses-wild-card-directories-from-implicit-glob-when-two-keys-differ-only-in-directory-seperator.js diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index ba368e3c566..09705e74f26 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -3548,7 +3548,7 @@ namespace ts { } if (isImplicitGlob(spec.substring(spec.lastIndexOf(directorySeparator) + 1))) { return { - key: useCaseSensitiveFileNames ? spec : toFileNameLowerCase(spec), + key: removeTrailingDirectorySeparator(useCaseSensitiveFileNames ? spec : toFileNameLowerCase(spec)), flags: WatchDirectoryFlags.Recursive }; } diff --git a/src/compiler/watchPublic.ts b/src/compiler/watchPublic.ts index 8af1397e19c..53e36660414 100644 --- a/src/compiler/watchPublic.ts +++ b/src/compiler/watchPublic.ts @@ -700,6 +700,7 @@ namespace ts { function reloadFileNamesFromConfigFile() { writeLog("Reloading new file names and options"); + reloadLevel = ConfigFileProgramReloadLevel.None; rootFileNames = getFileNamesFromConfigSpecs(compilerOptions.configFile!.configFileSpecs!, getNormalizedAbsolutePath(getDirectoryPath(configFileName), currentDirectory), compilerOptions, parseConfigFileHost, extraFileExtensions); if (updateErrorForNoInputFiles(rootFileNames, getNormalizedAbsolutePath(configFileName, currentDirectory), compilerOptions.configFile!.configFileSpecs!, configFileParsingDiagnostics!, canConfigFileJsonReportNoInputFiles)) { hasChangedConfigFileParsingErrors = true; diff --git a/src/testRunner/unittests/config/tsconfigParsing.ts b/src/testRunner/unittests/config/tsconfigParsing.ts index de793bffa4e..9a5e46322e4 100644 --- a/src/testRunner/unittests/config/tsconfigParsing.ts +++ b/src/testRunner/unittests/config/tsconfigParsing.ts @@ -421,5 +421,14 @@ namespace ts { const parsedCommand = parseJsonConfigFileContent(parsed.config, sys, "/foo.bar"); assert.deepEqual(parsedCommand.wildcardDirectories, { "/foo.bar/src": WatchDirectoryFlags.Recursive }); }); + + it("correctly parses wild card directories from implicit glob when two keys differ only in directory seperator", () => { + const parsed = parseConfigFileTextToJson("/foo.bar/tsconfig.json", JSON.stringify({ + include: ["./", "./**/*.json"] + })); + + const parsedCommand = parseJsonConfigFileContent(parsed.config, sys, "/foo"); + assert.deepEqual(parsedCommand.wildcardDirectories, { "/foo": WatchDirectoryFlags.Recursive }); + }); }); } diff --git a/src/testRunner/unittests/tscWatch/programUpdates.ts b/src/testRunner/unittests/tscWatch/programUpdates.ts index 68f03f0d20c..a17f857024f 100644 --- a/src/testRunner/unittests/tscWatch/programUpdates.ts +++ b/src/testRunner/unittests/tscWatch/programUpdates.ts @@ -616,6 +616,39 @@ export class A { ] }); + verifyTscWatch({ + scenario, + subScenario: "correctly parses wild card directories from implicit glob when two keys differ only in directory seperator", + commandLineArgs: ["-w", "--extendedDiagnostics"], + sys: () => { + const file1 = { + path: `${projectRoot}/f1.ts`, + content: "export const x = 1" + }; + const file2 = { + path: `${projectRoot}/f2.ts`, + content: "export const y = 1" + }; + const configFile = { + path: `${projectRoot}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true }, include: ["./", "./**/*.json"] }) + }; + return createWatchedSystem([file1, file2, libFile, configFile], { currentDirectory: projectRoot }); + }, + changes: [ + { + caption: "Add new file", + change: sys => sys.writeFile(`${projectRoot}/new-file.ts`, "export const z = 1;"), + timeouts: sys => sys.checkTimeoutQueueLengthAndRun(1), + }, + { + caption: "Import new file", + change: sys => sys.prependFile(`${projectRoot}/f1.ts`, `import { z } from "./new-file";`), + timeouts: sys => sys.checkTimeoutQueueLengthAndRun(1), + } + ] + }); + verifyTscWatch({ scenario, subScenario: "can correctly update configured project when set of root files has changed through include", diff --git a/tests/baselines/reference/tscWatch/programUpdates/correctly-parses-wild-card-directories-from-implicit-glob-when-two-keys-differ-only-in-directory-seperator.js b/tests/baselines/reference/tscWatch/programUpdates/correctly-parses-wild-card-directories-from-implicit-glob-when-two-keys-differ-only-in-directory-seperator.js new file mode 100644 index 00000000000..2fded49fea8 --- /dev/null +++ b/tests/baselines/reference/tscWatch/programUpdates/correctly-parses-wild-card-directories-from-implicit-glob-when-two-keys-differ-only-in-directory-seperator.js @@ -0,0 +1,388 @@ +Input:: +//// [/user/username/projects/myproject/f1.ts] +export const x = 1 + +//// [/user/username/projects/myproject/f2.ts] +export const y = 1 + +//// [/a/lib/lib.d.ts] +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } + +//// [/user/username/projects/myproject/tsconfig.json] +{"compilerOptions":{"composite":true},"include":["./","./**/*.json"]} + + +/a/lib/tsc.js -w --extendedDiagnostics +Output:: +[12:00:23 AM] Starting compilation in watch mode... + +Current directory: /user/username/projects/myproject CaseSensitiveFileNames: false +FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 undefined Config file +Synchronizing program +CreatingProgramWith:: + roots: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts"] + options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/f1.ts 250 undefined Source file +FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/f2.ts 250 undefined Source file +FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 undefined Source file +DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Type roots +Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Type roots +[12:00:34 AM] Found 0 errors. Watching for file changes. + +DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory + + +Program root files: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts"] +Program options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +Program structureReused: Not +Program files:: +/a/lib/lib.d.ts +/user/username/projects/myproject/f1.ts +/user/username/projects/myproject/f2.ts + +Semantic diagnostics in builder refreshed for:: +/a/lib/lib.d.ts +/user/username/projects/myproject/f1.ts +/user/username/projects/myproject/f2.ts + +Shape signatures in builder refreshed for:: +/a/lib/lib.d.ts (used version) +/user/username/projects/myproject/f1.ts (computed .d.ts during emit) +/user/username/projects/myproject/f2.ts (computed .d.ts during emit) + +WatchedFiles:: +/user/username/projects/myproject/tsconfig.json: + {"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250} +/user/username/projects/myproject/f1.ts: + {"fileName":"/user/username/projects/myproject/f1.ts","pollingInterval":250} +/user/username/projects/myproject/f2.ts: + {"fileName":"/user/username/projects/myproject/f2.ts","pollingInterval":250} +/a/lib/lib.d.ts: + {"fileName":"/a/lib/lib.d.ts","pollingInterval":250} + +FsWatches:: + +FsWatchesRecursive:: +/user/username/projects/myproject/node_modules/@types: + {"directoryName":"/user/username/projects/myproject/node_modules/@types","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject: + {"directoryName":"/user/username/projects/myproject","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +exitCode:: ExitStatus.undefined + +//// [/user/username/projects/myproject/f1.js] +"use strict"; +exports.__esModule = true; +exports.x = void 0; +exports.x = 1; + + +//// [/user/username/projects/myproject/f1.d.ts] +export declare const x = 1; + + +//// [/user/username/projects/myproject/f2.js] +"use strict"; +exports.__esModule = true; +exports.y = void 0; +exports.y = 1; + + +//// [/user/username/projects/myproject/f2.d.ts] +export declare const y = 1; + + +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../../../a/lib/lib.d.ts","./f1.ts","./f2.ts"],"fileInfos":[{"version":"-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }","affectsGlobalScope":true},{"version":"-10906998252-export const x = 1","signature":"-7495133367-export declare const x = 1;\n"},{"version":"-10905812331-export const y = 1","signature":"-6203665398-export declare const y = 1;\n"}],"options":{"composite":true},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,3]},"version":"FakeTSVersion"} + +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../../../a/lib/lib.d.ts", + "./f1.ts", + "./f2.ts" + ], + "fileInfos": { + "../../../../a/lib/lib.d.ts": { + "version": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "signature": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "affectsGlobalScope": true + }, + "./f1.ts": { + "version": "-10906998252-export const x = 1", + "signature": "-7495133367-export declare const x = 1;\n" + }, + "./f2.ts": { + "version": "-10905812331-export const y = 1", + "signature": "-6203665398-export declare const y = 1;\n" + } + }, + "options": { + "composite": true + }, + "referencedMap": {}, + "exportedModulesMap": {}, + "semanticDiagnosticsPerFile": [ + "../../../../a/lib/lib.d.ts", + "./f1.ts", + "./f2.ts" + ] + }, + "version": "FakeTSVersion", + "size": 828 +} + + +Change:: Add new file + +Input:: +//// [/user/username/projects/myproject/new-file.ts] +export const z = 1; + + +Output:: +DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +Scheduling update +Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +Reloading new file names and options +Synchronizing program +[12:00:39 AM] File change detected. Starting incremental compilation... + +CreatingProgramWith:: + roots: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts","/user/username/projects/myproject/new-file.ts"] + options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/new-file.ts 250 undefined Source file +DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.js :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +Project: /user/username/projects/myproject/tsconfig.json Detected file add/remove of non supported extension: /user/username/projects/myproject/new-file.js +Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.js :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.d.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +Project: /user/username/projects/myproject/tsconfig.json Detected output file: /user/username/projects/myproject/new-file.d.ts +Elapsed:: *ms DirectoryWatcher:: Triggered with /user/username/projects/myproject/new-file.d.ts :: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory +[12:00:47 AM] Found 0 errors. Watching for file changes. + + + +Program root files: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts","/user/username/projects/myproject/new-file.ts"] +Program options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +Program structureReused: Not +Program files:: +/a/lib/lib.d.ts +/user/username/projects/myproject/f1.ts +/user/username/projects/myproject/f2.ts +/user/username/projects/myproject/new-file.ts + +Semantic diagnostics in builder refreshed for:: +/user/username/projects/myproject/new-file.ts + +Shape signatures in builder refreshed for:: +/user/username/projects/myproject/new-file.ts (computed .d.ts) + +WatchedFiles:: +/user/username/projects/myproject/tsconfig.json: + {"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250} +/user/username/projects/myproject/f1.ts: + {"fileName":"/user/username/projects/myproject/f1.ts","pollingInterval":250} +/user/username/projects/myproject/f2.ts: + {"fileName":"/user/username/projects/myproject/f2.ts","pollingInterval":250} +/a/lib/lib.d.ts: + {"fileName":"/a/lib/lib.d.ts","pollingInterval":250} +/user/username/projects/myproject/new-file.ts: + {"fileName":"/user/username/projects/myproject/new-file.ts","pollingInterval":250} + +FsWatches:: + +FsWatchesRecursive:: +/user/username/projects/myproject/node_modules/@types: + {"directoryName":"/user/username/projects/myproject/node_modules/@types","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject: + {"directoryName":"/user/username/projects/myproject","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +exitCode:: ExitStatus.undefined + +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../../../a/lib/lib.d.ts","./f1.ts","./f2.ts","./new-file.ts"],"fileInfos":[{"version":"-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }","affectsGlobalScope":true},{"version":"-10906998252-export const x = 1","signature":"-7495133367-export declare const x = 1;\n"},{"version":"-10905812331-export const y = 1","signature":"-6203665398-export declare const y = 1;\n"},{"version":"-11960320495-export const z = 1;","signature":"-9207164725-export declare const z = 1;\n"}],"options":{"composite":true},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,3,4]},"version":"FakeTSVersion"} + +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../../../a/lib/lib.d.ts", + "./f1.ts", + "./f2.ts", + "./new-file.ts" + ], + "fileInfos": { + "../../../../a/lib/lib.d.ts": { + "version": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "signature": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "affectsGlobalScope": true + }, + "./f1.ts": { + "version": "-10906998252-export const x = 1", + "signature": "-7495133367-export declare const x = 1;\n" + }, + "./f2.ts": { + "version": "-10905812331-export const y = 1", + "signature": "-6203665398-export declare const y = 1;\n" + }, + "./new-file.ts": { + "version": "-11960320495-export const z = 1;", + "signature": "-9207164725-export declare const z = 1;\n" + } + }, + "options": { + "composite": true + }, + "referencedMap": {}, + "exportedModulesMap": {}, + "semanticDiagnosticsPerFile": [ + "../../../../a/lib/lib.d.ts", + "./f1.ts", + "./f2.ts", + "./new-file.ts" + ] + }, + "version": "FakeTSVersion", + "size": 949 +} + +//// [/user/username/projects/myproject/new-file.js] +"use strict"; +exports.__esModule = true; +exports.z = void 0; +exports.z = 1; + + +//// [/user/username/projects/myproject/new-file.d.ts] +export declare const z = 1; + + + +Change:: Import new file + +Input:: +//// [/user/username/projects/myproject/f1.ts] +import { z } from "./new-file";export const x = 1 + + +Output:: +FileWatcher:: Triggered with /user/username/projects/myproject/f1.ts 1:: WatchInfo: /user/username/projects/myproject/f1.ts 250 undefined Source file +Scheduling update +Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/f1.ts 1:: WatchInfo: /user/username/projects/myproject/f1.ts 250 undefined Source file +Synchronizing program +[12:00:53 AM] File change detected. Starting incremental compilation... + +CreatingProgramWith:: + roots: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts","/user/username/projects/myproject/new-file.ts"] + options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +[12:01:03 AM] Found 0 errors. Watching for file changes. + + + +Program root files: ["/user/username/projects/myproject/f1.ts","/user/username/projects/myproject/f2.ts","/user/username/projects/myproject/new-file.ts"] +Program options: {"composite":true,"watch":true,"extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +Program structureReused: SafeModules +Program files:: +/a/lib/lib.d.ts +/user/username/projects/myproject/new-file.ts +/user/username/projects/myproject/f1.ts +/user/username/projects/myproject/f2.ts + +Semantic diagnostics in builder refreshed for:: +/user/username/projects/myproject/f1.ts + +Shape signatures in builder refreshed for:: +/user/username/projects/myproject/f1.ts (computed .d.ts) + +WatchedFiles:: +/user/username/projects/myproject/tsconfig.json: + {"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250} +/user/username/projects/myproject/f1.ts: + {"fileName":"/user/username/projects/myproject/f1.ts","pollingInterval":250} +/user/username/projects/myproject/f2.ts: + {"fileName":"/user/username/projects/myproject/f2.ts","pollingInterval":250} +/a/lib/lib.d.ts: + {"fileName":"/a/lib/lib.d.ts","pollingInterval":250} +/user/username/projects/myproject/new-file.ts: + {"fileName":"/user/username/projects/myproject/new-file.ts","pollingInterval":250} + +FsWatches:: + +FsWatchesRecursive:: +/user/username/projects/myproject/node_modules/@types: + {"directoryName":"/user/username/projects/myproject/node_modules/@types","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject: + {"directoryName":"/user/username/projects/myproject","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +exitCode:: ExitStatus.undefined + +//// [/user/username/projects/myproject/f1.js] file written with same contents +//// [/user/username/projects/myproject/f1.d.ts] file written with same contents +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../../../a/lib/lib.d.ts","./new-file.ts","./f1.ts","./f2.ts"],"fileInfos":[{"version":"-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }","affectsGlobalScope":true},{"version":"-11960320495-export const z = 1;","signature":"-9207164725-export declare const z = 1;\n"},{"version":"1363236232-import { z } from \"./new-file\";export const x = 1","signature":"-7495133367-export declare const x = 1;\n"},{"version":"-10905812331-export const y = 1","signature":"-6203665398-export declare const y = 1;\n"}],"options":{"composite":true},"fileIdsList":[[2]],"referencedMap":[[3,1]],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,3,4,2]},"version":"FakeTSVersion"} + +//// [/user/username/projects/myproject/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../../../a/lib/lib.d.ts", + "./new-file.ts", + "./f1.ts", + "./f2.ts" + ], + "fileNamesList": [ + [ + "./new-file.ts" + ] + ], + "fileInfos": { + "../../../../a/lib/lib.d.ts": { + "version": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "signature": "-7698705165-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }", + "affectsGlobalScope": true + }, + "./new-file.ts": { + "version": "-11960320495-export const z = 1;", + "signature": "-9207164725-export declare const z = 1;\n" + }, + "./f1.ts": { + "version": "1363236232-import { z } from \"./new-file\";export const x = 1", + "signature": "-7495133367-export declare const x = 1;\n" + }, + "./f2.ts": { + "version": "-10905812331-export const y = 1", + "signature": "-6203665398-export declare const y = 1;\n" + } + }, + "options": { + "composite": true + }, + "referencedMap": { + "./f1.ts": [ + "./new-file.ts" + ] + }, + "exportedModulesMap": {}, + "semanticDiagnosticsPerFile": [ + "../../../../a/lib/lib.d.ts", + "./f1.ts", + "./f2.ts", + "./new-file.ts" + ] + }, + "version": "FakeTSVersion", + "size": 1005 +} + From 77ffb96b1a7b3f9d2e7d2417ff8f0cb0f815b977 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 25 May 2022 15:27:27 -0700 Subject: [PATCH 3/3] Fix extensions for noDtsResolution in node16/nodenext (#49233) --- src/compiler/moduleNameResolver.ts | 14 +++++--- .../fourslash/server/goToSource13_nodenext.ts | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/cases/fourslash/server/goToSource13_nodenext.ts diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 8f466c0495a..d2b0db1041b 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -1288,18 +1288,22 @@ namespace ts { ); } + const jsOnlyExtensions = [Extensions.JavaScript]; + const tsExtensions = [Extensions.TypeScript, Extensions.JavaScript]; + const tsPlusJsonExtensions = [...tsExtensions, Extensions.Json]; + const tsconfigExtensions = [Extensions.TSConfig]; function nodeNextModuleNameResolverWorker(features: NodeResolutionFeatures, moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations { const containingDirectory = getDirectoryPath(containingFile); // es module file or cjs-like input file, use a variant of the legacy cjs resolver that supports the selected modern features const esmMode = resolutionMode === ModuleKind.ESNext ? NodeResolutionFeatures.EsmMode : 0; - return nodeModuleNameResolverWorker(features | esmMode, moduleName, containingDirectory, compilerOptions, host, cache, compilerOptions.resolveJsonModule ? tsPlusJsonExtensions : tsExtensions, redirectedReference); + let extensions = compilerOptions.noDtsResolution ? [Extensions.TsOnly, Extensions.JavaScript] : tsExtensions; + if (compilerOptions.resolveJsonModule) { + extensions = [...extensions, Extensions.Json]; + } + return nodeModuleNameResolverWorker(features | esmMode, moduleName, containingDirectory, compilerOptions, host, cache, extensions, redirectedReference); } - const jsOnlyExtensions = [Extensions.JavaScript]; - const tsExtensions = [Extensions.TypeScript, Extensions.JavaScript]; - const tsPlusJsonExtensions = [...tsExtensions, Extensions.Json]; - const tsconfigExtensions = [Extensions.TSConfig]; function tryResolveJSModuleWorker(moduleName: string, initialDir: string, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations { return nodeModuleNameResolverWorker(NodeResolutionFeatures.None, moduleName, initialDir, { moduleResolution: ModuleResolutionKind.NodeJs, allowJs: true }, host, /*cache*/ undefined, jsOnlyExtensions, /*redirectedReferences*/ undefined); } diff --git a/tests/cases/fourslash/server/goToSource13_nodenext.ts b/tests/cases/fourslash/server/goToSource13_nodenext.ts new file mode 100644 index 00000000000..99a3a29ef2e --- /dev/null +++ b/tests/cases/fourslash/server/goToSource13_nodenext.ts @@ -0,0 +1,35 @@ +/// + +// @Filename: /node_modules/left-pad/package.json +//// { +//// "name": "left-pad", +//// "version": "1.3.0", +//// "description": "String left pad", +//// "main": "index.js", +//// "types": "index.d.ts" +//// } + +// @Filename: /node_modules/left-pad/index.d.ts +//// declare function leftPad(str: string|number, len: number, ch?: string|number): string; +//// declare namespace leftPad { } +//// export = leftPad; + +// @Filename: /node_modules/left-pad/index.js +//// module.exports = leftPad; +//// function /*end*/leftPad(str, len, ch) {} + +// @Filename: /tsconfig.json +//// { +//// "compilerOptions": { +//// "module": "node16", +//// "strict": true, +//// "outDir": "./out", +//// +//// } +//// } + +// @Filename: /index.mts +//// import leftPad = require("left-pad"); +//// /*start*/leftPad("", 4); + +verify.goToSourceDefinition("start", "end");