|
| 1 | +package fourslash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/microsoft/typescript-go/internal/core" |
| 7 | + "github.com/microsoft/typescript-go/internal/fourslash" |
| 8 | + . "github.com/microsoft/typescript-go/internal/fourslash/tests/util" |
| 9 | + "github.com/microsoft/typescript-go/internal/ls" |
| 10 | + "github.com/microsoft/typescript-go/internal/ls/lsutil" |
| 11 | + "github.com/microsoft/typescript-go/internal/lsp/lsproto" |
| 12 | + "github.com/microsoft/typescript-go/internal/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +func TestPnpAutoImportCompletion1(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 18 | + const content = ` |
| 19 | +// @filename: /.pnp.cjs |
| 20 | +module.exports = {}; |
| 21 | +
|
| 22 | +// @filename: /.pnp.data.json |
| 23 | +{ |
| 24 | + "dependencyTreeRoots": [ |
| 25 | + { |
| 26 | + "name": "project", |
| 27 | + "reference": "workspace:." |
| 28 | + } |
| 29 | + ], |
| 30 | + "ignorePatternData": null, |
| 31 | + "enableTopLevelFallback": false, |
| 32 | + "fallbackPool": [], |
| 33 | + "fallbackExclusionList": [], |
| 34 | + "packageRegistryData": [ |
| 35 | + ["project", [ |
| 36 | + ["workspace:.", { |
| 37 | + "packageLocation": "./", |
| 38 | + "packageDependencies": [ |
| 39 | + ["package-a", "npm:1.0.0"], |
| 40 | + ["workspace-lib", "workspace:libs/workspace-lib"] |
| 41 | + ] |
| 42 | + }] |
| 43 | + ]], |
| 44 | + ["package-a", [ |
| 45 | + ["npm:1.0.0", { |
| 46 | + "packageLocation": "./.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/", |
| 47 | + "packageDependencies": [] |
| 48 | + }] |
| 49 | + ]], |
| 50 | + ["package-b", [ |
| 51 | + ["npm:1.0.0", { |
| 52 | + "packageLocation": "./.yarn/cache/package-b-npm-1.0.0-efgh5678/node_modules/package-b/", |
| 53 | + "packageDependencies": [] |
| 54 | + }] |
| 55 | + ]], |
| 56 | + ["workspace-lib", [ |
| 57 | + ["workspace:libs/workspace-lib", { |
| 58 | + "packageLocation": "./libs/workspace-lib/", |
| 59 | + "packageDependencies": [] |
| 60 | + }] |
| 61 | + ]] |
| 62 | + ] |
| 63 | +} |
| 64 | +
|
| 65 | +// @filename: package.json |
| 66 | +{ |
| 67 | + "name": "project", |
| 68 | + "workspaces": [ |
| 69 | + "libs/*" |
| 70 | + ], |
| 71 | + "dependencies": { |
| 72 | + "package-a": "npm:1.0.0", |
| 73 | + "workspace-lib": "workspace:*" |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +// @filename: /.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/package.json |
| 78 | +{ |
| 79 | + "name": "package-a", |
| 80 | + "version": "1.0.0", |
| 81 | + "exports": { |
| 82 | + ".": {"types": "./index.d.ts", "default": "./index.js"}, |
| 83 | + "./subpath": {"types": "./helper.d.ts", "default": "./helper.js"} |
| 84 | + }, |
| 85 | + "types": "./index.d.ts", |
| 86 | + "main": "./index.js" |
| 87 | +} |
| 88 | +
|
| 89 | +// @filename: /.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/index.js |
| 90 | +export const aValue = "Some Var"; |
| 91 | +
|
| 92 | +// @filename: /.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/index.d.ts |
| 93 | +export declare const aValue: string; |
| 94 | +
|
| 95 | +// @filename: /.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/helper.js |
| 96 | +export function helperA(value) { |
| 97 | + return "Helper A: " + value; |
| 98 | +}; |
| 99 | +
|
| 100 | +// @filename: /.yarn/cache/package-a-npm-1.0.0-abcd1234/node_modules/package-a/helper.d.ts |
| 101 | +export declare function helperA(value: string): string; |
| 102 | +
|
| 103 | +// @filename: /libs/workspace-lib/package.json |
| 104 | +{ |
| 105 | + "name": "workspace-lib", |
| 106 | + "version": "1.0.0", |
| 107 | + "exports": { |
| 108 | + ".": {"types": "./index.d.ts", "default": "./index.js"} |
| 109 | + }, |
| 110 | + "types": "./index.d.ts", |
| 111 | + "main": "./index.js" |
| 112 | +} |
| 113 | +
|
| 114 | +// @filename: /libs/workspace-lib/index.js |
| 115 | +export const workspaceValue = "Workspace Value"; |
| 116 | +export function workspaceHelper() { |
| 117 | + return "Helper from workspace"; |
| 118 | +}; |
| 119 | +
|
| 120 | +// @filename: /libs/workspace-lib/index.d.ts |
| 121 | +export declare const workspaceValue: string; |
| 122 | +export declare function workspaceHelper(): string; |
| 123 | +
|
| 124 | +// @filename: /.yarn/cache/package-b-npm-1.0.0-efgh5678/node_modules/package-b/package.json |
| 125 | +{ |
| 126 | + "name": "package-b", |
| 127 | + "version": "1.0.0", |
| 128 | + "exports": { |
| 129 | + ".": {"types": "./index.d.ts", "default": "./index.js"} |
| 130 | + } |
| 131 | +} |
| 132 | +
|
| 133 | +// @filename: /.yarn/cache/package-b-npm-1.0.0-efgh5678/node_modules/package-b/index.js |
| 134 | +export const bValue = "B Value"; |
| 135 | +export function helperB(value) { |
| 136 | + return "Helper B: " + value; |
| 137 | +}; |
| 138 | +
|
| 139 | +// @filename: /.yarn/cache/package-b-npm-1.0.0-efgh5678/node_modules/package-b/index.d.ts |
| 140 | +export declare const bValue: string; |
| 141 | +export declare function helperB(value: string): string; |
| 142 | +
|
| 143 | +// @filename: /src/index.ts |
| 144 | +import { bValue } from 'package-b'; // Should be erroring because package-b is not in project's dependencies |
| 145 | +import { aValue } from 'package-a'; |
| 146 | +aValue; |
| 147 | +/**/ |
| 148 | +` |
| 149 | + f := fourslash.NewFourslash(t, nil /*capabilities*/, content) |
| 150 | + f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{ |
| 151 | + UserPreferences: &lsutil.UserPreferences{ |
| 152 | + IncludeCompletionsForModuleExports: core.TSTrue, |
| 153 | + IncludeCompletionsForImportStatements: core.TSTrue, |
| 154 | + }, |
| 155 | + IsIncomplete: false, |
| 156 | + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ |
| 157 | + CommitCharacters: &DefaultCommitCharacters, |
| 158 | + EditRange: Ignored, |
| 159 | + }, |
| 160 | + Items: &fourslash.CompletionsExpectedItems{ |
| 161 | + Includes: []fourslash.CompletionsExpectedItem{ |
| 162 | + // Verify that helperA completion creates an import from 'package-a/subpath' |
| 163 | + &lsproto.CompletionItem{ |
| 164 | + Label: "helperA", |
| 165 | + Data: PtrTo(any(&ls.CompletionItemData{ |
| 166 | + AutoImport: &ls.AutoImportData{ |
| 167 | + ModuleSpecifier: "package-a/subpath", |
| 168 | + }, |
| 169 | + })), |
| 170 | + Kind: PtrTo(lsproto.CompletionItemKindFunction), |
| 171 | + AdditionalTextEdits: fourslash.AnyTextEdits, |
| 172 | + SortText: PtrTo(string(ls.SortTextAutoImportSuggestions)), |
| 173 | + }, |
| 174 | + // Verify that workspaceHelper completion creates an import from 'workspace-lib' |
| 175 | + &lsproto.CompletionItem{ |
| 176 | + Label: "workspaceHelper", |
| 177 | + Data: PtrTo(any(&ls.CompletionItemData{ |
| 178 | + AutoImport: &ls.AutoImportData{ |
| 179 | + ModuleSpecifier: "workspace-lib", |
| 180 | + }, |
| 181 | + })), |
| 182 | + Kind: PtrTo(lsproto.CompletionItemKindFunction), |
| 183 | + AdditionalTextEdits: fourslash.AnyTextEdits, |
| 184 | + SortText: PtrTo(string(ls.SortTextAutoImportSuggestions)), |
| 185 | + }, |
| 186 | + }, |
| 187 | + // `bValue` can appear in the completion list since it's mentioned in a unresolved import, but `helperB` should not |
| 188 | + Excludes: []string{"helperB"}, |
| 189 | + }, |
| 190 | + }) |
| 191 | +} |
0 commit comments