diff --git a/package.json b/package.json index ccf686a01..18b65c846 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,8 @@ "@types/eslint": "^4.16.6", "@types/jest": "^24.0.12", "@types/node": "^12.0.0", - "@typescript-eslint/eslint-plugin": "^1.9.0", - "@typescript-eslint/experimental-utils": "^1.9.0", + "@typescript-eslint/eslint-plugin": "^1.9.1-alpha.3", + "@typescript-eslint/experimental-utils": "^1.9.1-alpha.3", "babel-jest": "^24.8.0", "eslint": "^5.1.0", "eslint-config-prettier": "^4.1.0", diff --git a/src/rules/__tests__/no-jest-import.test.js b/src/rules/__tests__/no-jest-import.test.ts similarity index 64% rename from src/rules/__tests__/no-jest-import.test.js rename to src/rules/__tests__/no-jest-import.test.ts index bc21192dd..4267b7612 100644 --- a/src/rules/__tests__/no-jest-import.test.js +++ b/src/rules/__tests__/no-jest-import.test.ts @@ -1,9 +1,15 @@ -'use strict'; +import { RuleTester as ESLintRuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; +import rule from '../no-jest-import'; -const rule = require('../no-jest-import.js'); -const { RuleTester } = require('eslint'); -const ruleTester = new RuleTester(); -const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`; +const RuleTester: TSESLint.RuleTester = ESLintRuleTester as any; + +const ruleTester = new RuleTester({ + parser: '@typescript-eslint/parser', + parserOptions: { + ecmaVersion: 6, + }, +}); ruleTester.run('no-jest-import', rule, { valid: [ @@ -22,7 +28,7 @@ ruleTester.run('no-jest-import', rule, { { endColumn: 15, column: 9, - message, + messageId: 'unexpectedImport', }, ], }, @@ -32,8 +38,8 @@ ruleTester.run('no-jest-import', rule, { errors: [ { endColumn: 24, - column: 1, - message, + column: 18, + messageId: 'unexpectedImport', }, ], }, @@ -43,7 +49,7 @@ ruleTester.run('no-jest-import', rule, { { endColumn: 26, column: 20, - message, + messageId: 'unexpectedImport', }, ], }, @@ -53,8 +59,8 @@ ruleTester.run('no-jest-import', rule, { errors: [ { endColumn: 34, - column: 1, - message, + column: 28, + messageId: 'unexpectedImport', }, ], }, @@ -65,7 +71,7 @@ ruleTester.run('no-jest-import', rule, { { endColumn: 28, column: 22, - message, + messageId: 'unexpectedImport', }, ], }, diff --git a/src/rules/no-jest-import.js b/src/rules/no-jest-import.js deleted file mode 100644 index 139d4fb7d..000000000 --- a/src/rules/no-jest-import.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -const { getDocsUrl } = require('./util'); - -const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`; - -module.exports = { - meta: { - docs: { - url: getDocsUrl(__filename), - }, - }, - create(context) { - return { - 'ImportDeclaration[source.value="jest"]'(node) { - context.report({ node, message }); - }, - 'CallExpression[callee.name="require"][arguments.0.value="jest"]'(node) { - context.report({ - loc: node.arguments[0].loc, - message, - }); - }, - }; - }, -}; diff --git a/src/rules/no-jest-import.ts b/src/rules/no-jest-import.ts new file mode 100644 index 000000000..c244dbdd8 --- /dev/null +++ b/src/rules/no-jest-import.ts @@ -0,0 +1,37 @@ +import { TSESTree } from '@typescript-eslint/experimental-utils'; +import { createRule } from './tsUtils'; + +export default createRule({ + name: __filename, + meta: { + type: 'problem', + docs: { + description: + "The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. It is therefore completely unnecessary to import in `jest`, as Jest doesn't export anything in the first place.", + category: 'Best Practices', + recommended: 'error', + }, + messages: { + unexpectedImport: `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`, + }, + schema: [], + }, + defaultOptions: [], + create(context) { + return { + 'ImportDeclaration[source.value="jest"]'( + node: TSESTree.ImportDeclaration, + ) { + context.report({ node: node.source, messageId: 'unexpectedImport' }); + }, + 'CallExpression[callee.name="require"][arguments.0.value="jest"]'( + node: TSESTree.CallExpression, + ) { + context.report({ + node: node.arguments[0], + messageId: 'unexpectedImport', + }); + }, + }; + }, +}); diff --git a/yarn.lock b/yarn.lock index 3902a7a33..ebc9c7015 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1064,40 +1064,40 @@ resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.12.tgz#45dd1d0638e8c8f153e87d296907659296873916" integrity sha512-SOhuU4wNBxhhTHxYaiG5NY4HBhDIDnJF60GU+2LqHAdKKer86//e4yg69aENCtQ04n0ovz+tq2YPME5t5yp4pw== -"@typescript-eslint/eslint-plugin@^1.9.0": - version "1.9.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-1.9.0.tgz#29d73006811bf2563b88891ceeff1c5ea9c8d9c6" - integrity sha512-FOgfBorxjlBGpDIw+0LaZIXRX6GEEUfzj8LXwaQIUCp+gDOvkI+1WgugJ7SmWiISqK9Vj5r8S7NDKO/LB+6X9A== +"@typescript-eslint/eslint-plugin@^1.9.1-alpha.3": + version "1.9.1-alpha.3" + resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-1.9.1-alpha.3.tgz#1a428cd22e699367a90f84c6f28fa31a0c18536c" + integrity sha512-YMdn15ImxsJ49SfvDXwv8pQBZ/ODm+ubhbfzSVxdWHrPWbUJuH+waRklKnpb3pa/+yIAx3hUxCwi2BTxhiR2Ng== dependencies: - "@typescript-eslint/experimental-utils" "1.9.0" - "@typescript-eslint/parser" "1.9.0" + "@typescript-eslint/experimental-utils" "1.9.1-alpha.3+8c8497c" + "@typescript-eslint/parser" "1.9.1-alpha.3+8c8497c" eslint-utils "^1.3.1" functional-red-black-tree "^1.0.1" regexpp "^2.0.1" requireindex "^1.2.0" tsutils "^3.7.0" -"@typescript-eslint/experimental-utils@1.9.0", "@typescript-eslint/experimental-utils@^1.9.0": - version "1.9.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-1.9.0.tgz#a92777d0c92d7bc8627abd7cdb06cdbcaf2b39e8" - integrity sha512-1s2dY9XxBwtS9IlSnRIlzqILPyeMly5tz1bfAmQ84Ul687xBBve5YsH5A5EKeIcGurYYqY2w6RkHETXIwnwV0A== +"@typescript-eslint/experimental-utils@1.9.1-alpha.3+8c8497c", "@typescript-eslint/experimental-utils@^1.9.1-alpha.3": + version "1.9.1-alpha.3" + resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-1.9.1-alpha.3.tgz#4dc44080fa2659a26e7acf9115e64aa1f132793b" + integrity sha512-0RB13XRNGcwrdpLgq/kyZiR3Vj4l4+RauBAjxBoMR77arO3YFTlb/QOw/41k+jBMzuhA9H6LVVBzqJKxnWav+Q== dependencies: - "@typescript-eslint/typescript-estree" "1.9.0" + "@typescript-eslint/typescript-estree" "1.9.1-alpha.3+8c8497c" -"@typescript-eslint/parser@1.9.0": - version "1.9.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-1.9.0.tgz#5796cbfcb9a3a5757aeb671c1ac88d7a94a95962" - integrity sha512-CWgC1XrQ34H/+LwAU7vY5xteZDkNqeAkeidEpJnJgkKu0yqQ3ZhQ7S+dI6MX4vmmM1TKRbOrKuXc6W0fIHhdbA== +"@typescript-eslint/parser@1.9.1-alpha.3+8c8497c": + version "1.9.1-alpha.3" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-1.9.1-alpha.3.tgz#5f59250ffa0aea4dd7d83b79673f23307f406754" + integrity sha512-BInMC4AVITeqh+UD5NWUst5cN4SRvUCWd55nj/VFNLLtPkciNPQlDwYa3+gQGCJzasD0Y4Fx6tRBUUS0ZWWz0w== dependencies: - "@typescript-eslint/experimental-utils" "1.9.0" - "@typescript-eslint/typescript-estree" "1.9.0" + "@typescript-eslint/experimental-utils" "1.9.1-alpha.3+8c8497c" + "@typescript-eslint/typescript-estree" "1.9.1-alpha.3+8c8497c" eslint-scope "^4.0.0" eslint-visitor-keys "^1.0.0" -"@typescript-eslint/typescript-estree@1.9.0": - version "1.9.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.9.0.tgz#5d6d49be936e96fb0f859673480f89b070a5dd9b" - integrity sha512-7Eg0TEQpCkTsEwsl1lIzd6i7L3pJLQFWesV08dS87bNz0NeSjbL78gNAP1xCKaCejkds4PhpLnZkaAjx9SU8OA== +"@typescript-eslint/typescript-estree@1.9.1-alpha.3+8c8497c": + version "1.9.1-alpha.3" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.9.1-alpha.3.tgz#b6319f9c6b07fff22953ec67cecb9c6354cab3cf" + integrity sha512-CVCk1aQz/n70msW2UePldSertYAOymuqvtW1xisABr0gVDRlZ894XlrT5ck+yORjjs0ln/hyabcGsYhRSUuHZQ== dependencies: lodash.unescape "4.0.1" semver "5.5.0"