From 31c7cef6ec3216a0c9331d394666c29f07ea88ed Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 17 Jul 2019 10:53:30 +0200 Subject: [PATCH] chore: convert to import/export (#302) --- .eslintrc.js | 25 +++-- package.json | 1 + src/__tests__/rules.test.js | 12 +- src/index.js | 26 +++-- .../__tests__/snapshot-processor.test.js | 4 +- src/processors/snapshot-processor.js | 11 +- .../__tests__/consistent-test-it.test.js | 6 +- src/rules/__tests__/expect-expect.test.js | 6 +- src/rules/__tests__/lowercase-name.test.js | 6 +- src/rules/__tests__/no-alias-methods.test.js | 6 +- .../__tests__/no-commented-out-tests.test.js | 6 +- src/rules/__tests__/no-disabled-tests.test.js | 6 +- .../__tests__/no-duplicate-hooks.test.js | 6 +- src/rules/__tests__/no-empty-title.js | 6 +- src/rules/__tests__/no-focused-tests.test.js | 6 +- src/rules/__tests__/no-hooks.test.js | 6 +- .../__tests__/no-identical-title.test.js | 6 +- .../__tests__/no-jasmine-globals.test.js | 6 +- src/rules/__tests__/no-jest-import.test.js | 5 +- .../__tests__/no-large-snapshots.test.js | 8 +- src/rules/__tests__/no-mocks-import.test.js | 5 +- src/rules/__tests__/no-test-callback.test.js | 6 +- src/rules/__tests__/no-test-prefixes.test.js | 6 +- .../no-test-return-statement.test.js | 6 +- src/rules/__tests__/no-truthy-falsy.test.js | 6 +- src/rules/__tests__/prefer-called-with.js | 6 +- .../prefer-expect-assertions.test.js | 6 +- .../__tests__/prefer-inline-snapshots.test.js | 6 +- src/rules/__tests__/prefer-spy-on.test.js | 6 +- .../__tests__/prefer-strict-equal.test.js | 6 +- src/rules/__tests__/prefer-to-be-null.test.js | 6 +- .../__tests__/prefer-to-be-undefined.test.js | 6 +- src/rules/__tests__/prefer-to-contain.test.js | 6 +- .../__tests__/prefer-to-have-length.test.js | 6 +- src/rules/__tests__/prefer-todo.test.js | 6 +- .../__tests__/require-tothrow-message.test.js | 6 +- src/rules/__tests__/valid-describe.test.js | 6 +- .../__tests__/valid-expect-in-promise.test.js | 6 +- src/rules/__tests__/valid-expect.test.js | 6 +- src/rules/consistent-test-it.js | 6 +- src/rules/expect-expect.js | 6 +- src/rules/lowercase-name.js | 6 +- src/rules/no-alias-methods.js | 6 +- src/rules/no-commented-out-tests.js | 6 +- src/rules/no-disabled-tests.js | 6 +- src/rules/no-duplicate-hooks.js | 6 +- src/rules/no-empty-title.js | 14 +-- src/rules/no-focused-tests.js | 6 +- src/rules/no-hooks.js | 6 +- src/rules/no-identical-title.js | 14 +-- src/rules/no-jasmine-globals.js | 6 +- src/rules/no-jest-import.js | 6 +- src/rules/no-large-snapshots.js | 11 +- src/rules/no-mocks-import.js | 7 +- src/rules/no-test-callback.js | 6 +- src/rules/no-test-prefixes.js | 6 +- src/rules/no-test-return-statement.js | 6 +- src/rules/no-truthy-falsy.js | 12 +- src/rules/prefer-called-with.js | 6 +- src/rules/prefer-expect-assertions.js | 6 +- src/rules/prefer-inline-snapshots.js | 6 +- src/rules/prefer-spy-on.js | 6 +- src/rules/prefer-strict-equal.js | 6 +- src/rules/prefer-to-be-null.js | 14 +-- src/rules/prefer-to-be-undefined.js | 12 +- src/rules/prefer-to-contain.js | 14 +-- src/rules/prefer-to-have-length.js | 12 +- src/rules/prefer-todo.js | 12 +- src/rules/require-tothrow-message.js | 6 +- src/rules/util.js | 84 +++++--------- src/rules/valid-describe.js | 6 +- src/rules/valid-expect-in-promise.js | 6 +- src/rules/valid-expect.js | 6 +- yarn.lock | 103 +++++++++++++++++- 74 files changed, 335 insertions(+), 389 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e2f7ddd4f..5667fedb2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,9 +9,9 @@ module.exports = { 'plugin:node/recommended', 'prettier', ], - plugins: ['eslint-plugin', 'node', 'prettier'], + plugins: ['eslint-plugin', 'node', 'prettier', 'import'], parserOptions: { - ecmaVersion: 2017, + ecmaVersion: 2018, }, env: { node: true, @@ -24,21 +24,30 @@ module.exports = { 'object-shorthand': ['warn', 'always', { avoidExplicitReturnArrows: true }], 'prefer-destructuring': [ 'error', - { - VariableDeclarator: { - array: true, - object: true, - }, - }, + { VariableDeclarator: { array: true, object: true } }, ], 'prettier/prettier': 'error', 'node/no-unsupported-features/es-syntax': 'off', 'node/no-unsupported-features/es-builtins': 'error', + 'import/no-commonjs': 'error', + 'import/no-unused-modules': 'error', }, overrides: [ { files: ['*.test.js'], globals, }, + { + files: 'src/**/*', + parserOptions: { + sourceType: 'module', + }, + }, + { + files: ['.eslintrc.js', 'babel.config.js'], + rules: { + 'import/no-commonjs': 'off', + }, + }, ], }; diff --git a/package.json b/package.json index 531ea2f94..4a1b1b9da 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "eslint": "^5.1.0", "eslint-config-prettier": "^5.1.0", "eslint-plugin-eslint-plugin": "^2.0.0", + "eslint-plugin-import": "^2.18.0", "eslint-plugin-node": "^8.0.0", "eslint-plugin-prettier": "^3.0.0", "husky": "^1.0.1", diff --git a/src/__tests__/rules.test.js b/src/__tests__/rules.test.js index 47a359833..be159332f 100644 --- a/src/__tests__/rules.test.js +++ b/src/__tests__/rules.test.js @@ -1,8 +1,6 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const { rules } = require('../'); +import { existsSync } from 'fs'; +import { resolve } from 'path'; +import { rules } from '../'; const ruleNames = Object.keys(rules); const numberOfRules = 33; @@ -10,9 +8,9 @@ const numberOfRules = 33; describe('rules', () => { it('should have a corresponding doc for each rule', () => { ruleNames.forEach(rule => { - const docPath = path.resolve(__dirname, '../../docs/rules', `${rule}.md`); + const docPath = resolve(__dirname, '../../docs/rules', `${rule}.md`); - if (!fs.existsSync(docPath)) { + if (!existsSync(docPath)) { throw new Error( `Could not find documentation file for rule "${rule}" in path "${docPath}"`, ); diff --git a/src/index.js b/src/index.js index 31101b385..22c2b0c61 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,23 @@ -'use strict'; +import { readdirSync } from 'fs'; +import { basename, join } from 'path'; +import * as snapshotProcessor from './processors/snapshot-processor'; -const fs = require('fs'); -const path = require('path'); +// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606 +/* istanbul ignore next */ +function interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : { default: obj }; +} -const rules = fs - .readdirSync(path.join(__dirname, 'rules')) +function importDefault(moduleName) { + return interopRequireDefault(require(moduleName)).default; +} + +const rules = readdirSync(join(__dirname, 'rules')) .filter(rule => rule !== '__tests__' && rule !== 'util.js') - .map(rule => path.basename(rule, '.js')) + .map(rule => basename(rule, '.js')) .reduce( - (acc, curr) => Object.assign(acc, { [curr]: require(`./rules/${curr}`) }), + (acc, curr) => + Object.assign(acc, { [curr]: importDefault(`./rules/${curr}`) }), {}, ); let allRules = {}; @@ -16,8 +25,7 @@ Object.keys(rules).forEach(function(key) { allRules[`jest/${key}`] = 'error'; }); -const snapshotProcessor = require('./processors/snapshot-processor'); - +// eslint-disable-next-line import/no-commonjs module.exports = { configs: { all: { diff --git a/src/processors/__tests__/snapshot-processor.test.js b/src/processors/__tests__/snapshot-processor.test.js index fc76a7b98..787f6182d 100644 --- a/src/processors/__tests__/snapshot-processor.test.js +++ b/src/processors/__tests__/snapshot-processor.test.js @@ -1,6 +1,4 @@ -'use strict'; - -const snapshotProcessor = require('../snapshot-processor'); +import * as snapshotProcessor from '../snapshot-processor'; describe('snapshot-processor', () => { it('exports an object with preprocess and postprocess functions', () => { diff --git a/src/processors/snapshot-processor.js b/src/processors/snapshot-processor.js index 8e0ce9df1..f49843073 100644 --- a/src/processors/snapshot-processor.js +++ b/src/processors/snapshot-processor.js @@ -1,15 +1,8 @@ -'use strict'; - // https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins -const preprocess = source => [source]; +export const preprocess = source => [source]; -const postprocess = messages => +export const postprocess = messages => messages[0].filter( // snapshot files should only be linted with snapshot specific rules message => message.ruleId === 'jest/no-large-snapshots', ); - -module.exports = { - preprocess, - postprocess, -}; diff --git a/src/rules/__tests__/consistent-test-it.test.js b/src/rules/__tests__/consistent-test-it.test.js index 00b75e377..ca842d97a 100644 --- a/src/rules/__tests__/consistent-test-it.test.js +++ b/src/rules/__tests__/consistent-test-it.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../consistent-test-it'); +import { RuleTester } from 'eslint'; +import rule from '../consistent-test-it'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/expect-expect.test.js b/src/rules/__tests__/expect-expect.test.js index 53354c740..eec3be3cc 100644 --- a/src/rules/__tests__/expect-expect.test.js +++ b/src/rules/__tests__/expect-expect.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../expect-expect'); +import { RuleTester } from 'eslint'; +import rule from '../expect-expect'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/lowercase-name.test.js b/src/rules/__tests__/lowercase-name.test.js index fd57d0115..4a4ac9dc2 100644 --- a/src/rules/__tests__/lowercase-name.test.js +++ b/src/rules/__tests__/lowercase-name.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../lowercase-name'); +import { RuleTester } from 'eslint'; +import rule from '../lowercase-name'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-alias-methods.test.js b/src/rules/__tests__/no-alias-methods.test.js index d3024bc8a..7a36fd3ee 100644 --- a/src/rules/__tests__/no-alias-methods.test.js +++ b/src/rules/__tests__/no-alias-methods.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-alias-methods'); +import { RuleTester } from 'eslint'; +import rule from '../no-alias-methods'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/no-commented-out-tests.test.js b/src/rules/__tests__/no-commented-out-tests.test.js index 1e5171942..a69024203 100644 --- a/src/rules/__tests__/no-commented-out-tests.test.js +++ b/src/rules/__tests__/no-commented-out-tests.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-commented-out-tests'); +import { RuleTester } from 'eslint'; +import rule from '../no-commented-out-tests'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-disabled-tests.test.js b/src/rules/__tests__/no-disabled-tests.test.js index 39c858801..0ed3929a6 100644 --- a/src/rules/__tests__/no-disabled-tests.test.js +++ b/src/rules/__tests__/no-disabled-tests.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-disabled-tests'); +import { RuleTester } from 'eslint'; +import rule from '../no-disabled-tests'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-duplicate-hooks.test.js b/src/rules/__tests__/no-duplicate-hooks.test.js index bb7fe76dd..402ecff7b 100644 --- a/src/rules/__tests__/no-duplicate-hooks.test.js +++ b/src/rules/__tests__/no-duplicate-hooks.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-duplicate-hooks'); +import { RuleTester } from 'eslint'; +import rule from '../no-duplicate-hooks'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-empty-title.js b/src/rules/__tests__/no-empty-title.js index 8070c47a2..137d4de8a 100644 --- a/src/rules/__tests__/no-empty-title.js +++ b/src/rules/__tests__/no-empty-title.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-empty-title'); +import { RuleTester } from 'eslint'; +import rule from '../no-empty-title'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-focused-tests.test.js b/src/rules/__tests__/no-focused-tests.test.js index dc70ef6cf..45d030de5 100644 --- a/src/rules/__tests__/no-focused-tests.test.js +++ b/src/rules/__tests__/no-focused-tests.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-focused-tests'); +import { RuleTester } from 'eslint'; +import rule from '../no-focused-tests'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/no-hooks.test.js b/src/rules/__tests__/no-hooks.test.js index 23adb3347..d9d9ef6b9 100644 --- a/src/rules/__tests__/no-hooks.test.js +++ b/src/rules/__tests__/no-hooks.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-hooks'); +import { RuleTester } from 'eslint'; +import rule from '../no-hooks'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-identical-title.test.js b/src/rules/__tests__/no-identical-title.test.js index 6ff1ab518..21f1da2c4 100644 --- a/src/rules/__tests__/no-identical-title.test.js +++ b/src/rules/__tests__/no-identical-title.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-identical-title'); +import { RuleTester } from 'eslint'; +import rule from '../no-identical-title'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/no-jasmine-globals.test.js b/src/rules/__tests__/no-jasmine-globals.test.js index adbe06cd0..2d73156ca 100644 --- a/src/rules/__tests__/no-jasmine-globals.test.js +++ b/src/rules/__tests__/no-jasmine-globals.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-jasmine-globals'); +import { RuleTester } from 'eslint'; +import rule from '../no-jasmine-globals'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/no-jest-import.test.js b/src/rules/__tests__/no-jest-import.test.js index 9eb40ad93..428063493 100644 --- a/src/rules/__tests__/no-jest-import.test.js +++ b/src/rules/__tests__/no-jest-import.test.js @@ -1,7 +1,6 @@ -'use strict'; +import { RuleTester } from 'eslint'; +import rule from '../no-jest-import'; -const rule = require('../no-jest-import.js'); -const { RuleTester } = require('eslint'); const ruleTester = new RuleTester(); ruleTester.run('no-jest-import', rule, { diff --git a/src/rules/__tests__/no-large-snapshots.test.js b/src/rules/__tests__/no-large-snapshots.test.js index f8cd2c200..2afcc819b 100644 --- a/src/rules/__tests__/no-large-snapshots.test.js +++ b/src/rules/__tests__/no-large-snapshots.test.js @@ -1,15 +1,15 @@ -'use strict'; +import { RuleTester } from 'eslint'; +import { parse } from 'babel-eslint'; +import rule from '../no-large-snapshots'; -const { RuleTester } = require('eslint'); -const rule = require('../no-large-snapshots'); const noLargeSnapshots = rule.create; -const { parse } = require('babel-eslint'); const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, }, }); + // lines - 1 to account for the starting newline we always add. const generateSnapshotNode = ({ lines, title = 'a big component 1' }) => parse(`exports[\`${title}\`] = \`\n${'line\n'.repeat(lines - 1)}\`;`).body[0]; diff --git a/src/rules/__tests__/no-mocks-import.test.js b/src/rules/__tests__/no-mocks-import.test.js index ce888f060..6e064cd6f 100644 --- a/src/rules/__tests__/no-mocks-import.test.js +++ b/src/rules/__tests__/no-mocks-import.test.js @@ -1,7 +1,6 @@ -'use strict'; +import { RuleTester } from 'eslint'; +import rule from '../no-mocks-import'; -const rule = require('../no-mocks-import.js'); -const { RuleTester } = require('eslint'); const ruleTester = new RuleTester(); ruleTester.run('no-mocks-import', rule, { diff --git a/src/rules/__tests__/no-test-callback.test.js b/src/rules/__tests__/no-test-callback.test.js index 59bb6629a..70573537c 100644 --- a/src/rules/__tests__/no-test-callback.test.js +++ b/src/rules/__tests__/no-test-callback.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-test-callback'); +import { RuleTester } from 'eslint'; +import rule from '../no-test-callback'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/no-test-prefixes.test.js b/src/rules/__tests__/no-test-prefixes.test.js index 65b0521b1..c99cf269b 100644 --- a/src/rules/__tests__/no-test-prefixes.test.js +++ b/src/rules/__tests__/no-test-prefixes.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-test-prefixes'); +import { RuleTester } from 'eslint'; +import rule from '../no-test-prefixes'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/no-test-return-statement.test.js b/src/rules/__tests__/no-test-return-statement.test.js index c785d7349..451ef4391 100644 --- a/src/rules/__tests__/no-test-return-statement.test.js +++ b/src/rules/__tests__/no-test-return-statement.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-test-return-statement'); +import { RuleTester } from 'eslint'; +import rule from '../no-test-return-statement'; const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); diff --git a/src/rules/__tests__/no-truthy-falsy.test.js b/src/rules/__tests__/no-truthy-falsy.test.js index 2e9cca737..76d797741 100644 --- a/src/rules/__tests__/no-truthy-falsy.test.js +++ b/src/rules/__tests__/no-truthy-falsy.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../no-truthy-falsy'); +import { RuleTester } from 'eslint'; +import rule from '../no-truthy-falsy'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-called-with.js b/src/rules/__tests__/prefer-called-with.js index 73c753cca..91560f72d 100644 --- a/src/rules/__tests__/prefer-called-with.js +++ b/src/rules/__tests__/prefer-called-with.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-called-with'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-called-with'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-expect-assertions.test.js b/src/rules/__tests__/prefer-expect-assertions.test.js index f5b83907d..e179b7d64 100644 --- a/src/rules/__tests__/prefer-expect-assertions.test.js +++ b/src/rules/__tests__/prefer-expect-assertions.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-expect-assertions'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-expect-assertions'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/prefer-inline-snapshots.test.js b/src/rules/__tests__/prefer-inline-snapshots.test.js index 066b24055..5b4069720 100644 --- a/src/rules/__tests__/prefer-inline-snapshots.test.js +++ b/src/rules/__tests__/prefer-inline-snapshots.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-inline-snapshots'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-inline-snapshots'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-spy-on.test.js b/src/rules/__tests__/prefer-spy-on.test.js index 3d5868e2c..44692e335 100644 --- a/src/rules/__tests__/prefer-spy-on.test.js +++ b/src/rules/__tests__/prefer-spy-on.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-spy-on'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-spy-on'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/prefer-strict-equal.test.js b/src/rules/__tests__/prefer-strict-equal.test.js index 42da93352..ff5d874b4 100644 --- a/src/rules/__tests__/prefer-strict-equal.test.js +++ b/src/rules/__tests__/prefer-strict-equal.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-strict-equal'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-strict-equal'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-to-be-null.test.js b/src/rules/__tests__/prefer-to-be-null.test.js index f8501d533..43fb7f033 100644 --- a/src/rules/__tests__/prefer-to-be-null.test.js +++ b/src/rules/__tests__/prefer-to-be-null.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-to-be-null'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-to-be-null'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-to-be-undefined.test.js b/src/rules/__tests__/prefer-to-be-undefined.test.js index 18526411c..622c408fe 100644 --- a/src/rules/__tests__/prefer-to-be-undefined.test.js +++ b/src/rules/__tests__/prefer-to-be-undefined.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-to-be-undefined'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-to-be-undefined'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-to-contain.test.js b/src/rules/__tests__/prefer-to-contain.test.js index d172fb9ee..a67444878 100644 --- a/src/rules/__tests__/prefer-to-contain.test.js +++ b/src/rules/__tests__/prefer-to-contain.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-to-contain'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-to-contain'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-to-have-length.test.js b/src/rules/__tests__/prefer-to-have-length.test.js index 184a6ce1d..c5be836ba 100644 --- a/src/rules/__tests__/prefer-to-have-length.test.js +++ b/src/rules/__tests__/prefer-to-have-length.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-to-have-length'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-to-have-length'; const ruleTester = new RuleTester(); diff --git a/src/rules/__tests__/prefer-todo.test.js b/src/rules/__tests__/prefer-todo.test.js index 5da4167a9..8dc087b33 100644 --- a/src/rules/__tests__/prefer-todo.test.js +++ b/src/rules/__tests__/prefer-todo.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../prefer-todo'); +import { RuleTester } from 'eslint'; +import rule from '../prefer-todo'; const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 }, diff --git a/src/rules/__tests__/require-tothrow-message.test.js b/src/rules/__tests__/require-tothrow-message.test.js index 3bee910c1..25c6fe58c 100644 --- a/src/rules/__tests__/require-tothrow-message.test.js +++ b/src/rules/__tests__/require-tothrow-message.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../require-tothrow-message'); +import { RuleTester } from 'eslint'; +import rule from '../require-tothrow-message'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/valid-describe.test.js b/src/rules/__tests__/valid-describe.test.js index eb980adbd..211d1537b 100644 --- a/src/rules/__tests__/valid-describe.test.js +++ b/src/rules/__tests__/valid-describe.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../valid-describe'); +import { RuleTester } from 'eslint'; +import rule from '../valid-describe'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/valid-expect-in-promise.test.js b/src/rules/__tests__/valid-expect-in-promise.test.js index d9159b198..77ab6dca6 100644 --- a/src/rules/__tests__/valid-expect-in-promise.test.js +++ b/src/rules/__tests__/valid-expect-in-promise.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../valid-expect-in-promise'); +import { RuleTester } from 'eslint'; +import rule from '../valid-expect-in-promise'; const ruleTester = new RuleTester({ parserOptions: { diff --git a/src/rules/__tests__/valid-expect.test.js b/src/rules/__tests__/valid-expect.test.js index 66922edec..8b3192375 100644 --- a/src/rules/__tests__/valid-expect.test.js +++ b/src/rules/__tests__/valid-expect.test.js @@ -1,7 +1,5 @@ -'use strict'; - -const { RuleTester } = require('eslint'); -const rule = require('../valid-expect'); +import { RuleTester } from 'eslint'; +import rule from '../valid-expect'; const ruleTester = new RuleTester(); diff --git a/src/rules/consistent-test-it.js b/src/rules/consistent-test-it.js index 5c93efc48..94a52f717 100644 --- a/src/rules/consistent-test-it.js +++ b/src/rules/consistent-test-it.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, getNodeName, isDescribe, isTestCase } from './util'; -const { getDocsUrl, getNodeName, isTestCase, isDescribe } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/expect-expect.js b/src/rules/expect-expect.js index 328d437e6..b35bf7c22 100644 --- a/src/rules/expect-expect.js +++ b/src/rules/expect-expect.js @@ -1,13 +1,11 @@ -'use strict'; - /* * This implementation is adapted from eslint-plugin-jasmine. * MIT license, Remco Haszing. */ -const { getDocsUrl, getNodeName } = require('./util'); +import { getDocsUrl, getNodeName } from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/lowercase-name.js b/src/rules/lowercase-name.js index 7f40e7dc0..0ff7a5c54 100644 --- a/src/rules/lowercase-name.js +++ b/src/rules/lowercase-name.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; const isItTestOrDescribeFunction = node => { return ( @@ -47,7 +45,7 @@ const descriptionBeginsWithLowerCase = node => { return false; }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-alias-methods.js b/src/rules/no-alias-methods.js index c6763eeaa..b27d2ed7c 100644 --- a/src/rules/no-alias-methods.js +++ b/src/rules/no-alias-methods.js @@ -1,8 +1,6 @@ -'use strict'; +import { expectCase, getDocsUrl, method } from './util'; -const { expectCase, getDocsUrl, method } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-commented-out-tests.js b/src/rules/no-commented-out-tests.js index 03fcaf4f2..f5b3132ee 100644 --- a/src/rules/no-commented-out-tests.js +++ b/src/rules/no-commented-out-tests.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; function hasTests(node) { return /^\s*(x|f)?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/m.test( @@ -8,7 +6,7 @@ function hasTests(node) { ); } -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-disabled-tests.js b/src/rules/no-disabled-tests.js index 4a6d30361..38fa7ec80 100644 --- a/src/rules/no-disabled-tests.js +++ b/src/rules/no-disabled-tests.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, getNodeName, scopeHasLocalReference } from './util'; -const { getDocsUrl, getNodeName, scopeHasLocalReference } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-duplicate-hooks.js b/src/rules/no-duplicate-hooks.js index 2bdf04007..9ec7b7383 100644 --- a/src/rules/no-duplicate-hooks.js +++ b/src/rules/no-duplicate-hooks.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl, isDescribe, isHook } = require('./util'); +import { getDocsUrl, isDescribe, isHook } from './util'; const newHookContext = () => ({ beforeAll: 0, @@ -9,7 +7,7 @@ const newHookContext = () => ({ afterEach: 0, }); -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-empty-title.js b/src/rules/no-empty-title.js index 601eb55da..b8b2a41d2 100644 --- a/src/rules/no-empty-title.js +++ b/src/rules/no-empty-title.js @@ -1,16 +1,14 @@ -'use strict'; - -const { +import { getDocsUrl, + getStringValue, hasExpressions, isDescribe, - isTestCase, - isTemplateLiteral, isString, - getStringValue, -} = require('./util'); + isTemplateLiteral, + isTestCase, +} from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-focused-tests.js b/src/rules/no-focused-tests.js index c3fadf6f7..96d982a2c 100644 --- a/src/rules/no-focused-tests.js +++ b/src/rules/no-focused-tests.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; const testFunctions = new Set(['describe', 'it', 'test']); @@ -17,7 +15,7 @@ const isPropertyNamedOnly = property => const isCallToTestOnlyFunction = callee => matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property); -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-hooks.js b/src/rules/no-hooks.js index f73f7af86..29dca2df1 100644 --- a/src/rules/no-hooks.js +++ b/src/rules/no-hooks.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, isHook } from './util'; -const { getDocsUrl, isHook } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-identical-title.js b/src/rules/no-identical-title.js index 9bfab51db..832b9ed36 100644 --- a/src/rules/no-identical-title.js +++ b/src/rules/no-identical-title.js @@ -1,13 +1,11 @@ -'use strict'; - -const { +import { getDocsUrl, + getStringValue, + hasExpressions, isDescribe, - isTestCase, isString, - hasExpressions, - getStringValue, -} = require('./util'); + isTestCase, +} from './util'; const newDescribeContext = () => ({ describeTitles: [], @@ -43,7 +41,7 @@ const isFirstArgValid = arg => { return true; }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-jasmine-globals.js b/src/rules/no-jasmine-globals.js index d4c1005f3..e3a617dce 100644 --- a/src/rules/no-jasmine-globals.js +++ b/src/rules/no-jasmine-globals.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, getNodeName, scopeHasLocalReference } from './util'; -const { getDocsUrl, getNodeName, scopeHasLocalReference } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-jest-import.js b/src/rules/no-jest-import.js index 7cdd81e7c..da983377b 100644 --- a/src/rules/no-jest-import.js +++ b/src/rules/no-jest-import.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl } from './util'; -const { getDocsUrl } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-large-snapshots.js b/src/rules/no-large-snapshots.js index 64e0a7d55..dd06edbd4 100644 --- a/src/rules/no-large-snapshots.js +++ b/src/rules/no-large-snapshots.js @@ -1,8 +1,5 @@ -'use strict'; - -const { getDocsUrl, getStringValue } = require('./util'); - -const path = require('path'); +import { isAbsolute } from 'path'; +import { getDocsUrl, getStringValue } from './util'; const reportOnViolation = (context, node) => { const lineLimit = @@ -18,7 +15,7 @@ const reportOnViolation = (context, node) => { context.options[0].whitelistedSnapshots; const allPathsAreAbsolute = Object.keys(whitelistedSnapshots || {}).every( - path.isAbsolute, + isAbsolute, ); if (!allPathsAreAbsolute) { @@ -54,7 +51,7 @@ const reportOnViolation = (context, node) => { } }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-mocks-import.js b/src/rules/no-mocks-import.js index 385d37493..923ea4b26 100644 --- a/src/rules/no-mocks-import.js +++ b/src/rules/no-mocks-import.js @@ -1,13 +1,12 @@ -'use strict'; +import { posix } from 'path'; -const { posix } = require('path'); -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; const mocksDirName = '__mocks__'; const isMockPath = path => path.split(posix.sep).includes(mocksDirName); -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-test-callback.js b/src/rules/no-test-callback.js index aa5abcac2..3370c9ed7 100644 --- a/src/rules/no-test-callback.js +++ b/src/rules/no-test-callback.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, isTestCase } from './util'; -const { getDocsUrl, isTestCase } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-test-prefixes.js b/src/rules/no-test-prefixes.js index 240989da3..acb0bd3db 100644 --- a/src/rules/no-test-prefixes.js +++ b/src/rules/no-test-prefixes.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl, getNodeName, isDescribe, isTestCase } from './util'; -const { getDocsUrl, getNodeName, isTestCase, isDescribe } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-test-return-statement.js b/src/rules/no-test-return-statement.js index 06329442e..33201b454 100644 --- a/src/rules/no-test-return-statement.js +++ b/src/rules/no-test-return-statement.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl, isFunction, isTestCase } = require('./util'); +import { getDocsUrl, isFunction, isTestCase } from './util'; const RETURN_STATEMENT = 'ReturnStatement'; const BLOCK_STATEMENT = 'BlockStatement'; @@ -16,7 +14,7 @@ const getBody = args => { return []; }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/no-truthy-falsy.js b/src/rules/no-truthy-falsy.js index 2c5feb157..4fcbe2df0 100644 --- a/src/rules/no-truthy-falsy.js +++ b/src/rules/no-truthy-falsy.js @@ -1,15 +1,13 @@ -'use strict'; - -const { - getDocsUrl, +import { expectCase, expectNotCase, - expectResolveCase, expectRejectCase, + expectResolveCase, + getDocsUrl, method, -} = require('./util'); +} from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-called-with.js b/src/rules/prefer-called-with.js index b59b53e0a..182375d87 100644 --- a/src/rules/prefer-called-with.js +++ b/src/rules/prefer-called-with.js @@ -1,8 +1,6 @@ -'use strict'; +import { expectCase, expectNotCase, getDocsUrl, method } from './util'; -const { getDocsUrl, expectCase, expectNotCase, method } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-expect-assertions.js b/src/rules/prefer-expect-assertions.js index 7b4a20d21..701bef6d7 100644 --- a/src/rules/prefer-expect-assertions.js +++ b/src/rules/prefer-expect-assertions.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; const validateArguments = expression => { return ( @@ -40,7 +38,7 @@ const reportMsg = (context, node) => { context.report({ messageId: 'haveExpectAssertions', node }); }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-inline-snapshots.js b/src/rules/prefer-inline-snapshots.js index b674ab3a4..33112fc5d 100644 --- a/src/rules/prefer-inline-snapshots.js +++ b/src/rules/prefer-inline-snapshots.js @@ -1,8 +1,6 @@ -'use strict'; +import { getDocsUrl } from './util'; -const { getDocsUrl } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-spy-on.js b/src/rules/prefer-spy-on.js index 6d452ab76..0b5c2f23b 100644 --- a/src/rules/prefer-spy-on.js +++ b/src/rules/prefer-spy-on.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl, getNodeName } = require('./util'); +import { getDocsUrl, getNodeName } from './util'; const getJestFnCall = node => { if ( @@ -22,7 +20,7 @@ const getJestFnCall = node => { return getJestFnCall(obj); }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-strict-equal.js b/src/rules/prefer-strict-equal.js index 73b891af0..6aaa23ea6 100644 --- a/src/rules/prefer-strict-equal.js +++ b/src/rules/prefer-strict-equal.js @@ -1,8 +1,6 @@ -'use strict'; +import { expectCase, getDocsUrl, method } from './util'; -const { expectCase, getDocsUrl, method } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-to-be-null.js b/src/rules/prefer-to-be-null.js index 313e3252b..23e453aa7 100644 --- a/src/rules/prefer-to-be-null.js +++ b/src/rules/prefer-to-be-null.js @@ -1,18 +1,16 @@ -'use strict'; - -const { - getDocsUrl, +import { argument, argument2, + expectNotToBeCase, + expectNotToEqualCase, expectToBeCase, expectToEqualCase, - expectNotToEqualCase, - expectNotToBeCase, + getDocsUrl, method, method2, -} = require('./util'); +} from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-to-be-undefined.js b/src/rules/prefer-to-be-undefined.js index f379ca460..1aff3f98e 100644 --- a/src/rules/prefer-to-be-undefined.js +++ b/src/rules/prefer-to-be-undefined.js @@ -1,18 +1,16 @@ -'use strict'; - -const { +import { argument, argument2, - expectToBeCase, expectNotToBeCase, - expectToEqualCase, expectNotToEqualCase, + expectToBeCase, + expectToEqualCase, getDocsUrl, method, method2, -} = require('./util'); +} from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-to-contain.js b/src/rules/prefer-to-contain.js index 1e65d2b82..205a81d7c 100644 --- a/src/rules/prefer-to-contain.js +++ b/src/rules/prefer-to-contain.js @@ -1,13 +1,11 @@ -'use strict'; - -const { - getDocsUrl, +import { + argument, expectCase, - expectResolveCase, expectRejectCase, + expectResolveCase, + getDocsUrl, method, - argument, -} = require('./util'); +} from './util'; const isEqualityCheck = node => method(node) && @@ -78,7 +76,7 @@ const getCommonFixes = (node, sourceCode, fixer) => { ]; }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-to-have-length.js b/src/rules/prefer-to-have-length.js index 2270a2b5d..905b800b8 100644 --- a/src/rules/prefer-to-have-length.js +++ b/src/rules/prefer-to-have-length.js @@ -1,15 +1,13 @@ -'use strict'; - -const { - getDocsUrl, +import { expectCase, expectNotCase, - expectResolveCase, expectRejectCase, + expectResolveCase, + getDocsUrl, method, -} = require('./util'); +} from './util'; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/prefer-todo.js b/src/rules/prefer-todo.js index 726794e34..48f466566 100644 --- a/src/rules/prefer-todo.js +++ b/src/rules/prefer-todo.js @@ -1,12 +1,10 @@ -'use strict'; - -const { - getDocsUrl, - isFunction, +import { composeFixers, + getDocsUrl, getNodeName, + isFunction, isString, -} = require('./util'); +} from './util'; function isOnlyTestTitle(node) { return node.arguments.length === 1; @@ -41,7 +39,7 @@ const isTestCase = node => node.type === 'CallExpression' && ['it', 'test', 'it.skip', 'test.skip'].includes(getNodeName(node.callee)); -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/require-tothrow-message.js b/src/rules/require-tothrow-message.js index 3e409cd5a..0ae027775 100644 --- a/src/rules/require-tothrow-message.js +++ b/src/rules/require-tothrow-message.js @@ -1,8 +1,6 @@ -'use strict'; +import { argument, expectCase, getDocsUrl, method } from './util'; -const { argument, expectCase, getDocsUrl, method } = require('./util'); - -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/util.js b/src/rules/util.js index 53f214a3d..11aa18a34 100644 --- a/src/rules/util.js +++ b/src/rules/util.js @@ -1,33 +1,31 @@ -'use strict'; - -const path = require('path'); -const { version } = require('../../package.json'); +import { basename } from 'path'; +import { version } from '../../package.json'; const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest'; -const expectCase = node => +export const expectCase = node => node.callee.name === 'expect' && node.arguments.length === 1 && node.parent && node.parent.type === 'MemberExpression' && node.parent.parent; -const expectNotCase = node => +export const expectNotCase = node => expectCase(node) && node.parent.parent.type === 'MemberExpression' && methodName(node) === 'not'; -const expectResolveCase = node => +export const expectResolveCase = node => expectCase(node) && node.parent.parent.type === 'MemberExpression' && methodName(node) === 'resolve'; -const expectRejectCase = node => +export const expectRejectCase = node => expectCase(node) && node.parent.parent.type === 'MemberExpression' && methodName(node) === 'reject'; -const expectToBeCase = (node, arg) => +export const expectToBeCase = (node, arg) => !(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) && expectCase(node) && methodName(node) === 'toBe' && @@ -37,7 +35,7 @@ const expectToBeCase = (node, arg) => arg === null) || (argument(node).name === 'undefined' && arg === undefined)); -const expectNotToBeCase = (node, arg) => +export const expectNotToBeCase = (node, arg) => expectNotCase(node) && methodName2(node) === 'toBe' && argument2(node) && @@ -46,7 +44,7 @@ const expectNotToBeCase = (node, arg) => arg === null) || (argument2(node).name === 'undefined' && arg === undefined)); -const expectToEqualCase = (node, arg) => +export const expectToEqualCase = (node, arg) => !(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) && expectCase(node) && methodName(node) === 'toEqual' && @@ -56,7 +54,7 @@ const expectToEqualCase = (node, arg) => arg === null) || (argument(node).name === 'undefined' && arg === undefined)); -const expectNotToEqualCase = (node, arg) => +export const expectNotToEqualCase = (node, arg) => expectNotCase(node) && methodName2(node) === 'toEqual' && argument2(node) && @@ -65,18 +63,18 @@ const expectNotToEqualCase = (node, arg) => arg === null) || (argument2(node).name === 'undefined' && arg === undefined)); -const method = node => node.parent.property; +export const method = node => node.parent.property; -const method2 = node => node.parent.parent.property; +export const method2 = node => node.parent.parent.property; const methodName = node => method(node).name; const methodName2 = node => method2(node).name; -const argument = node => +export const argument = node => node.parent.parent.arguments && node.parent.parent.arguments[0]; -const argument2 = node => +export const argument2 = node => node.parent.parent.parent.arguments && node.parent.parent.parent.arguments[0]; const describeAliases = new Set([ @@ -106,7 +104,7 @@ const testHookNames = new Set([ 'afterEach', ]); -const getNodeName = node => { +export const getNodeName = node => { function joinNames(a, b) { return a && b ? `${a}.${b}` : null; } @@ -126,37 +124,38 @@ const getNodeName = node => { return null; }; -const isHook = node => +export const isHook = node => node && node.type === 'CallExpression' && testHookNames.has(getNodeName(node.callee)); -const isTestCase = node => +export const isTestCase = node => node && node.type === 'CallExpression' && testCaseNames.has(getNodeName(node.callee)); -const isDescribe = node => +export const isDescribe = node => node && node.type === 'CallExpression' && describeAliases.has(getNodeName(node.callee)); -const isFunction = node => +export const isFunction = node => node && (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression'); -const isString = node => +export const isString = node => node && ((node.type === 'Literal' && typeof node.value === 'string') || isTemplateLiteral(node)); -const isTemplateLiteral = node => node && node.type === 'TemplateLiteral'; +export const isTemplateLiteral = node => + node && node.type === 'TemplateLiteral'; -const hasExpressions = node => +export const hasExpressions = node => node && node.expressions && node.expressions.length > 0; -const getStringValue = arg => +export const getStringValue = arg => isTemplateLiteral(arg) ? arg.quasis[0].value.raw : arg.value; /** @@ -167,8 +166,8 @@ const getStringValue = arg => * @param {string} filename - Name of the eslint rule * @returns {string} URL to the documentation for the given rule */ -const getDocsUrl = filename => { - const ruleName = path.basename(filename, '.js'); +export const getDocsUrl = filename => { + const ruleName = basename(filename, '.js'); return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`; }; @@ -200,7 +199,7 @@ const collectReferences = scope => { return { locals, unresolved }; }; -const scopeHasLocalReference = (scope, referenceName) => { +export const scopeHasLocalReference = (scope, referenceName) => { const references = collectReferences(scope); return ( // referenceName was found as a local variable or function declaration. @@ -211,37 +210,10 @@ const scopeHasLocalReference = (scope, referenceName) => { ); }; -function composeFixers(node) { +export function composeFixers(node) { return (...fixers) => { return fixerApi => { return fixers.reduce((all, fixer) => [...all, fixer(node, fixerApi)], []); }; }; } - -module.exports = { - method, - method2, - argument, - argument2, - expectCase, - expectNotCase, - expectResolveCase, - expectRejectCase, - expectToBeCase, - expectNotToBeCase, - expectToEqualCase, - expectNotToEqualCase, - getNodeName, - getStringValue, - isDescribe, - isFunction, - isHook, - isTemplateLiteral, - isTestCase, - isString, - hasExpressions, - getDocsUrl, - scopeHasLocalReference, - composeFixers, -}; diff --git a/src/rules/valid-describe.js b/src/rules/valid-describe.js index 737bc9528..0fa9c7cdd 100644 --- a/src/rules/valid-describe.js +++ b/src/rules/valid-describe.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl, isDescribe, isFunction } = require('./util'); +import { getDocsUrl, isDescribe, isFunction } from './util'; const isAsync = node => node.async; @@ -25,7 +23,7 @@ const paramsLocation = params => { }; }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/valid-expect-in-promise.js b/src/rules/valid-expect-in-promise.js index c77be4949..202618b7b 100644 --- a/src/rules/valid-expect-in-promise.js +++ b/src/rules/valid-expect-in-promise.js @@ -1,6 +1,4 @@ -'use strict'; - -const { getDocsUrl, isFunction } = require('./util'); +import { getDocsUrl, isFunction } from './util'; const isThenOrCatch = node => { return ( @@ -122,7 +120,7 @@ const isHavingAsyncCallBackParam = testFunction => { } }; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/src/rules/valid-expect.js b/src/rules/valid-expect.js index 1d017adce..21d5fb30e 100644 --- a/src/rules/valid-expect.js +++ b/src/rules/valid-expect.js @@ -1,15 +1,13 @@ -'use strict'; - /* * This implementation is ported from from eslint-plugin-jasmine. * MIT license, Tom Vincent. */ -const { getDocsUrl } = require('./util'); +import { getDocsUrl } from './util'; const expectProperties = ['not', 'resolves', 'rejects']; -module.exports = { +export default { meta: { docs: { url: getDocsUrl(__filename), diff --git a/yarn.lock b/yarn.lock index 63d74c346..9214f4cbf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1188,6 +1188,14 @@ array-ify@^1.0.0: resolved "https://registry.yarnpkg.com/array-ify/-/array-ify-1.0.0.tgz#9e528762b4a9066ad163a6962a364418e9626ece" integrity sha1-nlKHYrSpBmrRY6aWKjZEGOlibs4= +array-includes@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.0.3.tgz#184b48f62d92d7452bb31b323165c7f8bd02266d" + integrity sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0= + dependencies: + define-properties "^1.1.2" + es-abstract "^1.7.0" + array-union@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/array-union/-/array-union-1.0.2.tgz#9a34410e4f4e3da23dea375be5be70f24778ec39" @@ -1689,6 +1697,11 @@ console-control-strings@^1.0.0, console-control-strings@~1.1.0: resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" integrity sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4= +contains-path@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a" + integrity sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo= + conventional-changelog-angular@^1.3.3: version "1.6.6" resolved "https://registry.yarnpkg.com/conventional-changelog-angular/-/conventional-changelog-angular-1.6.6.tgz#b27f2b315c16d0a1f23eb181309d0e6a4698ea0f" @@ -1833,7 +1846,7 @@ date-fns@^1.27.2: resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c" integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw== -debug@^2.2.0, debug@^2.3.3: +debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== @@ -1953,6 +1966,14 @@ diff-sequences@^24.3.0: resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-24.3.0.tgz#0f20e8a1df1abddaf4d9c226680952e64118b975" integrity sha512-xLqpez+Zj9GKSnPWS0WZw1igGocZ+uua8+y+5dDNTT934N3QuY1sp2LkHzwiaYQGz60hMq0pjAshdeXm5VUOEw== +doctrine@1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa" + integrity sha1-N53Ocw9hZvds76TmcHoVmwLFpvo= + dependencies: + esutils "^2.0.2" + isarray "^1.0.0" + doctrine@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-3.0.0.tgz#addebead72a6574db783639dc87a121773973961" @@ -2011,7 +2032,7 @@ error-ex@^1.2.0, error-ex@^1.3.1: dependencies: is-arrayish "^0.2.1" -es-abstract@^1.5.1: +es-abstract@^1.5.1, es-abstract@^1.7.0: version "1.13.0" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.13.0.tgz#ac86145fdd5099d8dd49558ccba2eaf9b88e24e9" integrity sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg== @@ -2067,6 +2088,22 @@ eslint-formatter-pretty@^1.3.0: plur "^2.1.2" string-width "^2.0.0" +eslint-import-resolver-node@^0.3.2: + version "0.3.2" + resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.2.tgz#58f15fb839b8d0576ca980413476aab2472db66a" + integrity sha512-sfmTqJfPSizWu4aymbPr4Iidp5yKm8yDkHp+Ir3YiTHiiDfxh69mOUsmiqW6RZ9zRXFaF64GtYmN7e+8GHBv6Q== + dependencies: + debug "^2.6.9" + resolve "^1.5.0" + +eslint-module-utils@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.4.0.tgz#8b93499e9b00eab80ccb6614e69f03678e84e09a" + integrity sha512-14tltLm38Eu3zS+mt0KvILC3q8jyIAH518MlG+HO0p+yK885Lb1UHTY/UgR91eOyGdmxAPb+OLoW4znqIT6Ndw== + dependencies: + debug "^2.6.8" + pkg-dir "^2.0.0" + eslint-plugin-es@^1.3.1: version "1.4.0" resolved "https://registry.yarnpkg.com/eslint-plugin-es/-/eslint-plugin-es-1.4.0.tgz#475f65bb20c993fc10e8c8fe77d1d60068072da6" @@ -2080,6 +2117,23 @@ eslint-plugin-eslint-plugin@^2.0.0: resolved "https://registry.yarnpkg.com/eslint-plugin-eslint-plugin/-/eslint-plugin-eslint-plugin-2.1.0.tgz#a7a00f15a886957d855feacaafee264f039e62d5" integrity sha512-kT3A/ZJftt28gbl/Cv04qezb/NQ1dwYIbi8lyf806XMxkus7DvOVCLIfTXMrorp322Pnoez7+zabXH29tADIDg== +eslint-plugin-import@^2.18.0: + version "2.18.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.18.0.tgz#7a5ba8d32622fb35eb9c8db195c2090bd18a3678" + integrity sha512-PZpAEC4gj/6DEMMoU2Df01C5c50r7zdGIN52Yfi7CvvWaYssG7Jt5R9nFG5gmqodxNOz9vQS87xk6Izdtpdrig== + dependencies: + array-includes "^3.0.3" + contains-path "^0.1.0" + debug "^2.6.9" + doctrine "1.5.0" + eslint-import-resolver-node "^0.3.2" + eslint-module-utils "^2.4.0" + has "^1.0.3" + lodash "^4.17.11" + minimatch "^3.0.4" + read-pkg-up "^2.0.0" + resolve "^1.11.0" + eslint-plugin-node@^8.0.0: version "8.0.1" resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-8.0.1.tgz#55ae3560022863d141fa7a11799532340a685964" @@ -3104,7 +3158,7 @@ is-wsl@^1.1.0: resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d" integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0= -isarray@1.0.0, isarray@~1.0.0: +isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= @@ -3799,6 +3853,16 @@ load-json-file@^1.0.0: pinkie-promise "^2.0.0" strip-bom "^2.0.0" +load-json-file@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-2.0.0.tgz#7947e42149af80d696cbf797bcaabcfe1fe29ca8" + integrity sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg= + dependencies: + graceful-fs "^4.1.2" + parse-json "^2.2.0" + pify "^2.0.0" + strip-bom "^3.0.0" + load-json-file@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-4.0.0.tgz#2f5f45ab91e33216234fd53adab668eb4ec0993b" @@ -4627,6 +4691,13 @@ path-type@^1.0.0: pify "^2.0.0" pinkie-promise "^2.0.0" +path-type@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/path-type/-/path-type-2.0.0.tgz#f012ccb8415b7096fc2daa1054c3d72389594c73" + integrity sha1-8BLMuEFbcJb8LaoQVMPXI4lZTHM= + dependencies: + pify "^2.0.0" + path-type@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/path-type/-/path-type-3.0.0.tgz#cef31dc8e0a1a3bb0d105c0cd97cf3bf47f4e36f" @@ -4673,6 +4744,13 @@ pirates@^4.0.1: dependencies: node-modules-regexp "^1.0.0" +pkg-dir@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b" + integrity sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s= + dependencies: + find-up "^2.1.0" + pkg-dir@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-3.0.0.tgz#2749020f239ed990881b1f71210d51eb6523bea3" @@ -4833,6 +4911,14 @@ read-pkg-up@^1.0.1: find-up "^1.0.0" read-pkg "^1.0.0" +read-pkg-up@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be" + integrity sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4= + dependencies: + find-up "^2.0.0" + read-pkg "^2.0.0" + read-pkg-up@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-3.0.0.tgz#3ed496685dba0f8fe118d0691dc51f4a1ff96f07" @@ -4858,6 +4944,15 @@ read-pkg@^1.0.0: normalize-package-data "^2.3.2" path-type "^1.0.0" +read-pkg@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-2.0.0.tgz#8ef1c0623c6a6db0dc6713c4bfac46332b2368f8" + integrity sha1-jvHAYjxqbbDcZxPEv6xGMysjaPg= + dependencies: + load-json-file "^2.0.0" + normalize-package-data "^2.3.2" + path-type "^2.0.0" + read-pkg@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-3.0.0.tgz#9cbc686978fee65d16c00e2b19c237fcf6e38389" @@ -5128,7 +5223,7 @@ resolve@1.1.7: resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs= -resolve@^1.10.0, resolve@^1.3.2, resolve@^1.8.1: +resolve@^1.10.0, resolve@^1.11.0, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.8.1: version "1.11.1" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.11.1.tgz#ea10d8110376982fef578df8fc30b9ac30a07a3e" integrity sha512-vIpgF6wfuJOZI7KKKSP+HmiKggadPQAdsp5HiC1mvqnfp0gF1vdwgBWZIdrVft9pgqoMFQN+R7BSWZiBxx+BBw==