forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.ts
More file actions
198 lines (158 loc) · 8.99 KB
/
Copy pathdocument.ts
File metadata and controls
198 lines (158 loc) · 8.99 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
196
197
198
///<reference path='references.ts' />
module TypeScript {
export interface IncrementalParse {
(oldSyntaxTree: SyntaxTree, textChangeRange: TextChangeRange, newText: ISimpleText): SyntaxTree
}
export class Document implements IASTForDeclMap {
private _bloomFilter: BloomFilter = null;
private _declASTMap: ISyntaxElement[] = [];
private _astDeclMap: PullDecl[] = [];
// By default, our Document class doesn't support incremental update of its contents.
// However, we enable other layers (like teh services layer) to inject the capability
// into us by setting this function.
public static incrementalParse: IncrementalParse = null;
constructor(private compilationSettings: ImmutableCompilationSettings,
public fileName: string,
public referencedFiles: string[],
public scriptSnapshot: IScriptSnapshot,
public byteOrderMark: ByteOrderMark,
public version: string,
public isOpen: boolean,
private _syntaxTree: SyntaxTree,
private _topLevelDecl: PullDecl) {
}
public isDeclareFile(): boolean {
return isDTSFile(this.fileName);
}
public sourceUnit(): SourceUnitSyntax {
// If we don't have a script, create one from our parse tree.
return this.syntaxTree().sourceUnit();
}
public diagnostics(): Diagnostic[] {
return this.syntaxTree().diagnostics();
}
public lineMap(): LineMap {
return this.syntaxTree().lineMap();
}
public syntaxTree(): SyntaxTree {
if (!this._syntaxTree) {
var start = new Date().getTime();
this._syntaxTree = Parser.parse(
this.fileName, SimpleText.fromScriptSnapshot(this.scriptSnapshot), this.compilationSettings.codeGenTarget(), this.isDeclareFile());
var time = new Date().getTime() - start;
TypeScript.syntaxTreeParseTime += time;
}
return this._syntaxTree;
}
public bloomFilter(): BloomFilter {
if (!this._bloomFilter) {
var identifiers = createIntrinsicsObject<boolean>();
var pre = function (cur: TypeScript.ISyntaxElement) {
if (ASTHelpers.isValidAstNode(cur)) {
if (cur.kind() === SyntaxKind.IdentifierName) {
var nodeText = tokenValueText((<TypeScript.ISyntaxToken>cur));
identifiers[nodeText] = true;
}
}
};
TypeScript.getAstWalkerFactory().simpleWalk(this.sourceUnit(), pre, null, identifiers);
var identifierCount = 0;
for (var name in identifiers) {
if (identifiers[name]) {
identifierCount++;
}
}
this._bloomFilter = new BloomFilter(identifierCount);
this._bloomFilter.addKeys(identifiers);
}
return this._bloomFilter;
}
// Returns true if this file should get emitted into its own unique output file.
// Otherwise, it should be written into a single output file along with the rest of hte
// documents in the compilation.
public emitToOwnOutputFile(): boolean {
// If we haven't specified an output file in our settings, then we're definitely
// emitting to our own file. Also, if we're an external module, then we're
// definitely emitting to our own file.
return !this.compilationSettings.outFileOption() || this.syntaxTree().isExternalModule();
}
public update(scriptSnapshot: IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TextChangeRange): Document {
// See if we are currently holding onto a syntax tree. We may not be because we're
// either a closed file, or we've just been lazy and haven't had to create the syntax
// tree yet. Access the field instead of the method so we don't accidently realize
// the old syntax tree.
var oldSyntaxTree = this._syntaxTree;
if (textChangeRange !== null && Debug.shouldAssert(AssertionLevel.Normal)) {
var oldText = this.scriptSnapshot;
var newText = scriptSnapshot;
TypeScript.Debug.assert((oldText.getLength() - textChangeRange.span().length() + textChangeRange.newLength()) === newText.getLength(),
"TextChangeRange data was not consistent with before/after lengths of text.");
if (Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
var oldTextPrefix = oldText.getText(0, textChangeRange.span().start());
var newTextPrefix = newText.getText(0, textChangeRange.span().start());
TypeScript.Debug.assert(oldTextPrefix === newTextPrefix, "Prefixes of texts were not consistent.");
var oldTextSuffix = oldText.getText(textChangeRange.span().end(), oldText.getLength());
var newTextSuffix = newText.getText(textChangeRange.newSpan().end(), newText.getLength());
TypeScript.Debug.assert(oldTextSuffix === newTextSuffix, "Suffixes of texts were not consistent.");
}
}
var text = SimpleText.fromScriptSnapshot(scriptSnapshot);
// If we don't have a text change, or we don't have an old syntax tree, then do a full
// parse. Otherwise, do an incremental parse.
var newSyntaxTree = textChangeRange === null || oldSyntaxTree === null || Document.incrementalParse === null
? TypeScript.Parser.parse(this.fileName, text, this.compilationSettings.codeGenTarget(), TypeScript.isDTSFile(this.fileName))
: Document.incrementalParse(oldSyntaxTree, textChangeRange, text);
return new Document(this.compilationSettings, this.fileName, this.referencedFiles, scriptSnapshot, this.byteOrderMark, version, isOpen, newSyntaxTree, /*topLevelDecl:*/ null);
}
public static create(compilationSettings: ImmutableCompilationSettings, fileName: string, scriptSnapshot: IScriptSnapshot, byteOrderMark: ByteOrderMark, version: string, isOpen: boolean, referencedFiles: string[]): Document {
return new Document(compilationSettings, fileName, referencedFiles, scriptSnapshot, byteOrderMark, version, isOpen, /*syntaxTree:*/ null, /*topLevelDecl:*/ null);
}
public topLevelDecl(): PullDecl {
if (this._topLevelDecl === null) {
this._topLevelDecl = DeclarationCreator.create(this, this.compilationSettings);
}
return this._topLevelDecl;
}
public _getDeclForAST(ast: ISyntaxElement): PullDecl {
// Ensure we actually have created all our decls before we try to find a mathcing decl
// for this ast.
this.topLevelDecl();
return this._astDeclMap[syntaxID(ast)];
}
public getEnclosingDecl(ast: ISyntaxElement): PullDecl {
if (ast.kind() === SyntaxKind.SourceUnit) {
return this._getDeclForAST(ast);
}
// First, walk up the ISyntaxElement, looking for a decl corresponding to that ISyntaxElement node.
ast = ast.parent;
var decl: PullDecl = null;
while (ast) {
//if (ast.kind === SyntaxKind.ModuleDeclaration) {
// var moduleDecl = <ModuleDeclarationSyntax>ast;
// decl = this._getDeclForAST(<ISyntaxElement>moduleDecl.stringLiteral || ArrayUtilities.last(getModuleNames(moduleDecl.name)));
//}
//else {
decl = this._getDeclForAST(ast);
//}
if (decl) {
break;
}
ast = ast.parent;
}
// Now, skip over certain decls. The resolver never considers these the 'enclosing'
// decl for an ISyntaxElement node.
return decl._getEnclosingDeclFromParentDecl();
}
public _setDeclForAST(ast: ISyntaxElement, decl: PullDecl): void {
Debug.assert(decl.fileName() === this.fileName);
this._astDeclMap[syntaxID(ast)] = decl;
}
public _getASTForDecl(decl: PullDecl): ISyntaxElement {
return this._declASTMap[decl.declID];
}
public _setASTForDecl(decl: PullDecl, ast: ISyntaxElement): void {
Debug.assert(decl.fileName() === this.fileName);
this._declASTMap[decl.declID] = ast;
}
}
}