Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upLet and const support #904
Conversation
This comment has been minimized.
This comment has been minimized.
|
There should only be one declaration, right? |
This comment has been minimized.
This comment has been minimized.
|
yes. thanks. |
This comment has been minimized.
This comment has been minimized.
|
done. |
| @@ -2827,7 +2848,7 @@ module ts { | |||
| parseExpected(SyntaxKind.CaseKeyword); | |||
| node.expression = parseExpression(); | |||
| parseExpected(SyntaxKind.ColonToken); | |||
| node.statements = parseList(ParsingContext.SwitchClauseStatements, /*checkForStrictMode*/ false, parseStatement); | |||
| node.statements = parseList(ParsingContext.SwitchClauseStatements, /*checkForStrictMode*/ false, parseStatementAllowingLetDeclaration); | |||
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 16, 2014
Contributor
i thought we would not allow 'let' here as we're not in a block scope. or do you check for that later?
This comment has been minimized.
This comment has been minimized.
mhegazy
Oct 17, 2014
Author
i think you are correct. the ES6 spec makes it clear that lexical scopes do not include switch, try or finally blocks, section 8.1:
A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.
I will follow up to see if this is intentional or just an error in the spec and update this accordingly.
This comment has been minimized.
This comment has been minimized.
JsonFreeman
Oct 17, 2014
Contributor
They say "such as", which I normally interpret as "including but not limited to".
This comment has been minimized.
This comment has been minimized.
mhegazy
Oct 17, 2014
Author
yeah. checked with @bterlson and he confirmed that that is not the intention. so keeping Switch, try and finally blocks as valid lexical scopes
| @@ -2953,7 +2974,7 @@ module ts { | |||
| return isIdentifier() && lookAhead(() => nextToken() === SyntaxKind.ColonToken); | |||
| } | |||
|
|
|||
| function parseLabelledStatement(): LabeledStatement { | |||
| function parseLabelledStatement(allowLetDeclarations: boolean): LabeledStatement { | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| @@ -790,7 +793,8 @@ module ts { | |||
| Signature = CallSignature | ConstructSignature | IndexSignature, | |||
|
|
|||
| ParameterExcludes = Value, | |||
| VariableExcludes = Value & ~Variable, | |||
| VariableExcludes = (Value | BlockScoped) & ~Variable, | |||
This comment has been minimized.
This comment has been minimized.
CyrusNajmabadi
Oct 16, 2014
Contributor
can you comment what thsi means. i.e. give an intutive explanation like "vars don't exclude vars with teh same name. but blah blah blah."
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mhegazy
Oct 17, 2014
Author
cause blockScope is not a thing by itself. so you can be a variable and blockscopes, may be you can be a class and bockscopped in the future.
This comment has been minimized.
This comment has been minimized.
JsonFreeman
Oct 17, 2014
Contributor
This looks like vars and lets can coexist though. Did you mean (Value & ~Variable) | BlockScoped?
This comment has been minimized.
This comment has been minimized.
| @@ -685,6 +685,8 @@ module Harness { | |||
| options.target = ts.ScriptTarget.ES3; | |||
| } else if (setting.value.toLowerCase() === 'es5') { | |||
| options.target = ts.ScriptTarget.ES5; | |||
| } else if (setting.value.toLowerCase() === 'es6') { | |||
| options.target = ts.ScriptTarget.ES6; | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
What about this: function outer() {
const x = 0;
function inner() {
var x = 0;
}
}Looks to me like this will be reported as an error, but I'm not sure it should be. You probably should limit the lookup to just the current function, but I haven't carefully checked the ES6 spec. |
This comment has been minimized.
This comment has been minimized.
|
you are absolutely right. will fix it. thanks. |
This comment has been minimized.
This comment has been minimized.
|
Awesome that we now officially have an ES6 target. We'll need to update our class and arrow function emit to not rewrite these constructs in ES6 mode. Once this goes in we should make sure to file bugs to that effect. |
This comment has been minimized.
This comment has been minimized.
|
yes. that is the intention. |
This comment has been minimized.
This comment has been minimized.
|
I like that we're now doing this. So, if for example I declare my own Should we also be doing this for declarations that are merged during local binding? |
This comment has been minimized.
This comment has been minimized.
|
yes. @yuit checked in a fix for the local declarations. this is to ensure we are getting the same behavior while merging across files. so errors will be reported in lib.d.ts for a module "Array" and for redefining a property "length" in interface "Array" |
This comment has been minimized.
This comment has been minimized.
|
So ES6 permits a const declaration in a for statement, but it is still an error to re-assign the const, right? Not sure why you'd ever want to do that, but if that's what ES6 says then so be it. |
This comment has been minimized.
This comment has been minimized.
|
the only benefit is getting a fresh binding for every iteration, so it can be captured, but not changed later on. I could not come up with a meaningful use case that requires a new binding and read-only that is not identical to:
|
|
Overall looks great! |
This comment has been minimized.
This comment has been minimized.
|
The casing is not consistent between these two messages (6015 and 6047) |
This comment has been minimized.
This comment has been minimized.
|
I would collapse this case with the case above. They have exactly the same code |
This comment has been minimized.
This comment has been minimized.
|
Does this flag ever matter? A let/const would make the module instantiated, no? |
This comment has been minimized.
This comment has been minimized.
|
"Block" |
This comment has been minimized.
This comment has been minimized.
|
Why errorLocation and not location? |
This comment has been minimized.
This comment has been minimized.
|
location changes later on. errorlocation is the one we got initally |
This comment has been minimized.
This comment has been minimized.
|
Ok, please comment that. |
This comment has been minimized.
This comment has been minimized.
|
I prefer: if (declarationSourceFile === referenceSourceFile) {
if (declaration.pos > errorLocation.pos) {
...
}
}
else if (compilerOptions.out) {
...
}So you don't have to worry about the compilerOptions.out case if it was in the same file. |
This comment has been minimized.
This comment has been minimized.
KirillGrishin
commented on tests/baselines/reference/es6-amd.js in 873c1df
Mar 11, 2015
|
Do I understand correctly that currently (TS1.4.1) even with target ES6 the output will not be ES6 classes? |
This comment has been minimized.
This comment has been minimized.
|
That is correct, but 1.5 will support proper ES6 output (i.e. output a class as a class). |
nickie
commented
Feb 1, 2016
|
TypeScript's specification seems to allow a missing initializer for destructuring lexical bindings, whereas the EC262 specification does not. Is this intentional? In that case, I believe it should only be allowed in the case that a type annotation is present, e.g., in |
|
@nickie, can you report a new bug for this, with the label Spec? |
nickie
commented
Feb 1, 2016
|
@sandersn FYI only Owners (i.e. us) can add Labels |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

mhegazy commentedOct 16, 2014
This change adds support for ES6-style block-scoped variable declarations. these are only available when targeting ES6. the change also adds a new target for ES6.
There are two variants, let and const. Generaly let and const behave like a var declarations with the following exceptions:
What is left: