GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Let and const support by mhegazy · Pull Request #904 · microsoft/TypeScript

Conversation

@mhegazy

@mhegazy Mohamed Hegazy (mhegazy) commented Oct 16, 2014

Copy link
Copy Markdown
Contributor

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:

  • Both let and const can not be re-declared:
let a = 0;
a = 1;
let a; // Error: redeclaration
  • It is an error to re-declare a block-scoped variable as 'var' in the same scope:
let a = 0;
{
     var a; // Error: redeclaration, as var is hoisted to the top of the scope, and re-declares a
}
  • Let and const can not be declared in a non-guarded statements
if (true) {
    let a = 0; // OK
}

if (false) 
   let b = 0; // Error

for (let i = 0; i< 10; i++)  // OK
    console.log(i); 
  • Let and const declarations are block scoped, and not hoisted to the top of the function like other JS declarations:
let a = 0;
{
    let a = "local";
    console.log(a) // local
}
console.log(a); // 0
  • Let and const can not be used before they are defined, to avoid the lexical dead zone of vars.
v = 2; // ok
var v;

a = 2;
let a; // Error, used before initialization
  • Const declarations must have an initializer, unless in ambient context
  • It is an error to write to a Const
const c = 0;
console.log(c); // OK: 0

c = 2; // Error
c++; // Error

{
    const c2 = 0;
    var c2 = 0; // not a redeclaration, as the var is hoisted out, but still a write to c2
}
  • Let and const can not be exported, only vars are allowed to.

What is left:

  • Block emit if any of the let/const errors are reported, these may be syntactic, binding, or semantic errors
  • Wire in Test262 for parser verification to ensure we are ES6 complaint

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Mohamed Hegazy (mhegazy) added 15 commits October 11, 2014 12:52
@mhegazy
Add es6 target
873c1df
@mhegazy
Add basic parsing support for let and const
778f101
@mhegazy
Disallow let and const declarations outside blocks
979d45e
@mhegazy
Fix line endings
6f6f4af
@mhegazy
Add binder support for block scoped variable declarations
cf89f5c
@mhegazy
Do not allow use of block-scoped variable before its definition
1dde985
@mhegazy
Ensure duplicate let/const declarations accross files are reported
318575c
@mhegazy
Report duplicate identifier errors on all locations for merged declar…
cffc62a
…ations to align with local declarations
@mhegazy
Flag assignments to a const
f5c2740
@mhegazy
Flag const declarations shodowed by var redeclarations
82f5fb4
@mhegazy
Allow const in for statements
3e45601
@mhegazy
Do not allow let and const declarations to be exported from a module
03a100d
@mhegazy
Fix emitting for const in for loops
6154923
@mhegazy
Merge branch 'master' into letAndConst
e15f4e6
Conflicts:
	src/compiler/types.ts
@mhegazy
Add language service support for const
60bb37b
CyrusNajmabadi
CyrusNajmabadi reviewed Oct 16, 2014
View reviewed changes
Comment thread src/compiler/parser.ts

@CyrusNajmabadi Cyrus Najmabadi (CyrusNajmabadi) Oct 16, 2014

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought we would not allow 'let' here as we're not in a block scope. or do you check for that later?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhegazy Mohamed Hegazy (mhegazy) Oct 17, 2014

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JsonFreeman Jason Freeman (JsonFreeman) Oct 17, 2014

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They say "such as", which I normally interpret as "including but not limited to".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhegazy Mohamed Hegazy (mhegazy) Oct 17, 2014

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. checked with Brian Terlson (@bterlson) and he confirmed that that is not the intention. so keeping Switch, try and finally blocks as valid lexical scopes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Oct 17, 2014

Copy link
Copy Markdown
Member

Overall looks great!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Mohamed Hegazy (mhegazy) added 10 commits October 17, 2014 09:28
@mhegazy
Fix search for shadowed const declarations by a var declarations to s…
fd469d6
…earch for any variable instead of only a blockScoped one to ensure we are not picking it up from a wrong scope.
@mhegazy
Respond to code review comments
4ef68b9
@mhegazy
Allow const and let declarations to be exported in modules. Also ensu…
a5a6c6f
…re that const module elements are not used as references.
@mhegazy
Treat blockScoped variable declarations as a separate category when i…
0a59cdd
…t comes to symbol flags, instead of compining BlockScoped and Variable
@mhegazy
Merge branch 'master' into letAndConst
0e7d8b6
@mhegazy
Update error messages
dd5c89d
@mhegazy
Simplify the binder logic for managing blockScopeContainer
91f4098
@mhegazy
allow let and const declarations in module bodies under labels
d5fe43b
@mhegazy
Create a new flag for diagnostics 'isEarly' and disable emit if this …
dd7ca69
…flag is set. Set the flag by default on all let and const errors to ensure we are not emitting invalid JS code.
@mhegazy
respond to code review comments
373dc76
Mohamed Hegazy (mhegazy) added 5 commits October 23, 2014 15:01
@mhegazy
Merge branch 'master' into letAndConst
9353c11
Conflicts:
	src/compiler/types.ts
@mhegazy
Merge branch 'master' into letAndConst
d1858d0
@mhegazy
Ensure let and const declarations in labels are parsed correctelly
e4a2084
@mhegazy
Only check for collisions with variabels and not properties
67c78a2
@mhegazy
Merge branch 'master' into letAndConst
51e101c
Mohamed Hegazy (mhegazy) added a commit that referenced this pull request Oct 24, 2014
@mhegazy
Merge pull request #904 from Microsoft/letAndConst
290e43b
Let and const support
@mhegazy
Mohamed Hegazy (mhegazy) merged commit 290e43b into master Oct 24, 2014
@mhegazy
Mohamed Hegazy (mhegazy) deleted the letAndConst branch October 24, 2014 18:26
This was referenced Oct 25, 2014
Suggestion: Support ES6 'let' and 'const' keyword #19
Closed
Add ScriptTarget.ES6 #595
Closed
@NoelAbrahams Noel Abrahams (NoelAbrahams) mentioned this pull request Jan 12, 2015
Allow pure ECMAScript block #1644
Closed
@adidahiya Adi Dahiya (adidahiya) mentioned this pull request Apr 20, 2015
ES6 let and const syntax palantir/tslint#371
Closed
@nickie

Nikolaos S. Papaspyrou (nickie) commented Feb 1, 2016

Copy link
Copy Markdown

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 let [x, y]: number[]; The current playground implementation seems to disallow this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn

Nathan Shively-Sanders (sandersn) commented Feb 1, 2016

Copy link
Copy Markdown
Member

Nikolaos S. Papaspyrou (@nickie), can you report a new bug for this, with the label Spec?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nickie

Nikolaos S. Papaspyrou (nickie) commented Feb 1, 2016

Copy link
Copy Markdown

Nathan Shively-Sanders (@sandersn), done (6785) but I don't know how to add the label. Please add it, or advise me how to. Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Feb 1, 2016

Copy link
Copy Markdown
Member

Nathan Shively-Sanders (@sandersn) FYI only Owners (i.e. us) can add Labels

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@myitcv Paul Jolly (myitcv) mentioned this pull request Apr 7, 2016
let variable shadowing let variable of same name cannot reference outer variable in initialiser #7939
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@mhegazy @ahejlsberg @nickie @sandersn @RyanCavanaugh @KirillGrishin @CyrusNajmabadi @JsonFreeman
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.