Skip to content

Commit

Permalink
Support script/cjs in prefer-module-scope-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Oct 29, 2020
1 parent 7280b1e commit b1521b3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Add support for CommonJS in `prefer-module-scope-constants` ([#195](https://github.com/Shopify/web-configs/pull/195)).

### Changed

- Updated `eslint-plugin` plugins ([#194](https://github.com/Shopify/web-foundation/pull/194)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
}

const scope = context.getScope();
if (scope.type !== 'module') {
if (!['module', 'global'].includes(scope.type)) {
context.report(
node,
'You must place screaming snake case at module scope. If this is not meant to be a module-scoped variable, use camelcase instead.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const rule = require('../../../lib/rules/prefer-module-scope-constants');

const ruleTester = new RuleTester();

const parserOptions = {ecmaVersion: 6, sourceType: 'module'};
const moduleScopeErrors = [
{
kind: 'VariableDeclarator',
Expand All @@ -20,46 +19,63 @@ const nonConstErrors = [
},
];

ruleTester.run('prefer-module-scope-constants', rule, {
valid: [
{code: 'const FOO = true;', parserOptions},
{code: 'const foo = true;', parserOptions},
{code: '{ const foo = true; }', parserOptions},
{code: 'const foo = true, FOO = true;', parserOptions},
{code: 'const {FOO} = bar', parserOptions},
{code: '{ const {FOO} = bar; }', parserOptions},
{code: 'function foo() { const {FOO} = bar; }', parserOptions},
{code: '{ let {FOO} = bar; }', parserOptions},
{code: 'function foo() { let {FOO} = bar; }', parserOptions},
{code: 'const [FOO] = bar', parserOptions},
{code: '{ const [FOO] = bar; }', parserOptions},
{code: 'function foo() { const [FOO] = bar; }', parserOptions},
{code: '{ let [FOO] = bar; }', parserOptions},
{code: 'function foo() { let [FOO] = bar; }', parserOptions},
],
invalid: [
{code: 'let FOO = true;', parserOptions, errors: nonConstErrors},
{code: '{ let FOO = true; }', parserOptions, errors: nonConstErrors},
{
code: 'function foo() { let FOO = true; }',
parserOptions,
errors: nonConstErrors,
},
{
code: 'let foo = false, FOO = true;',
parserOptions,
errors: nonConstErrors,
},
{code: '{ const FOO = true; }', parserOptions, errors: moduleScopeErrors},
{
code: 'function foo() { const FOO = true; }',
parserOptions,
errors: moduleScopeErrors,
},
{
code: '{ const foo = false, FOO = true; }',
parserOptions,
errors: moduleScopeErrors,
},
],
const supportedParserOptions = [
{ecmaVersion: 6, sourceType: 'module'},
{ecmaVersion: 6, sourceType: 'script'},
];

supportedParserOptions.forEach((parserOptions) => {
ruleTester.run('prefer-module-scope-constants', rule, {
valid: [
{code: 'const FOO = true;', parserOptions},
{code: 'const foo = true;', parserOptions},
{code: '{ const foo = true; }', parserOptions},
{code: 'const foo = true, FOO = true;', parserOptions},
{code: 'const {FOO} = bar', parserOptions},
{code: '{ const {FOO} = bar; }', parserOptions},
{code: 'function foo() { const {FOO} = bar; }', parserOptions},
{code: '{ let {FOO} = bar; }', parserOptions},
{code: 'function foo() { let {FOO} = bar; }', parserOptions},
{code: 'const [FOO] = bar', parserOptions},
{code: '{ const [FOO] = bar; }', parserOptions},
{code: 'function foo() { const [FOO] = bar; }', parserOptions},
{code: '{ let [FOO] = bar; }', parserOptions},
{code: 'function foo() { let [FOO] = bar; }', parserOptions},
{
code: `
const MY_VALUE = true;
module.exports = () => {
console.log(MY_VALUE);
};
`,
parserOptions,
},
],
invalid: [
{code: 'let FOO = true;', parserOptions, errors: nonConstErrors},
{code: '{ let FOO = true; }', parserOptions, errors: nonConstErrors},
{
code: 'function foo() { let FOO = true; }',
parserOptions,
errors: nonConstErrors,
},
{
code: 'let foo = false, FOO = true;',
parserOptions,
errors: nonConstErrors,
},
{code: '{ const FOO = true; }', parserOptions, errors: moduleScopeErrors},
{
code: 'function foo() { const FOO = true; }',
parserOptions,
errors: moduleScopeErrors,
},
{
code: '{ const foo = false, FOO = true; }',
parserOptions,
errors: moduleScopeErrors,
},
],
});
});

0 comments on commit b1521b3

Please sign in to comment.