Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 96fba97

Browse files
committed
feat: use TS's preProcessFile to extract deps, refs #168
1 parent fa11398 commit 96fba97

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"sinon": "^1.17.4",
5757
"temp": "^0.8.3",
5858
"tslint": "3.11.0-dev.0",
59-
"typescript": "^1.9.0-dev.20160619-1.0",
59+
"typescript": "^1.9.0-dev.20160620-1.0",
6060
"webpack": "2.1.0-beta.4"
6161
}
6262
}

src/checker-runtime.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,18 @@ export class Host implements ts.LanguageServiceHost {
8484
}
8585

8686
getScriptVersion(fileName: string) {
87-
if (env.files[fileName]) {
88-
return env.files[fileName].version.toString();
87+
let fileName_ = path.normalize(fileName);
88+
if (env.files[fileName_]) {
89+
return env.files[fileName_].version.toString();
8990
}
9091
}
9192

9293
getScriptSnapshot(fileName: string) {
9394
let fileName_ = path.normalize(fileName);
9495
let file = env.files[fileName_];
96+
if (!file) {
97+
throw new Error(`Requested file is unknown: ${ fileName_ }`);
98+
}
9599
return env.compiler.ScriptSnapshot.fromString(file.text);
96100
}
97101

src/deps.ts

+6-39
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ export function isTypeDeclaration(fileName: string): boolean {
1919
return /\.d.ts$/.test(fileName);
2020
}
2121

22-
function isImportOrExportDeclaration(node: ts.Node) {
23-
return !!(<any>node).moduleSpecifier;
24-
}
25-
26-
function isImportEqualsDeclaration(node: ts.Node) {
27-
return !!(<any>node).moduleReference && (<any>node).moduleReference.hasOwnProperty('expression');
28-
}
29-
3022
export class FileAnalyzer {
3123
dependencies = new DependencyManager();
3224
validFiles = new ValidFilesManager();
@@ -74,30 +66,13 @@ export class FileAnalyzer {
7466

7567
findImportDeclarations(fileName: string): ts.ResolvedModule[] {
7668
let sourceFile = this.state.getSourceFile(fileName);
77-
let ts = this.state.ts;
78-
79-
let imports = [];
80-
let visit = (node: ts.Node) => {
81-
if (isImportEqualsDeclaration(node)) {
82-
// we need this check to ensure that we have an external import
83-
let importPath = (<any>node).moduleReference.expression.text;
84-
imports.push(importPath);
85-
} else if (isImportOrExportDeclaration(node)) {
86-
let importPath = (<any>node).moduleSpecifier.text;
87-
imports.push(importPath);
88-
}
89-
90-
ts.forEachChild(node, visit);
91-
};
92-
93-
imports.push.apply(imports, sourceFile.referencedFiles.map(file => file.fileName));
94-
ts.forEachChild(sourceFile, visit);
95-
96-
let resolvedImports = imports.map((importPath) => {
97-
return this.resolve(fileName, importPath);
98-
});
69+
let isJavaScript = sourceFile.flags & this.state.ts.NodeFlags.JavaScriptFile;
70+
let info = this.state.ts.preProcessFile(sourceFile.text, true, !!isJavaScript);
9971

100-
return resolvedImports.filter(Boolean);
72+
return info.importedFiles
73+
.map(file => file.fileName)
74+
.map(depName => this.resolve(fileName, depName))
75+
.filter(Boolean);
10176
}
10277

10378
resolve(fileName: string, depName: string): ts.ResolvedModule {
@@ -125,14 +100,6 @@ export class FileAnalyzer {
125100
this.state.fileAnalyzer.dependencies.addResolution(fileName, depName, resolvedModule);
126101
}
127102

128-
if (this.state.compilerConfig.options.traceResolution) {
129-
console.log(
130-
'Resolve', depName, '\n',
131-
'from', fileName,'\n',
132-
'resolved', resolvedModule && resolvedModule.resolvedFileName, '\n',
133-
'failedLookupLocations', resolution.failedLookupLocations, '\n\n');
134-
}
135-
136103
return resolvedModule;
137104
}
138105
}

src/host.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ export class Host implements ts.LanguageServiceHost {
4040
}
4141

4242
getScriptVersion(fileName: string) {
43-
if (this.state.getFile(fileName)) {
44-
return this.state.getFile(fileName).version.toString();
43+
let fileName_ = path.normalize(fileName);
44+
if (this.state.getFile(fileName_)) {
45+
return this.state.getFile(fileName_).version.toString();
4546
}
4647
}
4748

4849
getScriptSnapshot(fileName) {
49-
let file = this.state.getFile(fileName);
50+
let fileName_ = path.normalize(fileName);
51+
let file = this.state.getFile(fileName_);
52+
if (!file) {
53+
throw new Error(`Requested file is unknown: ${ fileName_ }`);
54+
}
5055
return this.state.ts.ScriptSnapshot.fromString(file.text);
5156
}
5257

@@ -67,9 +72,11 @@ export class Host implements ts.LanguageServiceHost {
6772
}
6873

6974
resolveModuleNames(moduleNames: string[], containingFile: string) {
70-
return moduleNames.map(moduleName => {
75+
let resolutions = moduleNames.map(moduleName => {
7176
return this.state.fileAnalyzer.dependencies.getResolution(containingFile, moduleName);
7277
});
78+
79+
return resolutions;
7380
}
7481

7582
getDefaultLibLocation(): string {

src/instance.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ export function ensureInstance(webpack: IWebPack, query: QueryOptions, instanceN
140140
let compilerPath = query.compiler || 'typescript';
141141

142142
let tsImpl: typeof ts;
143+
let tsImplPath: string;
143144
try {
144-
tsImpl = require(compilerPath);
145+
tsImplPath = require.resolve(compilerPath);
146+
tsImpl = require(tsImplPath);
145147
} catch (e) {
146148
console.error(e);
147149
console.error(COMPILER_ERROR);
@@ -155,8 +157,6 @@ export function ensureInstance(webpack: IWebPack, query: QueryOptions, instanceN
155157

156158
let { compilerConfig, loaderConfig } = readConfigFile(process.cwd(), query, tsImpl);
157159

158-
console.log(compilerConfig, loaderConfig);
159-
160160
applyDefaults(compilerConfig, loaderConfig);
161161
let babelImpl = setupBabel(loaderConfig);
162162
let cacheIdentifier = setupCache(loaderConfig, tsImpl, webpack, babelImpl);

0 commit comments

Comments
 (0)