Refactor ESLint config (#13482)

We would like to insert TypeScript into the ESLint configuration, and
because of the way that the current config is organized, that is not
easy to do.

Most files are assumed to be files that are suited for running in a
browser context. This isn't correct, as we should expect most files to
work in a Node context instead. This is because all browser-based files
will be run through a transpiler that is able to make use of
Node-specific variables anyway.

There are a couple of important ways we can categories files which our
ESLint config should be capable of handling well:

* Is the file a script or a module? In other words, does the file run
  procedurally or is the file intended to be brought into an existing
  file?
* If the file is a module, does it use the CommonJS syntax (`require()`)
  or does it use the ES syntax (`import`/`export`)?

When we introduce TypeScript, this set of questions will become:

* Is the file a script or a module?
* If the file is a module, is it a JavaScript module or a TypeScript
  module?
* If the file is a JavaScript module, does it use the CommonJS syntax
  (`require()`) or does it use the ES syntax (`import`/`export`)?

To represent these divisions, this commit removes global rules — so now
all of the rules are kept in `overrides` for explicitness — and sets up
rules for CommonJS- and ES-module-compatible files that intentionally do
not overlap with each other. This way TypeScript (which has its own set
of rules independent from JavaScript and therefore shouldn't overlap
with the other rules either) can be easily added later.

Finally, this commit splits up the ESLint config into separate files and
adds documentation to each section. This way sets of rules which are
connected to a particular plugin (`jsdoc`, `@babel`, etc.) can be easily
understood instead of being obscured.
feature/default_network_editable
Elliot Winkler 3 years ago committed by GitHub
parent 96ab979e7c
commit 1e494f3004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      .eslintrc.babel.js
  2. 67
      .eslintrc.base.js
  3. 352
      .eslintrc.js
  4. 23
      .eslintrc.jsdoc.js
  5. 10
      .eslintrc.node.js
  6. 1
      .prettierignore
  7. 16
      development/build/transforms/utils.js
  8. 1
      lavamoat/build-system/policy-override.json
  9. 1
      test/e2e/tests/add-hide-token.spec.js
  10. 1
      test/e2e/tests/send-eth.spec.js
  11. 1
      test/e2e/tests/signature-request.spec.js

@ -0,0 +1,9 @@
module.exports = {
parser: '@babel/eslint-parser',
plugins: ['@babel'],
rules: {
'@babel/no-invalid-this': 'error',
// Prettier handles this
'@babel/semi': 'off',
},
};

@ -0,0 +1,67 @@
const path = require('path');
module.exports = {
extends: [
'@metamask/eslint-config',
path.resolve(__dirname, '.eslintrc.jsdoc.js'),
],
globals: {
document: 'readonly',
window: 'readonly',
},
rules: {
'default-param-last': 'off',
'prefer-object-spread': 'error',
'require-atomic-updates': 'off',
// This is the same as our default config, but for the noted exceptions
'spaced-comment': [
'error',
'always',
{
markers: [
'global',
'globals',
'eslint',
'eslint-disable',
'*package',
'!',
',',
// Local additions
'/:', // This is for our code fences
],
exceptions: ['=', '-'],
},
],
'no-invalid-this': 'off',
// TODO: remove this override
'padding-line-between-statements': [
'error',
{
blankLine: 'always',
prev: 'directive',
next: '*',
},
{
blankLine: 'any',
prev: 'directive',
next: 'directive',
},
// Disabled temporarily to reduce conflicts while PR queue is large
// {
// blankLine: 'always',
// prev: ['multiline-block-like', 'multiline-expression'],
// next: ['multiline-block-like', 'multiline-expression'],
// },
],
// It is common to import modules without assigning them to variables in
// a browser context. For instance, we may import polyfills which change
// global variables, or we may import stylesheets.
'import/no-unassigned-import': 'off',
},
};

@ -1,132 +1,112 @@
const path = require('path');
const { version: reactVersion } = require('react/package.json'); const { version: reactVersion } = require('react/package.json');
module.exports = { module.exports = {
root: true, root: true,
parser: '@babel/eslint-parser', // Ignore files which are also in .prettierignore
parserOptions: {
sourceType: 'module',
ecmaVersion: 2017,
ecmaFeatures: {
experimentalObjectRestSpread: true,
impliedStrict: true,
modules: true,
blockBindings: true,
arrowFunctions: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
templateStrings: true,
classes: true,
jsx: true,
},
},
ignorePatterns: [ ignorePatterns: [
'!.eslintrc.js',
'!.mocharc.js',
'node_modules/**',
'dist/**',
'builds/**',
'test-*/**',
'docs/**',
'coverage/',
'jest-coverage/',
'development/chromereload.js',
'app/vendor/**', 'app/vendor/**',
'test/e2e/send-eth-with-private-key-test/**', 'builds/**/*',
'nyc_output/**', 'dist/**/*',
'.vscode/**', 'development/chromereload.js',
'lavamoat/*/policy.json',
'storybook-build/**',
], ],
overrides: [
/**
* == Modules ==
*
* The first two sections here, which cover module syntax, are mutually
* exclusive: the set of files covered between them may NOT overlap. This is
* because we do not allow a file to use two different styles for specifying
* imports and exports (however theoretically possible it may be).
*/
extends: ['@metamask/eslint-config', '@metamask/eslint-config-nodejs'], {
/**
plugins: ['@babel', 'import', 'jsdoc'], * Modules (CommonJS module syntax)
*
globals: { * This is code that uses `require()` and `module.exports` to import and
document: 'readonly', * export other modules.
window: 'readonly', */
files: [
'.eslintrc.js',
'.eslintrc.*.js',
'.mocharc.js',
'*.config.js',
'development/**/*.js',
'test/e2e/**/*.js',
'test/helpers/*.js',
'test/lib/wait-until-called.js',
],
extends: [
path.resolve(__dirname, '.eslintrc.base.js'),
path.resolve(__dirname, '.eslintrc.node.js'),
path.resolve(__dirname, '.eslintrc.babel.js'),
],
parserOptions: {
sourceType: 'module',
}, },
rules: { rules: {
'default-param-last': 'off', // This rule does not work with CommonJS modules. We will just have to
'prefer-object-spread': 'error', // trust that all of the files specified above are indeed modules.
'require-atomic-updates': 'off', 'import/unambiguous': 'off',
},
// This is the same as our default config, but for the noted exceptions },
'spaced-comment': [ /**
'error', * Modules (ES module syntax)
'always', *
* This is code that explicitly uses `import`/`export` instead of
* `require`/`module.exports`.
*/
{ {
markers: [ files: [
'global', 'app/**/*.js',
'globals', 'shared/**/*.js',
'eslint', 'ui/**/*.js',
'eslint-disable', '**/*.test.js',
'*package', 'test/lib/**/*.js',
'!', 'test/mocks/**/*.js',
',', 'test/jest/**/*.js',
// Local additions 'test/stub/**/*.js',
'/:', // This is for our code fences 'test/unit-global/**/*.js',
], ],
exceptions: ['=', '-'], // TODO: Convert these files to modern JS
}, excludedFiles: ['test/lib/wait-until-called.js'],
extends: [
path.resolve(__dirname, '.eslintrc.base.js'),
path.resolve(__dirname, '.eslintrc.node.js'),
path.resolve(__dirname, '.eslintrc.babel.js'),
], ],
parserOptions: {
sourceType: 'module',
},
},
'import/no-unassigned-import': 'off', /**
* == Everything else ==
'no-invalid-this': 'off', *
'@babel/no-invalid-this': 'error', * The sections from here on out may overlap with each other in various
* ways depending on their function.
// Prettier handles this */
'@babel/semi': 'off',
'node/no-process-env': 'off',
// Allow tag `jest-environment` to work around Jest bug
// See: https://github.com/facebook/jest/issues/7780
'jsdoc/check-tag-names': ['error', { definedTags: ['jest-environment'] }],
// TODO: remove this override /**
'padding-line-between-statements': [ * React-specific code
'error', *
* Code in this category contains JSX and hence needs to be run through the
* React plugin.
*/
{ {
blankLine: 'always', files: [
prev: 'directive', 'test/lib/render-helpers.js',
next: '*', 'test/jest/rendering.js',
}, 'ui/**/*.js',
{
blankLine: 'any',
prev: 'directive',
next: 'directive',
},
// Disabled temporarily to reduce conflicts while PR queue is large
// {
// blankLine: 'always',
// prev: ['multiline-block-like', 'multiline-expression'],
// next: ['multiline-block-like', 'multiline-expression'],
// },
], ],
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'],
// TODO: re-enable these rules parserOptions: {
'node/no-sync': 'off', ecmaFeatures: {
'node/no-unpublished-import': 'off', jsx: true,
'node/no-unpublished-require': 'off', },
'jsdoc/match-description': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns-description': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/valid-types': 'off',
}, },
overrides: [
{
files: ['ui/**/*.js', 'test/lib/render-helpers.js', 'test/jest/*.js'],
plugins: ['react'], plugins: ['react'],
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'],
rules: { rules: {
'react/no-unused-prop-types': 'error', 'react/no-unused-prop-types': 'error',
'react/no-unused-state': 'error', 'react/no-unused-state': 'error',
@ -139,74 +119,100 @@ module.exports = {
'react/default-props-match-prop-types': 'error', 'react/default-props-match-prop-types': 'error',
'react/jsx-no-duplicate-props': 'error', 'react/jsx-no-duplicate-props': 'error',
}, },
}, settings: {
{ react: {
files: ['test/e2e/**/*.spec.js'], // If this is set to 'detect', ESLint will import React in order to
extends: ['@metamask/eslint-config-mocha'], // find its version. Because we run ESLint in the build system under
rules: { // LavaMoat, this means that detecting the React version requires a
'mocha/no-hooks-for-single-case': 'off', // LavaMoat policy for all of React, in the build system. That's a
'mocha/no-setup-in-describe': 'off', // no-go, so we grab it from React's package.json.
}, version: reactVersion,
},
{
files: ['app/scripts/migrations/*.js', '*.stories.js'],
rules: {
'import/no-anonymous-default-export': ['error', { allowObject: true }],
}, },
}, },
{
files: ['app/scripts/migrations/*.js'],
rules: {
'node/global-require': 'off',
},
}, },
/**
* Mocha tests
*
* These are files that make use of globals and syntax introduced by the
* Mocha library.
*/
{ {
files: ['**/*.test.js'], files: [
'**/*.test.js',
'test/lib/wait-until-called.js',
'test/e2e/**/*.spec.js',
],
excludedFiles: [ excludedFiles: [
'ui/**/*.test.js', 'app/scripts/controllers/network/**/*.test.js',
'ui/__mocks__/*.js', 'app/scripts/controllers/permissions/**/*.test.js',
'shared/**/*.test.js',
'development/**/*.test.js',
'app/scripts/lib/**/*.test.js', 'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js', 'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js', 'app/scripts/platforms/*.test.js',
'app/scripts/controllers/network/**/*.test.js', 'development/**/*.test.js',
'app/scripts/controllers/permissions/**/*.test.js', 'shared/**/*.test.js',
'ui/**/*.test.js',
'ui/__mocks__/*.js',
], ],
extends: ['@metamask/eslint-config-mocha'], extends: ['@metamask/eslint-config-mocha'],
rules: { rules: {
// In Mocha tests, it is common to use `this` to store values or do
// things like force the test to fail.
'@babel/no-invalid-this': 'off',
'mocha/no-setup-in-describe': 'off', 'mocha/no-setup-in-describe': 'off',
}, },
}, },
{ /**
files: ['**/__snapshots__/*.snap'], * Jest tests
plugins: ['jest'], *
rules: { * These are files that make use of globals and syntax introduced by the
'jest/no-large-snapshots': [ * Jest library.
'error', */
{ maxSize: 50, inlineMaxSize: 50 },
],
},
},
{ {
files: [ files: [
'ui/**/*.test.js', '**/__snapshots__/*.snap',
'ui/__mocks__/*.js', 'app/scripts/controllers/network/**/*.test.js',
'shared/**/*.test.js', 'app/scripts/controllers/permissions/**/*.test.js',
'development/**/*.test.js',
'app/scripts/lib/**/*.test.js', 'app/scripts/lib/**/*.test.js',
'app/scripts/migrations/*.test.js', 'app/scripts/migrations/*.test.js',
'app/scripts/platforms/*.test.js', 'app/scripts/platforms/*.test.js',
'app/scripts/controllers/network/**/*.test.js', 'development/**/*.test.js',
'app/scripts/controllers/permissions/**/*.test.js', 'shared/**/*.test.js',
'test/jest/*.js',
'test/helpers/*.js',
'ui/**/*.test.js',
'ui/__mocks__/*.js',
], ],
extends: ['@metamask/eslint-config-jest'], extends: ['@metamask/eslint-config-jest'],
parserOptions: {
sourceType: 'module',
},
rules: { rules: {
'jest/no-restricted-matchers': 'off',
'import/unambiguous': 'off', 'import/unambiguous': 'off',
'import/named': 'off', 'import/named': 'off',
'jest/no-large-snapshots': [
'error',
{ maxSize: 50, inlineMaxSize: 50 },
],
'jest/no-restricted-matchers': 'off',
},
},
/**
* Migrations
*/
{
files: ['app/scripts/migrations/*.js', '**/*.stories.js'],
rules: {
'import/no-anonymous-default-export': ['error', { allowObject: true }],
}, },
}, },
/**
* Executables and related files
*
* These are files that run in a Node context. They are either designed to
* run as executables (in which case they will have a shebang at the top) or
* are dependencies of executables (in which case they may use
* `process.exit` to exit).
*/
{ {
files: [ files: [
'development/**/*.js', 'development/**/*.js',
@ -218,27 +224,9 @@ module.exports = {
'node/shebang': 'off', 'node/shebang': 'off',
}, },
}, },
{ /**
files: [ * Lockdown files
'.eslintrc.js', */
'.mocharc.js',
'babel.config.js',
'jest.config.js',
'nyc.config.js',
'stylelint.config.js',
'app/scripts/lockdown-run.js',
'app/scripts/lockdown-more.js',
'development/**/*.js',
'test/e2e/**/*.js',
'test/env.js',
'test/setup.js',
'test/helpers/protect-intrinsics-helpers.js',
'test/lib/wait-until-called.js',
],
parserOptions: {
sourceType: 'script',
},
},
{ {
files: [ files: [
'app/scripts/lockdown-run.js', 'app/scripts/lockdown-run.js',
@ -251,19 +239,11 @@ module.exports = {
Compartment: 'readonly', Compartment: 'readonly',
}, },
}, },
], {
files: ['app/scripts/lockdown-run.js', 'app/scripts/lockdown-more.js'],
settings: { parserOptions: {
jsdoc: { sourceType: 'script',
mode: 'typescript',
},
react: {
// If this is set to 'detect', ESLint will import React in order to find
// its version. Because we run ESLint in the build system under LavaMoat,
// this means that detecting the React version requires a LavaMoat policy
// for all of React, in the build system. That's a no-go, so we grab it
// from React's package.json.
version: reactVersion,
}, },
}, },
],
}; };

