From e9d756179b31cee1cc8d6e69f58255421e354e96 Mon Sep 17 00:00:00 2001 From: Charlie Briggs Date: Tue, 9 Jul 2019 17:32:13 +0100 Subject: [PATCH] Use extends in overrides Now https://github.com/eslint/eslint/issues/8813 is fixed this leads to much less object merging --- rules/auto.js | 43 +++++++++++++++++-------------------------- rules/cypress.js | 13 ++++++------- rules/jest.js | 35 +++++++++++++++-------------------- 3 files changed, 38 insertions(+), 53 deletions(-) diff --git a/rules/auto.js b/rules/auto.js index 08a73d6..df48c0f 100644 --- a/rules/auto.js +++ b/rules/auto.js @@ -31,34 +31,25 @@ const getConfig = ({ prettier = true, esModules = false }) => { config.settings = { react: { pragma: 'h' } }; } - const getJestConfig = () => { - // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813 - const jestConfig = require('./jest'); - return Object.assign({}, jestConfig, { - files: [ - '**/__tests__/**/*.js', - '**/__mocks__/**/*.js', - 'test/**/*.js', - 'test/**/*.jsx', - ], - }); - }; + const getJestConfig = () => ({ + files: [ + '**/__tests__/**/*.js', + '**/__mocks__/**/*.js', + 'test/**/*.js', + 'test/**/*.jsx', + ], + extends: path.join(__dirname, './jest.js'), + }); - const getMochaConfig = () => { - // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813 - const mochaConfig = require('../mocha'); - return Object.assign({}, mochaConfig, { - files: ['**/*.test.js', '**/*.spec.js'], - }); - }; + const getMochaConfig = () => ({ + files: ['**/*.test.js', '**/*.spec.js'], + extends: path.join(__dirname, './mocha.js'), + }); - const getCypressConfig = () => { - // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813 - const cypressConfig = require('./cypress'); - return Object.assign({}, cypressConfig, { - files: ['cypress/**/*.js'], - }); - }; + const getCypressConfig = () => ({ + files: ['cypress/**/*.js'], + extends: path.join(__dirname, './cypress.js'), + }); config.extends = usingJsx ? [path.join(__dirname, './react.js')] diff --git a/rules/cypress.js b/rules/cypress.js index 46fbb2a..e97993c 100644 --- a/rules/cypress.js +++ b/rules/cypress.js @@ -1,11 +1,11 @@ 'use strict'; -const cypressConfig = require('eslint-plugin-cypress').configs.recommended; -const mochaConfig = require('./mocha'); +const path = require('path'); -const mergedConfig = Object.assign({}, mochaConfig, { +const mergedConfig = { + extends: ['plugin:cypress/recommended', path.join(__dirname, './mocha.js')], // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813 - rules: Object.assign({}, mochaConfig.rules, cypressConfig.rules, { + rules: { // allow top-level hooks in cypress setup files 'mocha/no-top-level-hooks': 'off', // allow dev-deps in cypress test directories @@ -16,8 +16,7 @@ const mergedConfig = Object.assign({}, mochaConfig, { optionalDependencies: false, }, ], - }), - plugins: [...(mochaConfig.plugins || []), ...(cypressConfig.plugins || [])], -}); + }, +}; module.exports = mergedConfig; diff --git a/rules/jest.js b/rules/jest.js index 8784c0e..4cd426b 100644 --- a/rules/jest.js +++ b/rules/jest.js @@ -1,32 +1,27 @@ 'use strict'; -const jestConfig = require('eslint-plugin-jest').configs.recommended; const { usingJsx } = require('../lib/feature-detect'); -// Can't extend in overrides: https://github.com/eslint/eslint/issues/8813 -// the default jest/globals with jest/recommended doesn't work with eslint@5.x.x -// https://github.com/jest-community/eslint-plugin-jest/issues/128 -jestConfig.env = { - jest: true, +const config = { + extends: ['plugin:jest/recommended'], + rules: { + // This disallows .skip, which is auseful tool for writing skelton tests + // prior to full implementation. no-focused-tests is still enabled, which + // catches .only - that's the killer + 'jest/no-disabled-tests': [0, { extensions: ['.js', '.jsx'] }], + + // Disable as it's frequently necessary to do inline requires + // when working with jest mocks + 'global-require': 'off', + }, }; + if (usingJsx) { // allow test files to contain jsx but still keep the .js extension - jestConfig.rules['react/jsx-filename-extension'] = [ + config.rules['react/jsx-filename-extension'] = [ 'error', { extensions: ['.js', '.jsx'] }, ]; } -// This disallows .skip, which is auseful tool for writing skelton tests -// prior to full implementation. no-focused-tests is still enabled, which -// catches .only - that's the killer -jestConfig.rules['jest/no-disabled-tests'] = [ - 0, - { extensions: ['.js', '.jsx'] }, -]; - -// Disable as it's frequently necessary to do inline requires -// when working with jest mocks -jestConfig.rules['global-require'] = 'off'; - -module.exports = jestConfig; +module.exports = config;