-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Report error for function declarations as statement children in strict mode #62899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Report error for function declarations as statement children in strict mode #62899
Conversation
…t mode Fixes microsoft#62896 In strict mode, function declarations can only appear at the top level of a script, module, or function body, or inside a block. Code like `if (true) function f() {}` is a syntax error in strict mode but TypeScript was not reporting it. This change adds a check in the binder to detect when a function declaration is a direct child of a statement node (IfStatement, WhileStatement, DoStatement, ForStatement, ForInStatement, ForOfStatement, WithStatement, or LabeledStatement) and reports error TS1256.
Update the fix to report the error regardless of strict mode context, as TypeScript assumes strict mode at all times. Also improved the error message to be more helpful: "Function declarations are not allowed inside statements. Use a block statement to wrap the function declaration."
|
@RyanCavanaugh In the issue you mentioned "now that we assume strict mode at all times, this can be a proper parse error." I've implemented this to report unconditionally (regardless of the function checkStrictModeWithStatement(node: WithStatement) {
if (inStrictMode) {
errorOnFirstToken(node, Diagnostics.with_statements_are_not_allowed_in_strict_mode);
}
}Could you elaborate on whether this new check should:
Happy to adjust based on your guidance! |
This is not actually the case. I don't think we should do this until 7.0. |
|
Hm, no: #54500 Probably should only report when isStrictMode is true, but we will just remove that option in a later PR (or, earlier?) |
Per @jakebailey's feedback, TypeScript does not assume strict mode at all times. The error should only be reported when inStrictMode is true, consistent with other strict mode checks like 'with' statements. Updated the error message to mention strict mode and updated baselines accordingly.
|
@jakebailey Thanks for the clarification! I've updated the PR to only report the error when |
Fixes #62896
Problem
In strict mode, function declarations can only appear at the top level of a script, module, or function body, or inside a block. Code like
if (true) function f() {}is a syntax error in strict mode, and JavaScript engines throw:However, TypeScript was not reporting this error, allowing invalid code to pass compilation.
Fix
Added a check in the binder to detect when a function declaration is a direct child of a statement node and report error TS1256 in strict mode.
The affected statement types are:
IfStatementWhileStatementDoStatementForStatementForInStatementForOfStatementWithStatementLabeledStatementExamples
Test
Added
tests/cases/compiler/functionDeclarationAsStatementInStrictMode.tscovering all error and valid cases.