Skip to content

Commit f7ef154

Browse files
authored
Use options from referenced project for including resolved imports in 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
1 parent 7a2a687 commit f7ef154

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

src/compiler/program.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2907,6 +2907,7 @@ namespace ts {
29072907
const moduleNames = getModuleNames(file);
29082908
const resolutions = resolveModuleNamesReusingOldState(moduleNames, file);
29092909
Debug.assert(resolutions.length === moduleNames.length);
2910+
const optionsForFile = (useSourceOfProjectReferenceRedirect ? getRedirectReferenceForResolution(file)?.commandLine.options : undefined) || options;
29102911
for (let index = 0; index < moduleNames.length; index++) {
29112912
const resolution = resolutions[index];
29122913
setResolvedModule(file, moduleNames[index], resolution);
@@ -2933,11 +2934,11 @@ namespace ts {
29332934
// Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs')
29342935
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
29352936
const shouldAddFile = resolvedFileName
2936-
&& !getResolutionDiagnostic(options, resolution)
2937-
&& !options.noResolve
2937+
&& !getResolutionDiagnostic(optionsForFile, resolution)
2938+
&& !optionsForFile.noResolve
29382939
&& index < file.imports.length
29392940
&& !elideImport
2940-
&& !(isJsFile && !getAllowJSCompilerOption(options))
2941+
&& !(isJsFile && !getAllowJSCompilerOption(optionsForFile))
29412942
&& (isInJSFile(file.imports[index]) || !(file.imports[index].flags & NodeFlags.JSDoc));
29422943

29432944
if (elideImport) {

src/testRunner/unittests/tsserver/projectReferences.ts

+82
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,88 @@ bar();
476476
});
477477
});
478478

479+
it("when the referenced projects have allowJs and emitDeclarationOnly", () => {
480+
const compositeConfig: File = {
481+
path: `${tscWatch.projectRoot}/packages/emit-composite/tsconfig.json`,
482+
content: JSON.stringify({
483+
compilerOptions: {
484+
composite: true,
485+
allowJs: true,
486+
emitDeclarationOnly: true,
487+
outDir: "lib",
488+
rootDir: "src"
489+
},
490+
include: ["src"]
491+
})
492+
};
493+
const compositePackageJson: File = {
494+
path: `${tscWatch.projectRoot}/packages/emit-composite/package.json`,
495+
content: JSON.stringify({
496+
name: "emit-composite",
497+
version: "1.0.0",
498+
main: "src/index.js",
499+
typings: "lib/index.d.ts"
500+
})
501+
};
502+
const compositeIndex: File = {
503+
path: `${tscWatch.projectRoot}/packages/emit-composite/src/index.js`,
504+
content: `const testModule = require('./testModule');
505+
module.exports = {
506+
...testModule
507+
}`
508+
};
509+
const compositeTestModule: File = {
510+
path: `${tscWatch.projectRoot}/packages/emit-composite/src/testModule.js`,
511+
content: `/**
512+
* @param {string} arg
513+
*/
514+
const testCompositeFunction = (arg) => {
515+
}
516+
module.exports = {
517+
testCompositeFunction
518+
}`
519+
};
520+
const consumerConfig: File = {
521+
path: `${tscWatch.projectRoot}/packages/consumer/tsconfig.json`,
522+
content: JSON.stringify({
523+
include: ["src"],
524+
references: [{ path: "../emit-composite" }]
525+
})
526+
};
527+
const consumerIndex: File = {
528+
path: `${tscWatch.projectRoot}/packages/consumer/src/index.ts`,
529+
content: `import { testCompositeFunction } from 'emit-composite';
530+
testCompositeFunction('why hello there');
531+
testCompositeFunction('why hello there', 42);`
532+
};
533+
const symlink: SymLink = {
534+
path: `${tscWatch.projectRoot}/node_modules/emit-composite`,
535+
symLink: `${tscWatch.projectRoot}/packages/emit-composite`
536+
};
537+
const host = createServerHost([libFile, compositeConfig, compositePackageJson, compositeIndex, compositeTestModule, consumerConfig, consumerIndex, symlink], { useCaseSensitiveFileNames: true });
538+
const session = createSession(host, { canUseEvents: true });
539+
const service = session.getProjectService();
540+
openFilesForSession([consumerIndex], session);
541+
checkNumberOfProjects(service, { configuredProjects: 1 });
542+
checkProjectActualFiles(
543+
service.configuredProjects.get(consumerConfig.path)!,
544+
[consumerIndex.path, libFile.path, consumerConfig.path, compositeIndex.path, compositeTestModule.path]
545+
);
546+
const secondArg = protocolTextSpanFromSubstring(consumerIndex.content, "42");
547+
verifyGetErrRequest({
548+
host,
549+
session,
550+
expected: [{
551+
file: consumerIndex,
552+
syntax: [],
553+
semantic: [
554+
createDiagnostic(secondArg.start, secondArg.end, Diagnostics.Expected_0_arguments_but_got_1, ["1", "2"]),
555+
],
556+
suggestion: []
557+
}]
558+
});
559+
});
560+
479561
it("when finding local reference doesnt load ancestor/sibling projects", () => {
480562
const solutionLocation = "/user/username/projects/solution";
481563
const solution: File = {

0 commit comments

Comments
 (0)