Skip to content

Commit

Permalink
fix: regression in getReferencingNodesInOtherSourceFiles with sourc…
Browse files Browse the repository at this point in the history
…e files not marked as in the project

Closes #1227
Closes #1195 (probably)
  • Loading branch information
dsherret committed Nov 22, 2021
1 parent 760fe8c commit 7504aed
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
16 changes: 9 additions & 7 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ class LazyReferenceCoordinator {
if (!this.dirtySourceFiles.has(sourceFile))
return;
sourceFile._referenceContainer.refresh();
this.clearDityForSourceFile(sourceFile);
this.clearDirtyForSourceFile(sourceFile);
}
addDirtySourceFile(sourceFile) {
this.dirtySourceFiles.add(sourceFile);
}
clearDirtySourceFiles() {
this.dirtySourceFiles.clear();
}
clearDityForSourceFile(sourceFile) {
clearDirtyForSourceFile(sourceFile) {
this.dirtySourceFiles.delete(sourceFile);
}
}
Expand Down Expand Up @@ -17605,7 +17605,10 @@ class LanguageService {
host: this._compilerHost,
configFileParsingDiagnostics: params.configFileParsingDiagnostics,
});
this._context.compilerFactory.onSourceFileAdded(() => this._reset());
this._context.compilerFactory.onSourceFileAdded(sourceFile => {
if (sourceFile._isInProject())
this._reset();
});
this._context.compilerFactory.onSourceFileRemoved(() => this._reset());
}
get compilerObject() {
Expand Down Expand Up @@ -19233,11 +19236,10 @@ class CompilerFactory {
wasAdded = true;
return createdSourceFile;
});
if (options.markInProject) {
if (options.markInProject)
sourceFile._markAsInProject();
if (wasAdded)
this.sourceFileAddedEventContainer.fire(sourceFile);
}
if (wasAdded)
this.sourceFileAddedEventContainer.fire(sourceFile);
return sourceFile;
}
addSourceFileToCache(sourceFile) {
Expand Down
8 changes: 7 additions & 1 deletion packages/ts-morph/src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ export class LanguageService {
configFileParsingDiagnostics: params.configFileParsingDiagnostics,
});

this._context.compilerFactory.onSourceFileAdded(() => this._reset());
this._context.compilerFactory.onSourceFileAdded(sourceFile => {
// Only reset if the user is explicitly adding the source file.
// Otherwise it might have just been the TypeScript compiler
// doing some analysis and pulling in a new lib file or other file.
if (sourceFile._isInProject())
this._reset();
});
this._context.compilerFactory.onSourceFileRemoved(() => this._reset());
}

Expand Down
7 changes: 3 additions & 4 deletions packages/ts-morph/src/factories/CompilerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,11 @@ export class CompilerFactory {
return createdSourceFile;
});

if (options.markInProject) {
if (options.markInProject)
sourceFile._markAsInProject();

if (wasAdded)
this.sourceFileAddedEventContainer.fire(sourceFile);
}
if (wasAdded)
this.sourceFileAddedEventContainer.fire(sourceFile);

return sourceFile;
}
Expand Down
51 changes: 51 additions & 0 deletions packages/ts-morph/src/tests/issues/1227tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { InMemoryFileSystemHost } from "@ts-morph/common";
import { expect } from "chai";
import { Project } from "../../Project";

describe("tests for issue #1227", () => {
it("should find referencing source files", () => {
const fileSystem = new InMemoryFileSystemHost();
fileSystem.writeFileSync(
"/server.ts",
`import { run } from './mid'`,
);
fileSystem.writeFileSync(
"/mid.ts",
`import {A, B} from './constants';
export function run() {
console.log(A, B);
return null as any;
}
`,
);
fileSystem.writeFileSync(
"/constants.ts",
`export const A = 'a';
export const B = 'b';
`,
);
fileSystem.writeFileSync(
"/tsconfig.json",
`{
"exclude": [
"node_modules"
],
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "./dist",
"baseUrl": ".",
},
"files": [
"./server.ts",
]
}`,
);
const project = new Project({ fileSystem, tsConfigFilePath: "/tsconfig.json" });

const file = project.getSourceFileOrThrow("constants.ts");
expect(file.getReferencingNodesInOtherSourceFiles().map(n => n.getText()))
.to.deep.equal(["import {A, B} from './constants';"]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class LazyReferenceCoordinator {
if (!this.dirtySourceFiles.has(sourceFile))
return;
sourceFile._referenceContainer.refresh();
this.clearDityForSourceFile(sourceFile);
this.clearDirtyForSourceFile(sourceFile);
}

addDirtySourceFile(sourceFile: SourceFile) {
Expand All @@ -45,7 +45,7 @@ export class LazyReferenceCoordinator {
this.dirtySourceFiles.clear();
}

clearDityForSourceFile(sourceFile: SourceFile) {
clearDirtyForSourceFile(sourceFile: SourceFile) {
this.dirtySourceFiles.delete(sourceFile);
}
}

0 comments on commit 7504aed

Please sign in to comment.