linter-bundle 2.21.0 → 2.22.0
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/CHANGELOG.md +13 -1
- package/README.md +4 -0
- package/eslint/rules/no-unnecessary-typeof.js +158 -15
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -6,7 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
-
[Show all code changes](https://github.com/jens-duttke/linter-bundle/compare/v2.
|
|
9
|
+
[Show all code changes](https://github.com/jens-duttke/linter-bundle/compare/v2.22.0...HEAD)
|
|
10
|
+
|
|
11
|
+
## [2.22.0] - 2022-09-19
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- [eslint] Updated `@typescript-eslint` from `5.37.0` to `5.38.0`
|
|
16
|
+
- [eslint] Updated `eslint-plugin-functional` from `4.3.1` to `4.3.2`
|
|
17
|
+
- [stylelint] Updated `postcss-scss` from `4.0.4` to `4.0.5`
|
|
18
|
+
- [stylelint] Updated `stylelint` from `14.11.0` to `14.12.0`
|
|
19
|
+
- [eslint] Improved [`no-unnecessary-typeof`](./eslint/rules/no-unnecessary-typeof.md) to cover more cases
|
|
20
|
+
|
|
21
|
+
[Show all code changes](https://github.com/jens-duttke/linter-bundle/compare/v2.21.0...v2.22.0)
|
|
10
22
|
|
|
11
23
|
## [2.21.0] - 2022-09-14
|
|
12
24
|
|
package/README.md
CHANGED
|
@@ -425,3 +425,7 @@ If you get such an error message:
|
|
|
425
425
|
the problem is most likely, that your `tsconfig.json` does not cover your JavaScript files and that you don't have a `jsconfig.json` file in your root directory. This is required by the `@typescript-eslint` to use TypeScript for linting of JavaScript files.
|
|
426
426
|
|
|
427
427
|
To solve this problem, either `"include"` your JavaScript files in your `tsconfig.json` (don't forget to set the compiler option `"checkJs"` to `true`) or create a `jsconfig.json` file in your root directory (this can be a copy of your `tsconfig.json` with an `"include"` of your JavaScript files).
|
|
428
|
+
|
|
429
|
+
### In VSCode, in every file, the first line shows the error `Definition for rule "no-unnecessary-typeof" was not found. eslint(no-unnecessary-typeof)`
|
|
430
|
+
|
|
431
|
+
Please ensure that you've added the configuration options as described above ("VSCode setup" > "ESLint").
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @file ESLint rule which ensures that `typeof
|
|
3
|
-
*
|
|
4
|
-
* @see https://www.joshwcomeau.com/react/the-perils-of-rehydration/
|
|
2
|
+
* @file ESLint rule which ensures that a `typeof` operant has more than one type in TypeScript, to prevent unnecessary checks of types at runtime.
|
|
5
3
|
*/
|
|
6
4
|
|
|
5
|
+
/** @typedef {ts.Type & { intrinsicName?: string; types?: ts.Type[]; objectFlags?: ts.ObjectFlags; }} Type */
|
|
6
|
+
|
|
7
|
+
const ts = require('typescript');
|
|
8
|
+
|
|
7
9
|
const { ESLintUtils } = require('@typescript-eslint/utils');
|
|
8
10
|
|
|
9
11
|
/**
|
|
@@ -16,7 +18,8 @@ module.exports = {
|
|
|
16
18
|
recommended: true
|
|
17
19
|
},
|
|
18
20
|
messages: {
|
|
19
|
-
|
|
21
|
+
textWithType: 'Unnecessary `typeof`, because the only possible type of `{{ name }}` is `{{ type }}`.',
|
|
22
|
+
textWithoutType: 'Unnecessary `typeof`, because there is only one possible type of `{{ name }}`.'
|
|
20
23
|
}
|
|
21
24
|
},
|
|
22
25
|
create (context) {
|
|
@@ -33,21 +36,161 @@ module.exports = {
|
|
|
33
36
|
// @ts-expect-error -- ESLint `Identifier` is not recognized as `Node` by @typescript-eslint
|
|
34
37
|
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node.argument);
|
|
35
38
|
|
|
36
|
-
/** @type {
|
|
39
|
+
/** @type {Type} */
|
|
37
40
|
const nodeType = checker.getTypeAtLocation(originalNode);
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
if (isSingleType(nodeType)) {
|
|
43
|
+
if (nodeType.intrinsicName) {
|
|
44
|
+
context.report({
|
|
45
|
+
node,
|
|
46
|
+
messageId: 'textWithType',
|
|
47
|
+
data: {
|
|
48
|
+
name: node.argument.name,
|
|
49
|
+
type: nodeType.intrinsicName
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
messageId: 'textWithoutType',
|
|
57
|
+
data: {
|
|
58
|
+
name: node.argument.name
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
49
62
|
}
|
|
50
63
|
}
|
|
51
64
|
};
|
|
52
65
|
}
|
|
53
66
|
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Check if the number of types equals one.
|
|
70
|
+
*
|
|
71
|
+
* @param {Type} type - TypeScript type node.
|
|
72
|
+
* @returns {boolean} Returns `true` if the `type` is only one specific type.
|
|
73
|
+
*/
|
|
74
|
+
function isSingleType (type) {
|
|
75
|
+
if (isAnyOrUnknown(type)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!type.types) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const firstType = type.types[0];
|
|
84
|
+
|
|
85
|
+
if (isAnyOrUnknown(firstType)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (let i = 1; i < type.types.length; i++) {
|
|
90
|
+
if (isDifferentType(type.types[i], firstType)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Check if the type is either `any` or `unknown`, which represents multiple types.
|
|
100
|
+
*
|
|
101
|
+
* @param {ts.Type} type - TypeScript type node.
|
|
102
|
+
* @returns {boolean} Returns `true` if the type is either `any` or `unknown`, or an object which is based on `unknown`.
|
|
103
|
+
*/
|
|
104
|
+
function isAnyOrUnknown (type) {
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- `symbol` on Object is `undefined` for `Omit<unknown, 'undefined'>`
|
|
106
|
+
return (type.flags === ts.TypeFlags.Any || type.flags === ts.TypeFlags.Unknown || (type.flags === ts.TypeFlags.Object && type.symbol === undefined));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Check if two types are identical.
|
|
111
|
+
*
|
|
112
|
+
* @param {Type} type1 - TypeScript type node.
|
|
113
|
+
* @param {Type} type2 - TypeScript type node.
|
|
114
|
+
* @returns {boolean} Returns `true` if both types are identical.
|
|
115
|
+
*/
|
|
116
|
+
function isDifferentType (type1, type2) {
|
|
117
|
+
if (isString(type1) && isString(type2)) { return false; }
|
|
118
|
+
if (isNumber(type1) && isNumber(type2)) { return false; }
|
|
119
|
+
if (isBigInt(type1) && isBigInt(type2)) { return false; }
|
|
120
|
+
if (isBoolean(type1) && isBoolean(type2)) { return false; }
|
|
121
|
+
if (isSymbol(type1) && isSymbol(type2)) { return false; }
|
|
122
|
+
|
|
123
|
+
if (type1.flags !== type2.flags) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (type1.flags === ts.TypeFlags.Object) {
|
|
128
|
+
if (isFunction(type1)) {
|
|
129
|
+
return !isFunction(type2);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return isFunction(type2);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Checks if the given `type` is a `string`.
|
|
140
|
+
*
|
|
141
|
+
* @param {Type} type - TypeScript type node.
|
|
142
|
+
* @returns {boolean} Returns `true` if the type is a `string`.
|
|
143
|
+
*/
|
|
144
|
+
function isString (type) {
|
|
145
|
+
return [ts.TypeFlags.String, ts.TypeFlags.StringLiteral, ts.TypeFlags.StringMapping].includes(type.flags);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Checks if the given `type` is a `number`.
|
|
150
|
+
*
|
|
151
|
+
* @param {Type} type - TypeScript type node.
|
|
152
|
+
* @returns {boolean} Returns `true` if the type is a `number`.
|
|
153
|
+
*/
|
|
154
|
+
function isNumber (type) {
|
|
155
|
+
return [ts.TypeFlags.Number, ts.TypeFlags.NumberLiteral].includes(type.flags);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Checks if the given `type` is a `bigint`.
|
|
160
|
+
*
|
|
161
|
+
* @param {Type} type - TypeScript type node.
|
|
162
|
+
* @returns {boolean} Returns `true` if the type is a `bigint`.
|
|
163
|
+
*/
|
|
164
|
+
function isBigInt (type) {
|
|
165
|
+
return [ts.TypeFlags.BigInt, ts.TypeFlags.BigIntLiteral].includes(type.flags);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Checks if the given `type` is a `boolean`.
|
|
170
|
+
*
|
|
171
|
+
* @param {Type} type - TypeScript type node.
|
|
172
|
+
* @returns {boolean} Returns `true` if the type is a `boolean`.
|
|
173
|
+
*/
|
|
174
|
+
function isBoolean (type) {
|
|
175
|
+
return [ts.TypeFlags.Boolean, ts.TypeFlags.BooleanLiteral].includes(type.flags);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Checks if the given `type` is a `symbol`.
|
|
180
|
+
*
|
|
181
|
+
* @param {Type} type - TypeScript type node.
|
|
182
|
+
* @returns {boolean} Returns `true` if the type is a `symbol`.
|
|
183
|
+
*/
|
|
184
|
+
function isSymbol (type) {
|
|
185
|
+
return [ts.TypeFlags.ESSymbol, ts.TypeFlags.UniqueESSymbol].includes(type.flags);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Checks if the given `type` is a `function`.
|
|
190
|
+
*
|
|
191
|
+
* @param {Type} type - TypeScript type node.
|
|
192
|
+
* @returns {boolean} Returns `true` if the type is a `function`.
|
|
193
|
+
*/
|
|
194
|
+
function isFunction (type) {
|
|
195
|
+
return (type.objectFlags === ts.ObjectFlags.Anonymous);
|
|
196
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linter-bundle",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.22.0",
|
|
4
4
|
"description": "Ready-to use bundle of linting tools, containing configurations for ESLint, stylelint and markdownlint.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -40,14 +40,14 @@
|
|
|
40
40
|
"check-outdated": "npx --yes -- check-outdated --ignore-pre-releases"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
44
|
-
"@typescript-eslint/parser": "5.
|
|
45
|
-
"@typescript-eslint/utils": "5.
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "5.38.0",
|
|
44
|
+
"@typescript-eslint/parser": "5.38.0",
|
|
45
|
+
"@typescript-eslint/utils": "5.38.0",
|
|
46
46
|
"eslint": "8.23.1",
|
|
47
47
|
"eslint-import-resolver-typescript": "3.5.1",
|
|
48
48
|
"eslint-import-resolver-webpack": "0.13.2",
|
|
49
49
|
"eslint-plugin-eslint-comments": "3.2.0",
|
|
50
|
-
"eslint-plugin-functional": "4.3.
|
|
50
|
+
"eslint-plugin-functional": "4.3.2",
|
|
51
51
|
"eslint-plugin-import": "2.26.0",
|
|
52
52
|
"eslint-plugin-jest": "27.0.4",
|
|
53
53
|
"eslint-plugin-jsdoc": "39.3.6",
|
|
@@ -59,8 +59,8 @@
|
|
|
59
59
|
"eslint-plugin-unicorn": "43.0.2",
|
|
60
60
|
"markdownlint-cli": "0.32.2",
|
|
61
61
|
"micromatch": "4.0.5",
|
|
62
|
-
"postcss-scss": "4.0.
|
|
63
|
-
"stylelint": "14.
|
|
62
|
+
"postcss-scss": "4.0.5",
|
|
63
|
+
"stylelint": "14.12.0",
|
|
64
64
|
"stylelint-declaration-block-no-ignored-properties": "2.5.0",
|
|
65
65
|
"stylelint-order": "5.0.0",
|
|
66
66
|
"stylelint-scss": "4.3.0",
|