Skip to content

Commit 83ff198

Browse files
authored
chore: migrate no-jest-import to typescript (#259)
1 parent 718c08c commit 83ff198

File tree

5 files changed

+78
-61
lines changed

5 files changed

+78
-61
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"@types/eslint": "^4.16.6",
4646
"@types/jest": "^24.0.12",
4747
"@types/node": "^12.0.0",
48-
"@typescript-eslint/eslint-plugin": "^1.9.0",
49-
"@typescript-eslint/experimental-utils": "^1.9.0",
48+
"@typescript-eslint/eslint-plugin": "^1.9.1-alpha.3",
49+
"@typescript-eslint/experimental-utils": "^1.9.1-alpha.3",
5050
"babel-jest": "^24.8.0",
5151
"eslint": "^5.1.0",
5252
"eslint-config-prettier": "^4.1.0",

src/rules/__tests__/no-jest-import.test.js renamed to src/rules/__tests__/no-jest-import.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
'use strict';
1+
import { RuleTester as ESLintRuleTester } from 'eslint';
2+
import { TSESLint } from '@typescript-eslint/experimental-utils';
3+
import rule from '../no-jest-import';
24

3-
const rule = require('../no-jest-import.js');
4-
const { RuleTester } = require('eslint');
5-
const ruleTester = new RuleTester();
6-
const message = `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`;
5+
const RuleTester: TSESLint.RuleTester = ESLintRuleTester as any;
6+
7+
const ruleTester = new RuleTester({
8+
parser: '@typescript-eslint/parser',
9+
parserOptions: {
10+
ecmaVersion: 6,
11+
},
12+
});
713

814
ruleTester.run('no-jest-import', rule, {
915
valid: [
@@ -22,7 +28,7 @@ ruleTester.run('no-jest-import', rule, {
2228
{
2329
endColumn: 15,
2430
column: 9,
25-
message,
31+
messageId: 'unexpectedImport',
2632
},
2733
],
2834
},
@@ -32,8 +38,8 @@ ruleTester.run('no-jest-import', rule, {
3238
errors: [
3339
{
3440
endColumn: 24,
35-
column: 1,
36-
message,
41+
column: 18,
42+
messageId: 'unexpectedImport',
3743
},
3844
],
3945
},
@@ -43,7 +49,7 @@ ruleTester.run('no-jest-import', rule, {
4349
{
4450
endColumn: 26,
4551
column: 20,
46-
message,
52+
messageId: 'unexpectedImport',
4753
},
4854
],
4955
},
@@ -53,8 +59,8 @@ ruleTester.run('no-jest-import', rule, {
5359
errors: [
5460
{
5561
endColumn: 34,
56-
column: 1,
57-
message,
62+
column: 28,
63+
messageId: 'unexpectedImport',
5864
},
5965
],
6066
},
@@ -65,7 +71,7 @@ ruleTester.run('no-jest-import', rule, {
6571
{
6672
endColumn: 28,
6773
column: 22,
68-
message,
74+
messageId: 'unexpectedImport',
6975
},
7076
],
7177
},

src/rules/no-jest-import.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/rules/no-jest-import.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { createRule } from './tsUtils';
3+
4+
export default createRule({
5+
name: __filename,
6+
meta: {
7+
type: 'problem',
8+
docs: {
9+
description:
10+
"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.",
11+
category: 'Best Practices',
12+
recommended: 'error',
13+
},
14+
messages: {
15+
unexpectedImport: `Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything.`,
16+
},
17+
schema: [],
18+
},
19+
defaultOptions: [],
20+
create(context) {
21+
return {
22+
'ImportDeclaration[source.value="jest"]'(
23+
node: TSESTree.ImportDeclaration,
24+
) {
25+
context.report({ node: node.source, messageId: 'unexpectedImport' });
26+
},
27+
'CallExpression[callee.name="require"][arguments.0.value="jest"]'(
28+
node: TSESTree.CallExpression,
29+
) {
30+
context.report({
31+
node: node.arguments[0],
32+
messageId: 'unexpectedImport',
33+
});
34+
},
35+
};
36+
},
37+
});

yarn.lock

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,40 +1064,40 @@
10641064
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.12.tgz#45dd1d0638e8c8f153e87d296907659296873916"
10651065
integrity sha512-SOhuU4wNBxhhTHxYaiG5NY4HBhDIDnJF60GU+2LqHAdKKer86//e4yg69aENCtQ04n0ovz+tq2YPME5t5yp4pw==
10661066

1067-
"@typescript-eslint/eslint-plugin@^1.9.0":
1068-
version "1.9.0"
1069-
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-1.9.0.tgz#29d73006811bf2563b88891ceeff1c5ea9c8d9c6"
1070-
integrity sha512-FOgfBorxjlBGpDIw+0LaZIXRX6GEEUfzj8LXwaQIUCp+gDOvkI+1WgugJ7SmWiISqK9Vj5r8S7NDKO/LB+6X9A==
1067+
"@typescript-eslint/eslint-plugin@^1.9.1-alpha.3":
1068+
version "1.9.1-alpha.3"
1069+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-1.9.1-alpha.3.tgz#1a428cd22e699367a90f84c6f28fa31a0c18536c"
1070+
integrity sha512-YMdn15ImxsJ49SfvDXwv8pQBZ/ODm+ubhbfzSVxdWHrPWbUJuH+waRklKnpb3pa/+yIAx3hUxCwi2BTxhiR2Ng==
10711071
dependencies:
1072-
"@typescript-eslint/experimental-utils" "1.9.0"
1073-
"@typescript-eslint/parser" "1.9.0"
1072+
"@typescript-eslint/experimental-utils" "1.9.1-alpha.3+8c8497c"
1073+
"@typescript-eslint/parser" "1.9.1-alpha.3+8c8497c"
10741074
eslint-utils "^1.3.1"
10751075
functional-red-black-tree "^1.0.1"
10761076
regexpp "^2.0.1"
10771077
requireindex "^1.2.0"
10781078
tsutils "^3.7.0"
10791079

1080-
"@typescript-eslint/[email protected].0", "@typescript-eslint/experimental-utils@^1.9.0":
1081-
version "1.9.0"
1082-
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-1.9.0.tgz#a92777d0c92d7bc8627abd7cdb06cdbcaf2b39e8"
1083-
integrity sha512-1s2dY9XxBwtS9IlSnRIlzqILPyeMly5tz1bfAmQ84Ul687xBBve5YsH5A5EKeIcGurYYqY2w6RkHETXIwnwV0A==
1080+
"@typescript-eslint/[email protected].1-alpha.3+8c8497c", "@typescript-eslint/experimental-utils@^1.9.1-alpha.3":
1081+
version "1.9.1-alpha.3"
1082+
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-1.9.1-alpha.3.tgz#4dc44080fa2659a26e7acf9115e64aa1f132793b"
1083+
integrity sha512-0RB13XRNGcwrdpLgq/kyZiR3Vj4l4+RauBAjxBoMR77arO3YFTlb/QOw/41k+jBMzuhA9H6LVVBzqJKxnWav+Q==
10841084
dependencies:
1085-
"@typescript-eslint/typescript-estree" "1.9.0"
1085+
"@typescript-eslint/typescript-estree" "1.9.1-alpha.3+8c8497c"
10861086

1087-
"@typescript-eslint/[email protected].0":
1088-
version "1.9.0"
1089-
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-1.9.0.tgz#5796cbfcb9a3a5757aeb671c1ac88d7a94a95962"
1090-
integrity sha512-CWgC1XrQ34H/+LwAU7vY5xteZDkNqeAkeidEpJnJgkKu0yqQ3ZhQ7S+dI6MX4vmmM1TKRbOrKuXc6W0fIHhdbA==
1087+
"@typescript-eslint/[email protected].1-alpha.3+8c8497c":
1088+
version "1.9.1-alpha.3"
1089+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-1.9.1-alpha.3.tgz#5f59250ffa0aea4dd7d83b79673f23307f406754"
1090+
integrity sha512-BInMC4AVITeqh+UD5NWUst5cN4SRvUCWd55nj/VFNLLtPkciNPQlDwYa3+gQGCJzasD0Y4Fx6tRBUUS0ZWWz0w==
10911091
dependencies:
1092-
"@typescript-eslint/experimental-utils" "1.9.0"
1093-
"@typescript-eslint/typescript-estree" "1.9.0"
1092+
"@typescript-eslint/experimental-utils" "1.9.1-alpha.3+8c8497c"
1093+
"@typescript-eslint/typescript-estree" "1.9.1-alpha.3+8c8497c"
10941094
eslint-scope "^4.0.0"
10951095
eslint-visitor-keys "^1.0.0"
10961096

1097-
"@typescript-eslint/[email protected].0":
1098-
version "1.9.0"
1099-
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.9.0.tgz#5d6d49be936e96fb0f859673480f89b070a5dd9b"
1100-
integrity sha512-7Eg0TEQpCkTsEwsl1lIzd6i7L3pJLQFWesV08dS87bNz0NeSjbL78gNAP1xCKaCejkds4PhpLnZkaAjx9SU8OA==
1097+
"@typescript-eslint/[email protected].1-alpha.3+8c8497c":
1098+
version "1.9.1-alpha.3"
1099+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.9.1-alpha.3.tgz#b6319f9c6b07fff22953ec67cecb9c6354cab3cf"
1100+
integrity sha512-CVCk1aQz/n70msW2UePldSertYAOymuqvtW1xisABr0gVDRlZ894XlrT5ck+yORjjs0ln/hyabcGsYhRSUuHZQ==
11011101
dependencies:
11021102
lodash.unescape "4.0.1"
11031103
semver "5.5.0"

0 commit comments

Comments
 (0)