-
Notifications
You must be signed in to change notification settings - Fork 0
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
Ensure strict mode directive in all files #1005
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cruikshanks
requested review from
Demwunz,
robertparkinson,
Jozzey,
jonathangoulding,
Beckyrose200 and
rvsiyad
May 12, 2024 21:23
jonathangoulding
previously approved these changes
May 13, 2024
robertparkinson
previously approved these changes
May 13, 2024
Jozzey
previously approved these changes
May 13, 2024
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.
DEFRA/water-abstraction-team#115 It is currently an unwritten convention (we are [working on](DEFRA/water-abstraction-team#117) fixing that!) to add 'use strict' to the top of all our files. Why? > JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". > [MSDN docs - Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) When the alternate mode is named 'sloppy', would you want to use it!? The docs do provide some further explanation of what declaring strict mode means > Strict mode makes several changes to normal JavaScript semantics: > > 1. Eliminates some JavaScript silent errors by changing them to throw errors. > 2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. > 3. Prohibits some syntax likely to be defined in future versions of ECMAScript. For these reasons, this change updates our ESLint rules to ensure we do this.
To make this work we also had to tell ESLint we were not using ESM Modules as our default. It [defaults `sourceType` to 'module'](https://eslint.org/docs/latest/use/configure/language-options#specifying-javascript-options). When you use ESM modules strict mode is implied to what actually happens is ESLint shouts at us for hacving all these 'use strict' declarations!
We may have misunderstood the ESLint docs but 'commonjs' seemed the appropriate sourceType to set. > commonjs - CommonJS module (useful if your code uses require()). Your code has a top-level function scope and runs in non-strict mode. Running the linting worked fine. But then we tried to run the unit tests ``` > [email protected] test > lab --silent-skips --shuffle Error requiring file: /home/repos/water-abstraction-system/test/controllers/bill-licences.controller.test.js .sourceType must be "module", "script", "unambiguous", or undefined Error: .sourceType must be "module", "script", "unambiguous", or undefined at validate (/home/repos/water-abstraction-system/node_modules/@babel/core/lib/config/validation/options.js:92:25) at loadPrivatePartialConfig (/home/repos/water-abstraction-system/node_modules/@babel/core/lib/config/partial.js:80:50) at loadPrivatePartialConfig.next (<anonymous>) at Object.<anonymous> (/home/repos/water-abstraction-system/node_modules/@babel/core/lib/config/partial.js:149:25) at Generator.next (<anonymous>) at evaluateSync (/home/repos/water-abstraction-system/node_modules/gensync/index.js:251:28) at Object.sync [as loadPartialConfigSync] (/home/repos/water-abstraction-system/node_modules/gensync/index.js:89:14) at normalizeBabelParseConfigSync (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/worker/configuration.cjs:87:24) at LocalClient.handleMessage (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/worker/handle-message.cjs:25:90) at LocalClient.<anonymous> (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/client.cjs:98:79) at LocalClient.maybeParse (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/client.cjs:52:47) at parse (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/parse.cjs:28:14) at Object.parse (/home/repos/water-abstraction-system/node_modules/@babel/eslint-parser/lib/index.cjs:19:10) at internals.instrument (/home/repos/water-abstraction-system/node_modules/@hapi/lab/lib/modules/coverage.js:327:31) at require.extensions.<computed> [as .js] (/home/repos/water-abstraction-system/node_modules/@hapi/lab/lib/modules/coverage.js:117:59) at Module.load (node:internal/modules/cjs/loader:1206:32) at Module._load (node:internal/modules/cjs/loader:1022:12) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:179:18) at Object.<anonymous> (/home/repos/water-abstraction-system/test/controllers/bill-licences.controller.test.js:12:34) at Module._compile (node:internal/modules/cjs/loader:1369:14) at require.extensions.<computed> [as .js] (/home/repos/water-abstraction-system/node_modules/@hapi/lab/lib/modules/coverage.js:123:28) at Module.load (node:internal/modules/cjs/loader:1206:32) at Module._load (node:internal/modules/cjs/loader:1022:12) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:179:18) at internals.tryToResolveCJS (/home/repos/water-abstraction-system/node_modules/mo-walk/lib/index.js:144:17) at exports.tryToResolve (/home/repos/water-abstraction-system/node_modules/mo-walk/lib/index.js:106:22) at internals.traverse (/home/repos/water-abstraction-system/node_modules/@hapi/lab/lib/cli.js:152:39) at async exports.run (/home/repos/water-abstraction-system/node_modules/@hapi/lab/lib/cli.js:61:21) at async main (/home/repos/water-abstraction-system/node_modules/@hapi/lab/bin/lab:56:22) ``` We suspect this is an issue with Hapi Lab and its dependencies (boy we love JavaScript!) Setting the sourceType to 'script' makes the tests happy and we ESLint is still doing what we want it to.
Cruikshanks
dismissed stale reviews from Jozzey, robertparkinson, and jonathangoulding
via
May 13, 2024 11:43
e346840
Cruikshanks
force-pushed
the
require-strict-mode-directives
branch
from
May 13, 2024 11:43
2f4bae8
to
e346840
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
DEFRA/water-abstraction-team#115
It is currently an unwritten convention (we are working on fixing that!) to add 'use strict' to the top of all our files. Why?
When the alternate mode is named 'sloppy', would you want to use it!?
The docs do provide some further explanation of what declaring strict mode means
For these reasons, this change updates our ESLint rules to ensure we do this.