Skip to content

Commit

Permalink
Make stub for hasInvalidatedResolution
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Sep 14, 2022
1 parent 4110b80 commit 8a4b96d
Show file tree
Hide file tree
Showing 4 changed files with 649 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/compiler/watchPublic.ts
Expand Up @@ -112,6 +112,7 @@ namespace ts {
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];
/** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[] | readonly FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: SourceFile["impliedNodeFormat"] | undefined): (ResolvedTypeReferenceDirective | undefined)[];
/*@internal*/ hasInvalidatedResolution?: HasInvalidatedResolution;
/**
* Returns the module resolution cache used by a provided `resolveModuleNames` implementation so that any non-name module resolution operations (eg, package.json lookup) can reuse it
*/
Expand Down
91 changes: 74 additions & 17 deletions src/testRunner/unittests/tscWatch/watchApi.ts
@@ -1,23 +1,22 @@
namespace ts.tscWatch {
describe("unittests:: tsc-watch:: watchAPI:: tsc-watch with custom module resolution", () => {
const configFileJson: any = {
compilerOptions: { module: "commonjs", resolveJsonModule: true },
files: ["index.ts"]
};
const mainFile: File = {
path: `${projectRoot}/index.ts`,
content: "import settings from './settings.json';"
};
const config: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify(configFileJson)
};
const settingsJson: File = {
path: `${projectRoot}/settings.json`,
content: JSON.stringify({ content: "Print this" })
};

it("verify that module resolution with json extension works when returned without extension", () => {
const configFileJson: any = {
compilerOptions: { module: "commonjs", resolveJsonModule: true },
files: ["index.ts"]
};
const mainFile: File = {
path: `${projectRoot}/index.ts`,
content: "import settings from './settings.json';"
};
const config: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify(configFileJson)
};
const settingsJson: File = {
path: `${projectRoot}/settings.json`,
content: JSON.stringify({ content: "Print this" })
};
const { sys, baseline, oldSnap, cb, getPrograms } = createBaseline(createWatchedSystem(
[libFile, mainFile, config, settingsJson],
{ currentDirectory: projectRoot }),
Expand Down Expand Up @@ -50,6 +49,64 @@ namespace ts.tscWatch {
watchOrSolution: watch
});
});

describe("hasInvalidatedResolution", () => {
function verifyWatch(subScenario: string, implementHasInvalidatedResolution: boolean) {
it(subScenario, () => {
const { sys, baseline, oldSnap, cb, getPrograms } = createBaseline(createWatchedSystem({
[`${projectRoot}/tsconfig.json`]: JSON.stringify({
compilerOptions: { traceResolution: true, extendedDiagnostics: true },
files: ["main.ts"]
}),
[`${projectRoot}/main.ts`]: `import { foo } from "./other";`,
[`${projectRoot}/other.d.ts`]: "export function foo(): void;",
[libFile.path]: libFile.content,
}, { currentDirectory: projectRoot }));
const host = createWatchCompilerHostOfConfigFileForBaseline({
configFileName: `${projectRoot}/tsconfig.json`,
system: sys,
cb,
});
host.resolveModuleNames = (moduleNames, containingFile, _reusedNames, _redirectedReference, options) =>
moduleNames.map(m => resolveModuleName(m, containingFile, options, host).resolvedModule);
// Invalidate resolutions only when ts file is created
if (implementHasInvalidatedResolution) host.hasInvalidatedResolution = () => sys.fileExists(`${projectRoot}/other.ts`);
const watch = createWatchProgram(host);
runWatchBaseline({
scenario: "watchApi",
subScenario,
commandLineArgs: ["--w"],
sys,
baseline,
oldSnap,
getPrograms,
changes: [
{
caption: "write other with same contents",
change: sys => sys.appendFile(`${projectRoot}/other.d.ts`, ""),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "change other file",
change: sys => sys.appendFile(`${projectRoot}/other.d.ts`, "export function bar(): void;"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "write other with same contents but write ts file",
change: sys => {
sys.appendFile(`${projectRoot}/other.d.ts`, "");
sys.writeFile(`${projectRoot}/other.ts`, "export function foo() {}");
},
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
],
watchOrSolution: watch
});
});
}
verifyWatch("host implements does not implement hasInvalidatedResolution", /*implementHasInvalidatedResolution*/ false);
verifyWatch("host implements hasInvalidatedResolution", /*implementHasInvalidatedResolution*/ true);
});
});

describe("unittests:: tsc-watch:: watchAPI:: tsc-watch expose error count to watch status reporter", () => {
Expand Down

0 comments on commit 8a4b96d

Please sign in to comment.