@ -0,0 +1,23 @@
module.exports = {
// Note that jsdoc is already in the `plugins` array thanks to
// @metamask/eslint-config — this just extends the config there
rules: {
// Allow tag `jest-environment` to work around Jest bug
// See: https://github.com/facebook/jest/issues/7780
'jsdoc/check-tag-names': ['error', { definedTags: ['jest-environment'] }],
'jsdoc/match-description': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns-description': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/valid-types': 'off',
},
settings: {
jsdoc: {
mode: 'typescript',
},
},
};

@ -0,0 +1,10 @@
module.exports = {
extends: ['@metamask/eslint-config-nodejs'],
rules: {
'node/no-process-env': 'off',
// TODO: re-enable these rules
'node/no-sync': 'off',
'node/no-unpublished-import': 'off',
'node/no-unpublished-require': 'off',
},
};

@ -10,3 +10,4 @@ app/vendor/**
.vscode/** .vscode/**
test/e2e/send-eth-with-private-key-test/** test/e2e/send-eth-with-private-key-test/**
*.scss *.scss
development/chromereload.js

@ -1,11 +1,17 @@
const { ESLint } = require('eslint'); const { ESLint } = require('eslint');
const eslintrc = require('../../../.eslintrc.js'); const eslintrc = require('../../../.eslintrc.js');
// We don't want linting to fail for purely stylistic reasons. eslintrc.overrides.forEach((override) => {
eslintrc.rules['prettier/prettier'] = 'off'; const rules = override.rules ?? {};
// Sometimes we use `let` instead of `const` to assign variables depending on
// the build type. // We don't want linting to fail for purely stylistic reasons.
eslintrc.rules['prefer-const'] = 'off'; rules['prettier/prettier'] = 'off';
// Sometimes we use `let` instead of `const` to assign variables depending on
// the build type.
rules['prefer-const'] = 'off';
override.rules = rules;
});
// Remove all test-related overrides. We will never lint test files here. // Remove all test-related overrides. We will never lint test files here.
eslintrc.overrides = eslintrc.overrides.filter((override) => { eslintrc.overrides = eslintrc.overrides.filter((override) => {

@ -14,6 +14,7 @@
}, },
"@eslint/eslintrc": { "@eslint/eslintrc": {
"packages": { "packages": {
"<root>": true,
"@babel/eslint-parser": true, "@babel/eslint-parser": true,
"@babel/eslint-plugin": true, "@babel/eslint-plugin": true,
"@metamask/eslint-config": true, "@metamask/eslint-config": true,

@ -55,6 +55,7 @@ describe('Hide token', function () {
}); });
}); });
/* eslint-disable-next-line mocha/max-top-level-suites */
describe('Add existing token using search', function () { describe('Add existing token using search', function () {
const ganacheOptions = { const ganacheOptions = {
accounts: [ accounts: [

@ -91,6 +91,7 @@ describe('Send ETH from inside MetaMask using default gas', function () {
}); });
}); });
/* eslint-disable-next-line mocha/max-top-level-suites */
describe('Send ETH from inside MetaMask using advanced gas modal', function () { describe('Send ETH from inside MetaMask using advanced gas modal', function () {
const ganacheOptions = { const ganacheOptions = {
accounts: [ accounts: [

@ -86,6 +86,7 @@ describe('Sign Typed Data V4 Signature Request', function () {
}); });
}); });
/* eslint-disable-next-line mocha/max-top-level-suites */
describe('Sign Typed Data V3 Signature Request', function () { describe('Sign Typed Data V3 Signature Request', function () {
it('can initiate and confirm a Signature Request', async function () { it('can initiate and confirm a Signature Request', async function () {
const ganacheOptions = { const ganacheOptions = {

Loading…
Cancel
Save