Skip to content
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

Allow scripts to not have "use strict"; atop them #25

Merged
merged 1 commit into from
Feb 28, 2017
Merged

Allow scripts to not have "use strict"; atop them #25

merged 1 commit into from
Feb 28, 2017

Conversation

whymarrh
Copy link
Contributor

This PR updates the rule to not produce an error when there is no "use strict"; literal atop the file. This allows something else, some other rule (e.g. strict), to check whether or not "use strict"; exists (or shouldn't exist, as per below) and for this rule to ensure that if the file has "use strict"; at the top, that it is indeed followed by a newline.

From this SO answer on the use of "use strict"; in ES6 (the emphasis is theirs):

ES6 modules are always in strict mode. To quote the relevant part of the spec:

10.2.1 Strict Mode Code

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

  • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
  • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
  • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

This is all well and good, but if a project has mixed ES6 and ES5 code (e.g. a React app using ES6 modules + webpack but with some configuration scripts or build scripts using plain ol' ES5), some files will not have a "use strict"; atop them and ESLint will complain. I ended up in a situation where I couldn't appease ESLint by adding "use strict":

src/App.js
  1:1  error  'use strict' is unnecessary inside of modules  strict

Or by removing "use strict";:

src/App.js
  1:1  error  Expected newline after 'use strict'  strict-newline/strict-newline

Right now, I am simply disabling the strict-newline/strict-newline rule on a directory-by-directory basis, but it would be ideal if I could not have to do that.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 4333894 on whymarrh:allow-no-use-strict into 05ac98c on MitMaro:master.

1 similar comment
@coveralls
Copy link

coveralls commented Feb 27, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 4333894 on whymarrh:allow-no-use-strict into 05ac98c on MitMaro:master.

@MitMaro MitMaro merged commit 70ec0c9 into MitMaro:master Feb 28, 2017
@MitMaro
Copy link
Owner

MitMaro commented Feb 28, 2017

Thanks!

@whymarrh whymarrh deleted the allow-no-use-strict branch February 28, 2017 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants