-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (40 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const ts = require("typescript"); // 3.8.3
console.log("TypeScript", ts.version);
class ServiceHost {
constructor(name, content) {
const compilerOptions = ts.getDefaultCompilerOptions();
this.name = name;
this.content = content;
this.options = compilerOptions;
this.getDefaultLibFileName = ts.getDefaultLibFileName;
}
getCurrentDirectory() {
return process.cwd();
}
getCompilationSettings() {
return this.options;
}
getScriptFileNames() {
return [this.name];
}
getScriptVersion() {
return ts.version;
}
getScriptSnapshot() {
return ts.ScriptSnapshot.fromString(this.content);
}
}
const fileName = "testfile.ts";
const text = `import {
lstatSync,
stat,
statSync,
} from "fs";
lstatSync;
stat;
statSync;
`;
const languageService = ts.createLanguageService(new ServiceHost(fileName, text));
const fileChanges = languageService.organizeImports({ type: "file", fileName }, {})[0];
console.log("Text Changes", fileChanges.textChanges);
console.log("Failure?", fileChanges.textChanges[0].newText.includes("undefined") ? "YES" : "NO");