Skip to content

add grammar check for labeled declaration#25317

Merged
weswigham merged 6 commits into
microsoft:masterfrom
Kingwl:grammarCheckForLabeledFunction
Jul 18, 2018
Merged

add grammar check for labeled declaration#25317
weswigham merged 6 commits into
microsoft:masterfrom
Kingwl:grammarCheckForLabeledFunction

Conversation

@Kingwl
Copy link
Copy Markdown
Contributor

@Kingwl Kingwl commented Jun 29, 2018

Fixes #25183
Fixes #25326

@Kingwl Kingwl force-pushed the grammarCheckForLabeledFunction branch from b5fe0b8 to ec04ca5 Compare June 29, 2018 06:27
Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 1343
},
"Association of a statement label with a function declaration is not allowed.": {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

how about A function declaration can not be the target of a label.

@DanielRosenwasser any better suggestions here?

Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser Jun 29, 2018

Choose a reason for hiding this comment

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

Surprisingly, variable statements can be targets of labeled statements - which I think is really stupid!

If we were willing to make that break, we could just make it something like

Declarations cannot be labeled.

Since you're not allowed to have class declarations be labeled either, and presumably the same should apply to enums and namespaces.

We may also want to consider

'{0}' declarations cannot be labeled.

where 0 is substituted with var, const, let, enum, function, function*, async function, class.

Also, keep in mind #25326.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

module or namespace has an error now:
A namespace declaration is only allowed in a namespace or module.

@@ -0,0 +1,8 @@
error TS2318: Cannot find global type 'IterableIterator'.
Copy link
Copy Markdown
Contributor

@mhegazy mhegazy Jun 29, 2018

Choose a reason for hiding this comment

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

Add @lib to avoid the errors.

Comment thread src/compiler/checker.ts Outdated
return false;
});
}
if (compilerOptions.target! >= ScriptTarget.ES2015) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It should also be disallowed when in strict mode. e.g.:

function bar() {
    "use strict";
    l: function f() { }  // Error
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We do all our handling of strict mode checks in the binder and not in the checker.

@Kingwl Kingwl force-pushed the grammarCheckForLabeledFunction branch 2 times, most recently from 280ad14 to e9aa180 Compare June 30, 2018 15:46
(function (E) {
})(E || (E = {}));
}
label:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

related #25340, #22686
😅not my fault

@Kingwl Kingwl force-pushed the grammarCheckForLabeledFunction branch from e9aa180 to 8dae277 Compare June 30, 2018 16:02
@Kingwl Kingwl changed the title add grammar check for labeled function declaration add grammar check for labeled declaration Jun 30, 2018
Comment thread src/compiler/binder.ts Outdated
// Grammar checking for labeledStatement
if (inStrictMode && options.target! >= ScriptTarget.ES2015) {
if (isDeclarationStatement(node.statement) || isVariableStatement(node.statement)) {
errorOnFirstToken(node.label, Diagnostics._0_declarations_cannot_be_labeled, formatSyntaxKind(node.statement.kind));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not think we ever exposed these kinds in error messages..

What we have done is have a generic message, like A label is not allowed here, or Invalid use of label.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the other alternative is to have a switch statement for the kind, e.g. "function" for SyntaxKind.FunctonDeclaration, but personally I do not think the value you get is worth the effort.

return isIterationStatement(statement, /*lookInLabeledStatements*/ false)
? visitIterationStatement(statement, /*outermostLabeledStatement*/ node)
: restoreEnclosingLabel(visitNode(statement, visitor, isStatement), node, convertedLoopState && resetLabel);
: restoreEnclosingLabel(visitNode(statement, visitor, isStatement, liftToBlock), node, convertedLoopState && resetLabel);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure i understand what this change is doing?

Copy link
Copy Markdown
Member

@weswigham weswigham Jul 6, 2018

Choose a reason for hiding this comment

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

Fixing #25340 by lifting a sequence of, eg VariableStatement, EndOfDeclarationMarker (like a downlevel class produces) into a block. (Since right now that triggers an assertion violation)

@weswigham
Copy link
Copy Markdown
Member

@Kingwl Looks good - do you want to also add the various import and export statement forms to your test for coverage's sake? (import a = require(), import "a", export {}, export default a, and export = {})

@Kingwl Kingwl force-pushed the grammarCheckForLabeledFunction branch from e345693 to 2c516bf Compare July 9, 2018 14:32
@Kingwl Kingwl force-pushed the grammarCheckForLabeledFunction branch from 2c516bf to d6ba356 Compare July 16, 2018 09:43
@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jul 18, 2018

@weswigham can you please review and merge

@weswigham weswigham merged commit ed8b764 into microsoft:master Jul 18, 2018
@Kingwl Kingwl deleted the grammarCheckForLabeledFunction branch July 19, 2018 00:08
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants