Skip to content

Commit

Permalink
Use options from referenced project for including resolved imports in…
Browse files Browse the repository at this point in the history
… the file when using sources of project reference (microsoft#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 microsoft#43909
  • Loading branch information
sheetalkamat authored May 3, 2021
1 parent 7a2a687 commit f7ef154
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
82 changes: 82 additions & 0 deletions src/testRunner/unittests/tsserver/projectReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit f7ef154

Please sign in to comment.