Skip to content

add use strict and simple parameter check#25633

Merged
RyanCavanaugh merged 6 commits into
microsoft:masterfrom
Kingwl:strictParameter
Sep 6, 2018
Merged

add use strict and simple parameter check#25633
RyanCavanaugh merged 6 commits into
microsoft:masterfrom
Kingwl:strictParameter

Conversation

@Kingwl
Copy link
Copy Markdown
Contributor

@Kingwl Kingwl commented Jul 13, 2018

Fixes #6815, #25616

Comment thread src/compiler/checker.ts Outdated
}

function checkGrammarForUseStrictSimpleParameterList (node: FunctionLikeDeclaration): boolean {
const useStrictDirective = node.body && isBlock(node.body) && startsWithUseStrict(node.body.statements) ? first(node.body.statements) : undefined;
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.

startsWithUseStrict is actually not correct. It doesn't handle the case where 'use strict' is not the first directive in the prologue:

function foo(param = 1) {
  'foo';
  'use strict'; // this is also a syntax error
}

Also note that startsWithUseStrict is also not correct about what it considers a Use Strict Directive:
'use\u0020strict'; is not a valid Use Strict Directive, but the function thinks it is.
Though that is probably not your responsibility to fix it in this PR.

@mhegazy mhegazy requested a review from weswigham July 13, 2018 16:45
Comment thread src/compiler/checker.ts Outdated
}

function checkGrammarForUseStrictSimpleParameterList (node: FunctionLikeDeclaration): boolean {
const useStrictDirective = node.body && isBlock(node.body) && startsWithUseStrict(node.body.statements) ? first(node.body.statements) : undefined;
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.

Is it necessary to only do this check if the target is ES2016 or higher? These early errors were introduced in ES2016 and are valid in ES2015: https://www.ecma-international.org/ecma-262/6.0/index.html

@Kingwl Kingwl force-pushed the strictParameter branch from 78ade3a to cdfef4f Compare July 16, 2018 09:41
@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jul 16, 2018

@weswigham can you please review?

Comment thread src/compiler/checker.ts Outdated
if (parameters.length === 0) return [];

const last = lastOrUndefined(parameters);
if (last && isRestParameter(last)) return [last];
Copy link
Copy Markdown
Member

@weswigham weswigham Jul 31, 2018

Choose a reason for hiding this comment

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

The presence of a rest parameter likely shouldn't preclude other parameters from being non-simple. Since an error is issued (well, marked with related spans) on each non-simple parameter, I think the restiness check aughta get moved into the filter below (this way fixing one error on the rest doesn't just move the error to the initialized parameter before it, for example).

A test of this would be good, too:

function foo(a = 3, ... b) {
  "use strict";
}

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 1345
},
"{0} is here.": {
Copy link
Copy Markdown
Member

@weswigham weswigham Jul 31, 2018

Choose a reason for hiding this comment

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

I imagine this is a localization hazard. Parameter declared here. and "use strict" directive used here. should be written in full, instead of a "generic" message, so "parameter" and "use strict" directive get localized.

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 1344
},
"'use strict' directive cannot be used with non simple parameter list.": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: non-simple instead of non simple just because non isn't a word.

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

This seems by and large great - just some small changes I think we need to localize correctly and issue better errors.

!!! related TS1348 tests/cases/conformance/functions/functionWithUseStrictAndSimpleParameterList_es2016.ts:20:5: Use strict directive used here.
~~~~~~~
!!! error TS1345: This parameter is not allowed with 'use strict' directive.
!!! related TS1348 tests/cases/conformance/functions/functionWithUseStrictAndSimpleParameterList_es2016.ts:20:5: Use strict directive used here.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Message should be 'use strict' directive and not Use strict directive to match the other diagnostic.

@RyanCavanaugh
Copy link
Copy Markdown
Member

@weswigham one more round please

@RyanCavanaugh RyanCavanaugh merged commit ed70d48 into microsoft:master Sep 6, 2018
@RyanCavanaugh
Copy link
Copy Markdown
Member

Thanks!

@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.

5 participants