From f7ef1540d3c10fb282d1d433d9f2850b28391169 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 3 May 2021 11:35:21 -0700 Subject: [PATCH] Use options from referenced project for including resolved imports in the file when using sources of project reference (#43914) * Test where allowJs present in referenced project affects picking up right set of import files * use options from referened project for including resolved imports in the file when using sources of project reference Fixes #43909 --- src/compiler/program.ts | 7 +- .../unittests/tsserver/projectReferences.ts | 82 +++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 869202fe2ed..a50b60a3171 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2907,6 +2907,7 @@ namespace ts { const moduleNames = getModuleNames(file); const resolutions = resolveModuleNamesReusingOldState(moduleNames, file); Debug.assert(resolutions.length === moduleNames.length); + const optionsForFile = (useSourceOfProjectReferenceRedirect ? getRedirectReferenceForResolution(file)?.commandLine.options : undefined) || options; for (let index = 0; index < moduleNames.length; index++) { const resolution = resolutions[index]; setResolvedModule(file, moduleNames[index], resolution); @@ -2933,11 +2934,11 @@ namespace ts { // Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs') // This may still end up being an untyped module -- the file won't be included but imports will be allowed. const shouldAddFile = resolvedFileName - && !getResolutionDiagnostic(options, resolution) - && !options.noResolve + && !getResolutionDiagnostic(optionsForFile, resolution) + && !optionsForFile.noResolve && index < file.imports.length && !elideImport - && !(isJsFile && !getAllowJSCompilerOption(options)) + && !(isJsFile && !getAllowJSCompilerOption(optionsForFile)) && (isInJSFile(file.imports[index]) || !(file.imports[index].flags & NodeFlags.JSDoc)); if (elideImport) { diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index c8a86ab44b8..1645033cd58 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -476,6 +476,88 @@ bar(); }); }); + it("when the referenced projects have allowJs and emitDeclarationOnly", () => { + const compositeConfig: File = { + path: `${tscWatch.projectRoot}/packages/emit-composite/tsconfig.json`, + content: JSON.stringify({ + compilerOptions: { + composite: true, + allowJs: true, + emitDeclarationOnly: true, + outDir: "lib", + rootDir: "src" + }, + include: ["src"] + }) + }; + const compositePackageJson: File = { + path: `${tscWatch.projectRoot}/packages/emit-composite/package.json`, + content: JSON.stringify({ + name: "emit-composite", + version: "1.0.0", + main: "src/index.js", + typings: "lib/index.d.ts" + }) + }; + const compositeIndex: File = { + path: `${tscWatch.projectRoot}/packages/emit-composite/src/index.js`, + content: `const testModule = require('./testModule'); +module.exports = { + ...testModule +}` + }; + const compositeTestModule: File = { + path: `${tscWatch.projectRoot}/packages/emit-composite/src/testModule.js`, + content: `/** + * @param {string} arg + */ + const testCompositeFunction = (arg) => { +} +module.exports = { + testCompositeFunction +}` + }; + const consumerConfig: File = { + path: `${tscWatch.projectRoot}/packages/consumer/tsconfig.json`, + content: JSON.stringify({ + include: ["src"], + references: [{ path: "../emit-composite" }] + }) + }; + const consumerIndex: File = { + path: `${tscWatch.projectRoot}/packages/consumer/src/index.ts`, + content: `import { testCompositeFunction } from 'emit-composite'; +testCompositeFunction('why hello there'); +testCompositeFunction('why hello there', 42);` + }; + const symlink: SymLink = { + path: `${tscWatch.projectRoot}/node_modules/emit-composite`, + symLink: `${tscWatch.projectRoot}/packages/emit-composite` + }; + const host = createServerHost([libFile, compositeConfig, compositePackageJson, compositeIndex, compositeTestModule, consumerConfig, consumerIndex, symlink], { useCaseSensitiveFileNames: true }); + const session = createSession(host, { canUseEvents: true }); + const service = session.getProjectService(); + openFilesForSession([consumerIndex], session); + checkNumberOfProjects(service, { configuredProjects: 1 }); + checkProjectActualFiles( + service.configuredProjects.get(consumerConfig.path)!, + [consumerIndex.path, libFile.path, consumerConfig.path, compositeIndex.path, compositeTestModule.path] + ); + const secondArg = protocolTextSpanFromSubstring(consumerIndex.content, "42"); + verifyGetErrRequest({ + host, + session, + expected: [{ + file: consumerIndex, + syntax: [], + semantic: [ + createDiagnostic(secondArg.start, secondArg.end, Diagnostics.Expected_0_arguments_but_got_1, ["1", "2"]), + ], + suggestion: [] + }] + }); + }); + it("when finding local reference doesnt load ancestor/sibling projects", () => { const solutionLocation = "/user/username/projects/solution"; const solution: File = {