Skip to content

Commit 290e43b

Browse files
committed
Merge pull request #904 from Microsoft/letAndConst
Let and const support
2 parents a65496f + 51e101c commit 290e43b

File tree

142 files changed

+5766
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+5766
-274
lines changed

scripts/processDiagnosticMessages.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
interface DiagnosticDetails {
44
category: string;
55
code: number;
6+
isEarly?: boolean;
67
}
78

89
interface InputDiagnosticMessageTable {
@@ -63,8 +64,9 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
6364
' ' + convertPropertyName(nameMap[name]) +
6465
': { code: ' + diagnosticDetails.code +
6566
', category: DiagnosticCategory.' + diagnosticDetails.category +
66-
', key: "' + name.replace('"', '\\"') +
67-
'" },\r\n';
67+
', key: "' + name.replace('"', '\\"') + '"' +
68+
(diagnosticDetails.isEarly ? ', isEarly: true' : '') +
69+
' },\r\n';
6870
}
6971

7072
result += ' };\r\n}';

src/compiler/binder.ts

+81-36
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ module ts {
3131

3232
var parent: Node;
3333
var container: Declaration;
34+
var blockScopeContainer: Node;
3435
var lastContainer: Declaration;
3536
var symbolCount = 0;
3637
var Symbol = objectAllocator.getSymbolConstructor();
3738

3839
if (!file.locals) {
3940
file.locals = {};
40-
container = file;
41+
container = blockScopeContainer = file;
4142
bind(file);
4243
file.symbolCount = symbolCount;
4344
}
@@ -86,10 +87,11 @@ module ts {
8687
}
8788
// Report errors every position with duplicate declaration
8889
// Report errors on previous encountered declarations
90+
var message = symbol.flags & SymbolFlags.BlockScopedVariable ? Diagnostics.Cannot_redeclare_block_scoped_variable_0 : Diagnostics.Duplicate_identifier_0;
8991
forEach(symbol.declarations, (declaration) => {
90-
file.semanticErrors.push(createDiagnosticForNode(declaration.name, Diagnostics.Duplicate_identifier_0, getDisplayName(declaration)));
92+
file.semanticErrors.push(createDiagnosticForNode(declaration.name, message, getDisplayName(declaration)));
9193
});
92-
file.semanticErrors.push(createDiagnosticForNode(node.name, Diagnostics.Duplicate_identifier_0, getDisplayName(node)));
94+
file.semanticErrors.push(createDiagnosticForNode(node.name, message, getDisplayName(node)));
9395

9496
symbol = createSymbol(0, name);
9597
}
@@ -167,12 +169,13 @@ module ts {
167169

168170
// All container nodes are kept on a linked list in declaration order. This list is used by the getLocalNameOfContainer function
169171
// in the type checker to validate that the local name used for a container is unique.
170-
function bindChildren(node: Declaration, symbolKind: SymbolFlags) {
172+
function bindChildren(node: Declaration, symbolKind: SymbolFlags, isBlockScopeContainer: boolean) {
171173
if (symbolKind & SymbolFlags.HasLocals) {
172174
node.locals = {};
173175
}
174176
var saveParent = parent;
175177
var saveContainer = container;
178+
var savedBlockScopeContainer = blockScopeContainer;
176179
parent = node;
177180
if (symbolKind & SymbolFlags.IsContainer) {
178181
container = node;
@@ -184,12 +187,16 @@ module ts {
184187
lastContainer = container;
185188
}
186189
}
190+
if (isBlockScopeContainer) {
191+
blockScopeContainer = node;
192+
}
187193
forEachChild(node, bind);
188194
container = saveContainer;
189195
parent = saveParent;
196+
blockScopeContainer = savedBlockScopeContainer;
190197
}
191198

192-
function bindDeclaration(node: Declaration, symbolKind: SymbolFlags, symbolExcludes: SymbolFlags) {
199+
function bindDeclaration(node: Declaration, symbolKind: SymbolFlags, symbolExcludes: SymbolFlags, isBlockScopeContainer: boolean) {
193200
switch (container.kind) {
194201
case SyntaxKind.ModuleDeclaration:
195202
declareModuleMember(node, symbolKind, symbolExcludes);
@@ -225,121 +232,159 @@ module ts {
225232
declareSymbol(container.symbol.exports, container.symbol, node, symbolKind, symbolExcludes);
226233
break;
227234
}
228-
bindChildren(node, symbolKind);
235+
bindChildren(node, symbolKind, isBlockScopeContainer);
229236
}
230237

231238
function bindConstructorDeclaration(node: ConstructorDeclaration) {
232-
bindDeclaration(node, SymbolFlags.Constructor, 0);
239+
bindDeclaration(node, SymbolFlags.Constructor, 0, /*isBlockScopeContainer*/ true);
233240
forEach(node.parameters, p => {
234241
if (p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected)) {
235-
bindDeclaration(p, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
242+
bindDeclaration(p, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
236243
}
237244
});
238245
}
239246

240247
function bindModuleDeclaration(node: ModuleDeclaration) {
241248
if (node.name.kind === SyntaxKind.StringLiteral) {
242-
bindDeclaration(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
249+
bindDeclaration(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes, /*isBlockScopeContainer*/ true);
243250
}
244251
else if (isInstantiated(node)) {
245-
bindDeclaration(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
252+
bindDeclaration(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes, /*isBlockScopeContainer*/ true);
246253
}
247254
else {
248-
bindDeclaration(node, SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes);
255+
bindDeclaration(node, SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes, /*isBlockScopeContainer*/ true);
249256
}
250257
}
251258

252-
function bindAnonymousDeclaration(node: Node, symbolKind: SymbolFlags, name: string) {
259+
function bindAnonymousDeclaration(node: Node, symbolKind: SymbolFlags, name: string, isBlockScopeContainer: boolean) {
253260
var symbol = createSymbol(symbolKind, name);
254261
addDeclarationToSymbol(symbol, node, symbolKind);
255-
bindChildren(node, symbolKind);
262+
bindChildren(node, symbolKind, isBlockScopeContainer);
256263
}
257264

258265
function bindCatchVariableDeclaration(node: CatchBlock) {
259-
var symbol = createSymbol(SymbolFlags.Variable, node.variable.text || "__missing");
260-
addDeclarationToSymbol(symbol, node, SymbolFlags.Variable);
266+
var symbol = createSymbol(SymbolFlags.FunctionScopedVariable, node.variable.text || "__missing");
267+
addDeclarationToSymbol(symbol, node, SymbolFlags.FunctionScopedVariable);
261268
var saveParent = parent;
262-
parent = node;
269+
var savedBlockScopeContainer = blockScopeContainer;
270+
parent = blockScopeContainer = node;
263271
forEachChild(node, bind);
264272
parent = saveParent;
273+
blockScopeContainer = savedBlockScopeContainer;
274+
}
275+
276+
function bindBlockScopedVariableDeclaration(node: Declaration) {
277+
switch (blockScopeContainer.kind) {
278+
case SyntaxKind.ModuleDeclaration:
279+
declareModuleMember(node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
280+
break;
281+
case SyntaxKind.SourceFile:
282+
if (isExternalModule(<SourceFile>container)) {
283+
declareModuleMember(node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
284+
break;
285+
}
286+
default:
287+
if (!blockScopeContainer.locals) {
288+
blockScopeContainer.locals = {};
289+
}
290+
declareSymbol(blockScopeContainer.locals, undefined, node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
291+
}
292+
293+
bindChildren(node, SymbolFlags.BlockScopedVariable, /*isBlockScopeContainer*/ false);
265294
}
266295

267296
function bind(node: Node) {
268297
node.parent = parent;
269298
switch (node.kind) {
270299
case SyntaxKind.TypeParameter:
271-
bindDeclaration(<Declaration>node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes);
300+
bindDeclaration(<Declaration>node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes, /*isBlockScopeContainer*/ false);
272301
break;
273302
case SyntaxKind.Parameter:
274-
bindDeclaration(<Declaration>node, SymbolFlags.Variable, SymbolFlags.ParameterExcludes);
303+
bindDeclaration(<Declaration>node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes, /*isBlockScopeContainer*/ false);
275304
break;
276305
case SyntaxKind.VariableDeclaration:
277-
bindDeclaration(<Declaration>node, SymbolFlags.Variable, SymbolFlags.VariableExcludes);
306+
if (node.flags & NodeFlags.BlockScoped) {
307+
bindBlockScopedVariableDeclaration(<Declaration>node);
308+
}
309+
else {
310+
bindDeclaration(<Declaration>node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes, /*isBlockScopeContainer*/ false);
311+
}
278312
break;
279313
case SyntaxKind.Property:
280314
case SyntaxKind.PropertyAssignment:
281-
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
315+
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
282316
break;
283317
case SyntaxKind.EnumMember:
284-
bindDeclaration(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes);
318+
bindDeclaration(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes, /*isBlockScopeContainer*/ false);
285319
break;
286320
case SyntaxKind.CallSignature:
287-
bindDeclaration(<Declaration>node, SymbolFlags.CallSignature, 0);
321+
bindDeclaration(<Declaration>node, SymbolFlags.CallSignature, 0, /*isBlockScopeContainer*/ false);
288322
break;
289323
case SyntaxKind.Method:
290-
bindDeclaration(<Declaration>node, SymbolFlags.Method, SymbolFlags.MethodExcludes);
324+
bindDeclaration(<Declaration>node, SymbolFlags.Method, SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
291325
break;
292326
case SyntaxKind.ConstructSignature:
293-
bindDeclaration(<Declaration>node, SymbolFlags.ConstructSignature, 0);
327+
bindDeclaration(<Declaration>node, SymbolFlags.ConstructSignature, 0, /*isBlockScopeContainer*/ true);
294328
break;
295329
case SyntaxKind.IndexSignature:
296-
bindDeclaration(<Declaration>node, SymbolFlags.IndexSignature, 0);
330+
bindDeclaration(<Declaration>node, SymbolFlags.IndexSignature, 0, /*isBlockScopeContainer*/ false);
297331
break;
298332
case SyntaxKind.FunctionDeclaration:
299-
bindDeclaration(<Declaration>node, SymbolFlags.Function, SymbolFlags.FunctionExcludes);
333+
bindDeclaration(<Declaration>node, SymbolFlags.Function, SymbolFlags.FunctionExcludes, /*isBlockScopeContainer*/ true);
300334
break;
301335
case SyntaxKind.Constructor:
302336
bindConstructorDeclaration(<ConstructorDeclaration>node);
303337
break;
304338
case SyntaxKind.GetAccessor:
305-
bindDeclaration(<Declaration>node, SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes);
339+
bindDeclaration(<Declaration>node, SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes, /*isBlockScopeContainer*/ true);
306340
break;
307341
case SyntaxKind.SetAccessor:
308-
bindDeclaration(<Declaration>node, SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes);
342+
bindDeclaration(<Declaration>node, SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes, /*isBlockScopeContainer*/ true);
309343
break;
310344
case SyntaxKind.TypeLiteral:
311-
bindAnonymousDeclaration(node, SymbolFlags.TypeLiteral, "__type");
345+
bindAnonymousDeclaration(node, SymbolFlags.TypeLiteral, "__type", /*isBlockScopeContainer*/ false);
312346
break;
313347
case SyntaxKind.ObjectLiteral:
314-
bindAnonymousDeclaration(node, SymbolFlags.ObjectLiteral, "__object");
348+
bindAnonymousDeclaration(node, SymbolFlags.ObjectLiteral, "__object", /*isBlockScopeContainer*/ false);
315349
break;
316350
case SyntaxKind.FunctionExpression:
317351
case SyntaxKind.ArrowFunction:
318-
bindAnonymousDeclaration(node, SymbolFlags.Function, "__function");
352+
bindAnonymousDeclaration(node, SymbolFlags.Function, "__function", /*isBlockScopeContainer*/ true);
319353
break;
320354
case SyntaxKind.CatchBlock:
321355
bindCatchVariableDeclaration(<CatchBlock>node);
322356
break;
323357
case SyntaxKind.ClassDeclaration:
324-
bindDeclaration(<Declaration>node, SymbolFlags.Class, SymbolFlags.ClassExcludes);
358+
bindDeclaration(<Declaration>node, SymbolFlags.Class, SymbolFlags.ClassExcludes, /*isBlockScopeContainer*/ false);
325359
break;
326360
case SyntaxKind.InterfaceDeclaration:
327-
bindDeclaration(<Declaration>node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes);
361+
bindDeclaration(<Declaration>node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes, /*isBlockScopeContainer*/ false);
328362
break;
329363
case SyntaxKind.EnumDeclaration:
330-
bindDeclaration(<Declaration>node, SymbolFlags.Enum, SymbolFlags.EnumExcludes);
364+
bindDeclaration(<Declaration>node, SymbolFlags.Enum, SymbolFlags.EnumExcludes, /*isBlockScopeContainer*/ false);
331365
break;
332366
case SyntaxKind.ModuleDeclaration:
333367
bindModuleDeclaration(<ModuleDeclaration>node);
334368
break;
335369
case SyntaxKind.ImportDeclaration:
336-
bindDeclaration(<Declaration>node, SymbolFlags.Import, SymbolFlags.ImportExcludes);
370+
bindDeclaration(<Declaration>node, SymbolFlags.Import, SymbolFlags.ImportExcludes, /*isBlockScopeContainer*/ false);
337371
break;
338372
case SyntaxKind.SourceFile:
339373
if (isExternalModule(<SourceFile>node)) {
340-
bindAnonymousDeclaration(node, SymbolFlags.ValueModule, '"' + removeFileExtension((<SourceFile>node).filename) + '"');
374+
bindAnonymousDeclaration(node, SymbolFlags.ValueModule, '"' + removeFileExtension((<SourceFile>node).filename) + '"', /*isBlockScopeContainer*/ true);
341375
break;
342376
}
377+
378+
case SyntaxKind.Block:
379+
case SyntaxKind.TryBlock:
380+
case SyntaxKind.CatchBlock:
381+
case SyntaxKind.FinallyBlock:
382+
case SyntaxKind.ForStatement:
383+
case SyntaxKind.ForInStatement:
384+
case SyntaxKind.SwitchStatement:
385+
bindChildren(node, 0 , true);
386+
break;
387+
343388
default:
344389
var saveParent = parent;
345390
parent = node;

0 commit comments

Comments
 (0)