forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsiface.ts
More file actions
142 lines (132 loc) · 4.15 KB
/
Copy pathtsiface.ts
File metadata and controls
142 lines (132 loc) · 4.15 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as ts from "typescript"
import { Host } from "./format"
import { prelude } from "./prelude"
class MyHost implements ts.CompilerHost {
constructor(public fileText: Record<string, string>) {}
getSourceFile(
fileName: string,
languageVersion: ts.ScriptTarget,
onError?: (message: string) => void,
shouldCreateNewSourceFile?: boolean
): ts.SourceFile {
let text = ""
if (this.fileText.hasOwnProperty(fileName)) {
text = this.fileText[fileName]
} else {
if (onError) onError("File not found: " + fileName)
}
if (text == null) {
onError("File not found: " + fileName)
text = ""
}
const setParentNodes = true
return ts.createSourceFile(
fileName,
text,
languageVersion,
setParentNodes
)
}
writeFile: ts.WriteFileCallback
getDefaultLibFileName(options: ts.CompilerOptions): string {
return "corelib.d.ts"
}
getDefaultLibLocation?(): string {
return "."
}
getCurrentDirectory(): string {
return "."
}
getCanonicalFileName(fileName: string): string {
return fileName
}
useCaseSensitiveFileNames(): boolean {
return true
}
getNewLine(): string {
return "\n"
}
fileExists(fileName: string): boolean {
return this.fileText.hasOwnProperty(fileName)
}
readFile(fileName: string): string {
if (!this.fileExists(fileName)) return undefined
return this.fileText[fileName]
}
trace?(s: string): void {
console.log(s)
}
}
export function getProgramDiagnostics(program: ts.Program): ts.Diagnostic[] {
let diagnostics = program.getConfigFileParsingDiagnostics()
addDiags(() => program.getOptionsDiagnostics())
addDiags(() => program.getSyntacticDiagnostics())
addDiags(() => program.getGlobalDiagnostics())
addDiags(() => program.getSemanticDiagnostics())
return diagnostics.slice(0)
function addDiags(f: () => readonly ts.Diagnostic[]) {
if (!diagnostics.some(d => d.category == ts.DiagnosticCategory.Error))
diagnostics = diagnostics.concat(f())
}
}
export function buildAST(host: Host, source: string) {
const mainFn = host?.mainFileName() || "main.ts"
const tsHost = new MyHost({
[mainFn]: source,
...prelude,
})
tsHost.writeFile = (fileName, data, writeBOM, onError) => {
host.write(fileName, data)
}
const tsOptions: ts.CompilerOptions = {
allowJs: true,
allowUnreachableCode: true,
allowUnusedLabels: true,
alwaysStrict: false,
checkJs: true,
declaration: false,
experimentalDecorators: true,
forceConsistentCasingInFileNames: true,
// lib: string[];
module: ts.ModuleKind.ES2022,
newLine: ts.NewLineKind.LineFeed,
noEmit: true,
noFallthroughCasesInSwitch: true,
noImplicitAny: true,
noImplicitReturns: true,
noImplicitThis: true,
// noStrictGenericChecks: true,
noPropertyAccessFromIndexSignature: true,
// noLib: true,
noImplicitOverride: true,
// skipLibCheck: true,
// skipDefaultLibCheck: true,
sourceMap: false,
strict: true,
// strictFunctionTypes: false,
// strictBindCallApply: false,
strictNullChecks: false,
// strictPropertyInitialization: false,
// suppressExcessPropertyErrors: true,
// suppressImplicitAnyIndexErrors: true,
target: ts.ScriptTarget.ES2022,
useUnknownInCatchVariables: true,
// types?: string[];
}
const program = ts.createProgram({
rootNames: Object.keys(prelude).concat([mainFn]),
options: tsOptions,
host: tsHost,
})
return program
}
const diagHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => ".",
getCanonicalFileName: fn => fn,
getNewLine: () => "\n",
}
export function formatDiagnostics(
diagnostics: readonly ts.Diagnostic[]
): string {
return ts.formatDiagnostics(diagnostics, diagHost).trim()
}