diff --git a/deno/ts_morph.js b/deno/ts_morph.js index 826b7c793..9ba703e16 100644 --- a/deno/ts_morph.js +++ b/deno/ts_morph.js @@ -403,7 +403,7 @@ class LazyReferenceCoordinator { if (!this.dirtySourceFiles.has(sourceFile)) return; sourceFile._referenceContainer.refresh(); - this.clearDityForSourceFile(sourceFile); + this.clearDirtyForSourceFile(sourceFile); } addDirtySourceFile(sourceFile) { this.dirtySourceFiles.add(sourceFile); @@ -411,7 +411,7 @@ class LazyReferenceCoordinator { clearDirtySourceFiles() { this.dirtySourceFiles.clear(); } - clearDityForSourceFile(sourceFile) { + clearDirtyForSourceFile(sourceFile) { this.dirtySourceFiles.delete(sourceFile); } } @@ -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() { @@ -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) { diff --git a/packages/ts-morph/src/compiler/tools/LanguageService.ts b/packages/ts-morph/src/compiler/tools/LanguageService.ts index db7f7ac94..cb78de9f2 100644 --- a/packages/ts-morph/src/compiler/tools/LanguageService.ts +++ b/packages/ts-morph/src/compiler/tools/LanguageService.ts @@ -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()); } diff --git a/packages/ts-morph/src/factories/CompilerFactory.ts b/packages/ts-morph/src/factories/CompilerFactory.ts index 898267ba4..2c29f3c9a 100644 --- a/packages/ts-morph/src/factories/CompilerFactory.ts +++ b/packages/ts-morph/src/factories/CompilerFactory.ts @@ -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; } diff --git a/packages/ts-morph/src/tests/issues/1227tests.ts b/packages/ts-morph/src/tests/issues/1227tests.ts new file mode 100644 index 000000000..0ae89912d --- /dev/null +++ b/packages/ts-morph/src/tests/issues/1227tests.ts @@ -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';"]); + }); +}); diff --git a/packages/ts-morph/src/utils/references/LazyReferenceCoordinator.ts b/packages/ts-morph/src/utils/references/LazyReferenceCoordinator.ts index a13711940..86e50bb4a 100644 --- a/packages/ts-morph/src/utils/references/LazyReferenceCoordinator.ts +++ b/packages/ts-morph/src/utils/references/LazyReferenceCoordinator.ts @@ -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) { @@ -45,7 +45,7 @@ export class LazyReferenceCoordinator { this.dirtySourceFiles.clear(); } - clearDityForSourceFile(sourceFile: SourceFile) { + clearDirtyForSourceFile(sourceFile: SourceFile) { this.dirtySourceFiles.delete(sourceFile); } }