@typescript-eslint/eslint-plugin 6.17.0 → 6.17.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rules/no-require-imports.js +54 -5
- package/dist/rules/no-require-imports.js.map +1 -1
- package/dist/rules/no-var-requires.js +24 -3
- package/dist/rules/no-var-requires.js.map +1 -1
- package/docs/rules/no-require-imports.md +22 -0
- package/docs/rules/no-var-requires.md +22 -0
- package/docs/rules/switch-exhaustiveness-check.md +2 -2
- package/package.json +8 -9
|
@@ -1,24 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
const utils_1 = require("@typescript-eslint/utils");
|
|
4
27
|
const eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils");
|
|
5
|
-
const
|
|
6
|
-
exports.default =
|
|
28
|
+
const util = __importStar(require("../util"));
|
|
29
|
+
exports.default = util.createRule({
|
|
7
30
|
name: 'no-require-imports',
|
|
8
31
|
meta: {
|
|
9
32
|
type: 'problem',
|
|
10
33
|
docs: {
|
|
11
34
|
description: 'Disallow invocation of `require()`',
|
|
12
35
|
},
|
|
13
|
-
schema: [
|
|
36
|
+
schema: [
|
|
37
|
+
{
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {
|
|
40
|
+
allow: {
|
|
41
|
+
type: 'array',
|
|
42
|
+
items: { type: 'string' },
|
|
43
|
+
description: 'Patterns of import paths to allow requiring from.',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
14
49
|
messages: {
|
|
15
50
|
noRequireImports: 'A `require()` style import is forbidden.',
|
|
16
51
|
},
|
|
17
52
|
},
|
|
18
|
-
defaultOptions: [],
|
|
19
|
-
create(context) {
|
|
53
|
+
defaultOptions: [{ allow: [] }],
|
|
54
|
+
create(context, options) {
|
|
55
|
+
const allowPatterns = options[0].allow.map(pattern => new RegExp(pattern, 'u'));
|
|
56
|
+
function isImportPathAllowed(importPath) {
|
|
57
|
+
return allowPatterns.some(pattern => importPath.match(pattern));
|
|
58
|
+
}
|
|
20
59
|
return {
|
|
21
60
|
'CallExpression[callee.name="require"]'(node) {
|
|
61
|
+
if (node.arguments[0]?.type === utils_1.AST_NODE_TYPES.Literal &&
|
|
62
|
+
typeof node.arguments[0].value === 'string' &&
|
|
63
|
+
isImportPathAllowed(node.arguments[0].value)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
22
66
|
const variable = utils_1.ASTUtils.findVariable((0, eslint_utils_1.getScope)(context), 'require');
|
|
23
67
|
// ignore non-global require usage as it's something user-land custom instead
|
|
24
68
|
// of the commonjs standard
|
|
@@ -30,6 +74,11 @@ exports.default = (0, util_1.createRule)({
|
|
|
30
74
|
}
|
|
31
75
|
},
|
|
32
76
|
TSExternalModuleReference(node) {
|
|
77
|
+
if (node.expression.type === utils_1.AST_NODE_TYPES.Literal &&
|
|
78
|
+
typeof node.expression.value === 'string' &&
|
|
79
|
+
isImportPathAllowed(node.expression.value)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
33
82
|
context.report({
|
|
34
83
|
node,
|
|
35
84
|
messageId: 'noRequireImports',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-require-imports.js","sourceRoot":"","sources":["../../src/rules/no-require-imports.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"no-require-imports.js","sourceRoot":"","sources":["../../src/rules/no-require-imports.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAAoE;AACpE,wEAAiE;AAEjE,8CAAgC;AAShC,kBAAe,IAAI,CAAC,UAAU,CAAsB;IAClD,IAAI,EAAE,oBAAoB;IAC1B,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,oCAAoC;SAClD;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,KAAK,EAAE;wBACL,IAAI,EAAE,OAAO;wBACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;wBACzB,WAAW,EAAE,mDAAmD;qBACjE;iBACF;gBACD,oBAAoB,EAAE,KAAK;aAC5B;SACF;QACD,QAAQ,EAAE;YACR,gBAAgB,EAAE,0CAA0C;SAC7D;KACF;IACD,cAAc,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;IAC/B,MAAM,CAAC,OAAO,EAAE,OAAO;QACrB,MAAM,aAAa,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAA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ACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -14,12 +14,33 @@ exports.default = (0, util_1.createRule)({
|
|
|
14
14
|
messages: {
|
|
15
15
|
noVarReqs: 'Require statement not part of import statement.',
|
|
16
16
|
},
|
|
17
|
-
schema: [
|
|
17
|
+
schema: [
|
|
18
|
+
{
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
allow: {
|
|
22
|
+
type: 'array',
|
|
23
|
+
items: { type: 'string' },
|
|
24
|
+
description: 'Patterns of import paths to allow requiring from.',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
18
30
|
},
|
|
19
|
-
defaultOptions: [],
|
|
20
|
-
create(context) {
|
|
31
|
+
defaultOptions: [{ allow: [] }],
|
|
32
|
+
create(context, options) {
|
|
33
|
+
const allowPatterns = options[0].allow.map(pattern => new RegExp(pattern, 'u'));
|
|
34
|
+
function isImportPathAllowed(importPath) {
|
|
35
|
+
return allowPatterns.some(pattern => importPath.match(pattern));
|
|
36
|
+
}
|
|
21
37
|
return {
|
|
22
38
|
'CallExpression[callee.name="require"]'(node) {
|
|
39
|
+
if (node.arguments[0]?.type === utils_1.AST_NODE_TYPES.Literal &&
|
|
40
|
+
typeof node.arguments[0].value === 'string' &&
|
|
41
|
+
isImportPathAllowed(node.arguments[0].value)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
23
44
|
const parent = node.parent.type === utils_1.AST_NODE_TYPES.ChainExpression
|
|
24
45
|
? node.parent.parent
|
|
25
46
|
: node.parent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-var-requires.js","sourceRoot":"","sources":["../../src/rules/no-var-requires.ts"],"names":[],"mappings":";;AACA,oDAAoE;AACpE,wEAAiE;AAEjE,kCAAqC;
|
|
1
|
+
{"version":3,"file":"no-var-requires.js","sourceRoot":"","sources":["../../src/rules/no-var-requires.ts"],"names":[],"mappings":";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;gBACD,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,sBAAc,CAAC,eAAe;oBACjD,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM;oBACpB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;gBAElB,IACE;oBACE,sBAAc,CAAC,cAAc;oBAC7B,sBAAc,CAAC,gBAAgB;oBAC/B,sBAAc,CAAC,aAAa;oBAC5B,sBAAc,CAAC,cAAc;oBAC7B,sBAAc,CAAC,eAAe;oBAC9B,sBAAc,CAAC,kBAAkB;iBAClC,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,EACvB,CAAC;oBACD,MAAM,QAAQ,GAAG,gBAAQ,CAAC,YAAY,CAAC,IAAA,uBAAQ,EAAC,OAAO,CAAC,EAAE,SAAS,CAAC,CAAC;oBAErE,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,CAAC;wBAClC,OAAO,CAAC,MAAM,CAAC;4BACb,IAAI;4BACJ,SAAS,EAAE,WAAW;yBACvB,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -28,6 +28,28 @@ import { lib2 } from 'lib2';
|
|
|
28
28
|
import * as lib3 from 'lib3';
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
## Options
|
|
32
|
+
|
|
33
|
+
### `allow`
|
|
34
|
+
|
|
35
|
+
A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
|
|
36
|
+
|
|
37
|
+
With `{allow: ['/package\\.json$']}`:
|
|
38
|
+
|
|
39
|
+
<!--tabs-->
|
|
40
|
+
|
|
41
|
+
### ❌ Incorrect
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
console.log(require('../data.json').version);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### ✅ Correct
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
console.log(require('../package.json').version);
|
|
51
|
+
```
|
|
52
|
+
|
|
31
53
|
## When Not To Use It
|
|
32
54
|
|
|
33
55
|
If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you.
|
|
@@ -28,6 +28,28 @@ require('foo');
|
|
|
28
28
|
import foo from 'foo';
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
## Options
|
|
32
|
+
|
|
33
|
+
### `allow`
|
|
34
|
+
|
|
35
|
+
A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
|
|
36
|
+
|
|
37
|
+
With `{allow: ['/package\\.json$']}`:
|
|
38
|
+
|
|
39
|
+
<!--tabs-->
|
|
40
|
+
|
|
41
|
+
### ❌ Incorrect
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const foo = require('../data.json');
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### ✅ Correct
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const foo = require('../package.json');
|
|
51
|
+
```
|
|
52
|
+
|
|
31
53
|
## When Not To Use It
|
|
32
54
|
|
|
33
55
|
If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you.
|
|
@@ -13,14 +13,14 @@ This rule reports when a `switch` statement over a value typed as a union of lit
|
|
|
13
13
|
|
|
14
14
|
## Options
|
|
15
15
|
|
|
16
|
-
### `
|
|
16
|
+
### `allowDefaultCaseForExhaustiveSwitch`
|
|
17
17
|
|
|
18
18
|
Defaults to true. If set to false, this rule will also report when a `switch` statement has a case for everything in a union and _also_ contains a `default` case. Thus, by setting this option to false, the rule becomes stricter.
|
|
19
19
|
|
|
20
20
|
When a `switch` statement over a union type is exhaustive, a final `default` case would be a form of dead code.
|
|
21
21
|
Additionally, if a new value is added to the union type, a `default` would prevent the `switch-exhaustiveness-check` rule from reporting on the new case not being handled in the `switch` statement.
|
|
22
22
|
|
|
23
|
-
#### `
|
|
23
|
+
#### `allowDefaultCaseForExhaustiveSwitch` Caveats
|
|
24
24
|
|
|
25
25
|
It can sometimes be useful to include a redundant `default` case on an exhaustive `switch` statement if it's possible for values to have types not represented by the union type.
|
|
26
26
|
For example, in applications that can have version mismatches between clients and servers, it's possible for a server running a newer software version to send a value not recognized by the client's older typings.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
|
3
|
-
"version": "6.17.
|
|
3
|
+
"version": "6.17.1-alpha.1",
|
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@eslint-community/regexpp": "^4.5.1",
|
|
60
|
-
"@typescript-eslint/scope-manager": "6.17.
|
|
61
|
-
"@typescript-eslint/type-utils": "6.17.
|
|
62
|
-
"@typescript-eslint/utils": "6.17.
|
|
63
|
-
"@typescript-eslint/visitor-keys": "6.17.
|
|
60
|
+
"@typescript-eslint/scope-manager": "6.17.1-alpha.1",
|
|
61
|
+
"@typescript-eslint/type-utils": "6.17.1-alpha.1",
|
|
62
|
+
"@typescript-eslint/utils": "6.17.1-alpha.1",
|
|
63
|
+
"@typescript-eslint/visitor-keys": "6.17.1-alpha.1",
|
|
64
64
|
"debug": "^4.3.4",
|
|
65
65
|
"graphemer": "^1.4.0",
|
|
66
66
|
"ignore": "^5.2.4",
|
|
@@ -73,8 +73,8 @@
|
|
|
73
73
|
"@types/debug": "*",
|
|
74
74
|
"@types/marked": "*",
|
|
75
75
|
"@types/natural-compare": "*",
|
|
76
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "6.17.
|
|
77
|
-
"@typescript-eslint/rule-tester": "6.17.
|
|
76
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "6.17.1-alpha.1",
|
|
77
|
+
"@typescript-eslint/rule-tester": "6.17.1-alpha.1",
|
|
78
78
|
"ajv": "^6.12.6",
|
|
79
79
|
"chalk": "^5.3.0",
|
|
80
80
|
"cross-fetch": "*",
|
|
@@ -102,6 +102,5 @@
|
|
|
102
102
|
"funding": {
|
|
103
103
|
"type": "opencollective",
|
|
104
104
|
"url": "https://opencollective.com/typescript-eslint"
|
|
105
|
-
}
|
|
106
|
-
"gitHead": "e566a5dda347470b8ced3cc301b7e4d3e7ed721b"
|
|
105
|
+
}
|
|
107
106
|
}
|