Repository navigation
Expand file tree
/
Copy pathtypescript.ts
More file actions
195 lines (179 loc) · 8.84 KB
/
Copy pathtypescript.ts
File metadata and controls
195 lines (179 loc) · 8.84 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* The relevant parts of TypeScript.
*
* For all exports, see: https://github.com/dcodeIO/AssemblyScript/blob/master/src/typescript.ts
*
* @module assemblyscript/typescript
* @preferred
*/ /** */
import * as path from "path";
import * as ts from "../lib/typescript/build";
import * as library from "./library";
// With a little help of "Find all references" the following list aims to provide an answer to
// the interesting question "Which parts of TypeScript do we actually use, and where?".
export import ArrayTypeNode = ts.ArrayTypeNode;
export import AsExpression = ts.AsExpression;
export import BinaryExpression = ts.BinaryExpression;
export import Block = ts.Block;
export import BreakStatement = ts.BreakStatement;
export import CallExpression = ts.CallExpression;
export import ClassDeclaration = ts.ClassDeclaration;
import CompilerHost = ts.CompilerHost;
import CompilerOptions = ts.CompilerOptions;
export import ConditionalExpression = ts.ConditionalExpression;
export import ConstructorDeclaration = ts.ConstructorDeclaration;
export import ContinueStatement = ts.ContinueStatement;
export import DiagnosticCategory = ts.DiagnosticCategory;
export import DiagnosticCollection = ts.DiagnosticCollection;
export import DiagnosticMessage = ts.DiagnosticMessage;
export import Diagnostic = ts.Diagnostic;
export import DoStatement = ts.DoStatement;
export import ElementAccessExpression = ts.ElementAccessExpression;
export import EnumDeclaration = ts.EnumDeclaration;
export import EnumMember = ts.EnumMember;
export import EntityName = ts.EntityName;
export import ExpressionStatement = ts.ExpressionStatement;
export import Expression = ts.Expression;
import FormatDiagnosticsHost = ts.FormatDiagnosticsHost;
export import ForStatement = ts.ForStatement;
export import FunctionBody = ts.FunctionBody;
export import FunctionLikeDeclaration = ts.FunctionLikeDeclaration;
export import FunctionDeclaration = ts.FunctionDeclaration;
export import Identifier = ts.Identifier;
export import IfStatement = ts.IfStatement;
export import LiteralExpression = ts.LiteralExpression;
export import MethodDeclaration = ts.MethodDeclaration;
export import ModifierFlags = ts.ModifierFlags;
import ModuleKind = ts.ModuleKind;
export import NewExpression = ts.NewExpression;
export import NodeArray = ts.NodeArray;
export import NodeFlags = ts.NodeFlags;
export import Node = ts.Node;
export import ParameterDeclaration = ts.ParameterDeclaration;
export import ParenthesizedExpression = ts.ParenthesizedExpression;
export import PostfixUnaryExpression = ts.PostfixUnaryExpression;
export import PrefixUnaryExpression = ts.PrefixUnaryExpression;
export import Program = ts.Program;
export import PropertyAccessExpression = ts.PropertyAccessExpression;
export import PropertyDeclaration = ts.PropertyDeclaration;
export import Signature = ts.Signature;
export import TypeAliasDeclaration = ts.TypeAliasDeclaration;
export import TypeChecker = ts.TypeChecker;
export import TypeNode = ts.TypeNode;
export import TypeParameterDeclaration = ts.TypeParameterDeclaration;
export import TypeReferenceNode = ts.TypeReferenceNode;
export import TypeReference = ts.TypeReference;
export import Type = ts.Type;
export import VariableDeclaration = ts.VariableDeclaration;
export import VariableDeclarationList = ts.VariableDeclarationList;
export import VariableStatement = ts.VariableStatement;
export import ReturnStatement = ts.ReturnStatement;
import ResolvedModule = ts.ResolvedModule;
export import ScriptTarget = ts.ScriptTarget;
export import SourceFile = ts.SourceFile;
export import Statement = ts.Statement;
export import SwitchStatement = ts.SwitchStatement;
export import Symbol = ts.Symbol;
export import SyntaxKind = ts.SyntaxKind;
export import WhileStatement = ts.WhileStatement;
export import getPreEmitDiagnostics = ts.getPreEmitDiagnostics;
export import getSourceFileOfNode = ts.getSourceFileOfNode;
export import getTextOfNode = ts.getTextOfNode;
export import createDiagnosticCollection = ts.createDiagnosticCollection;
export import createDiagnosticForNode = ts.createDiagnosticForNode;
import createGetCanonicalFileName = ts.createGetCanonicalFileName;
export import createProgram = ts.createProgram;
export import createSourceFile = ts.createSourceFile;
import resolveModuleName = ts.resolveModuleName;
import sys = ts.sys;
// generated diagnostic extensions
export { DiagnosticsEx } from "./typescript/diagnosticMessages.generated";
/** Default format diagnostics host for convenience. */
export const defaultFormatDiagnosticsHost: FormatDiagnosticsHost = {
getCurrentDirectory: () => sys.getCurrentDirectory(),
getNewLine: () => sys.newLine,
getCanonicalFileName: createGetCanonicalFileName(sys.useCaseSensitiveFileNames)
};
/** Default compiler options for AssemblyScript compilation. */
export const defaultCompilerOptions = <CompilerOptions>{
target: ScriptTarget.Latest,
module: ModuleKind.None,
noLib: true,
experimentalDecorators: true,
types: []
};
/** Creates an AssemblyScript-compatible compiler host. */
export function createCompilerHost(moduleSearchLocations: string[], entryFileSource?: string, entryFileName: string = "module.ts"): CompilerHost {
const files: { [key: string]: SourceFile } = {};
if (typeof entryFileSource === "string")
files[entryFileName] = createSourceFile(entryFileName, <string>entryFileSource, ScriptTarget.Latest);
Object.keys(library.files).forEach(name => {
files[name] = createSourceFile(name, library.files[name], ScriptTarget.Latest);
});
return {
getSourceFile,
getDefaultLibFileName: () => "assembly.d.ts",
writeFile: (filename: string, content: string) => sys.writeFile(filename, content),
getCurrentDirectory: () => sys.getCurrentDirectory(),
getDirectories: (path: string) => sys.getDirectories(path),
getCanonicalFileName: fileName => sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
getNewLine: () => sys.newLine,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
fileExists,
readFile,
resolveModuleNames
};
function fileExists(fileName: string): boolean {
return !!files[fileName] || sys.fileExists(fileName);
}
function readFile(fileName: string): string {
return files[fileName] ? files[fileName].text : sys.readFile(fileName);
}
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
if (files[fileName])
return files[fileName];
const sourceText = sys.readFile(fileName);
if (sourceText === undefined && onError)
onError("file not found: " + fileName);
return files[fileName] = createSourceFile(fileName, sourceText || "", languageVersion);
}
function resolveModuleNames(moduleNames: string[], containingFile: string): ResolvedModule[] {
return <ResolvedModule[]>moduleNames.map(moduleName => {
// try to use standard resolution
const result = resolveModuleName(moduleName, containingFile, defaultCompilerOptions, { fileExists, readFile });
if (result.resolvedModule)
return result.resolvedModule;
// check fallback locations, for simplicity assume that module at location should be represented by '.d.ts' file
for (const location of moduleSearchLocations) {
const modulePath = path.join(location, moduleName + ".d.ts");
if (fileExists(modulePath))
return { resolvedFileName: modulePath };
}
return undefined;
});
}
}
/** Formats a diagnostic message in plain text. */
export function formatDiagnostics(diagnostics: Diagnostic[], host?: FormatDiagnosticsHost) {
return /* override */ ts.formatDiagnostics(diagnostics, host || defaultFormatDiagnosticsHost);
}
/** Formats a diagnostic message with terminal colors and source context. */
export function formatDiagnosticsWithColorAndContext(diagnostics: Diagnostic[], host?: FormatDiagnosticsHost) {
return /* override */ ts.formatDiagnosticsWithColorAndContext(diagnostics, host || defaultFormatDiagnosticsHost);
}
/** Prints a diagnostic message to console. */
export function printDiagnostic(diagnostic: Diagnostic): void {
if (typeof process !== "undefined" && process && process.stderr) {
if (diagnostic.category === DiagnosticCategory.Message)
process.stderr.write(formatDiagnostics([ diagnostic ], defaultFormatDiagnosticsHost));
else
process.stderr.write(formatDiagnosticsWithColorAndContext([ diagnostic ], defaultFormatDiagnosticsHost) + "\n");
} else {
if (diagnostic.category === DiagnosticCategory.Message)
(console.info || console.log)(formatDiagnostics([ diagnostic ], defaultFormatDiagnosticsHost));
else if (diagnostic.category === DiagnosticCategory.Warning)
(console.warn || console.log)(formatDiagnostics([ diagnostic ], defaultFormatDiagnosticsHost));
else
(console.error || console.log)(formatDiagnostics([ diagnostic ], defaultFormatDiagnosticsHost));
}
}