@typescript-eslint/eslint-plugin 6.0.0-alpha.94 → 6.0.0-alpha.95
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.
|
@@ -38,13 +38,14 @@ exports.default = util.createRule({
|
|
|
38
38
|
type: 'object',
|
|
39
39
|
additionalProperties: false,
|
|
40
40
|
properties: {
|
|
41
|
+
allow: util.readonlynessOptionsSchema.properties.allow,
|
|
41
42
|
checkParameterProperties: {
|
|
42
43
|
type: 'boolean',
|
|
43
44
|
},
|
|
44
45
|
ignoreInferredTypes: {
|
|
45
46
|
type: 'boolean',
|
|
46
47
|
},
|
|
47
|
-
|
|
48
|
+
treatMethodsAsReadonly: util.readonlynessOptionsSchema.properties.treatMethodsAsReadonly,
|
|
48
49
|
},
|
|
49
50
|
},
|
|
50
51
|
],
|
|
@@ -54,14 +55,14 @@ exports.default = util.createRule({
|
|
|
54
55
|
},
|
|
55
56
|
defaultOptions: [
|
|
56
57
|
{
|
|
58
|
+
allow: util.readonlynessOptionsDefaults.allow,
|
|
57
59
|
checkParameterProperties: true,
|
|
58
60
|
ignoreInferredTypes: false,
|
|
59
|
-
|
|
61
|
+
treatMethodsAsReadonly: util.readonlynessOptionsDefaults.treatMethodsAsReadonly,
|
|
60
62
|
},
|
|
61
63
|
],
|
|
62
|
-
create(context, [{ checkParameterProperties, ignoreInferredTypes, treatMethodsAsReadonly }]) {
|
|
64
|
+
create(context, [{ allow, checkParameterProperties, ignoreInferredTypes, treatMethodsAsReadonly, },]) {
|
|
63
65
|
const services = util.getParserServices(context);
|
|
64
|
-
const checker = services.program.getTypeChecker();
|
|
65
66
|
return {
|
|
66
67
|
[[
|
|
67
68
|
utils_1.AST_NODE_TYPES.ArrowFunctionExpression,
|
|
@@ -86,8 +87,9 @@ exports.default = util.createRule({
|
|
|
86
87
|
continue;
|
|
87
88
|
}
|
|
88
89
|
const type = services.getTypeAtLocation(actualParam);
|
|
89
|
-
const isReadOnly = util.isTypeReadonly(
|
|
90
|
+
const isReadOnly = util.isTypeReadonly(services.program, type, {
|
|
90
91
|
treatMethodsAsReadonly: treatMethodsAsReadonly,
|
|
92
|
+
allow,
|
|
91
93
|
});
|
|
92
94
|
if (!isReadOnly) {
|
|
93
95
|
context.report({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-readonly-parameter-types.js","sourceRoot":"","sources":["../../src/rules/prefer-readonly-parameter-types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAA0D;AAE1D,8CAAgC;
|
|
1
|
+
{"version":3,"file":"prefer-readonly-parameter-types.js","sourceRoot":"","sources":["../../src/rules/prefer-readonly-parameter-types.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAA0D;AAE1D,8CAAgC;AAYhC,kBAAe,IAAI,CAAC,UAAU,CAAsB;IAClD,IAAI,EAAE,iCAAiC;IACvC,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,gGAAgG;YAClG,oBAAoB,EAAE,IAAI;SAC3B;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,oBAAoB,EAAE,KAAK;gBAC3B,UAAU,EAAE;oBACV,KAAK,EAAE,IAAI,CAAC,yBAAyB,CAAC,UAAU,CAAC,KAAK;oBACtD,wBAAwB,EAAE;wBACxB,IAAI,EAAE,SAAS;qBAChB;oBACD,mBAAmB,EAAE;wBACnB,IAAI,EAAE,SAAS;qBAChB;oBACD,sBAAsB,EACpB,IAAI,CAAC,yBAAyB,CAAC,UAAU,CAAC,sBAAsB;iBACnE;aACF;SACF;QACD,QAAQ,EAAE;YACR,gBAAgB,EAAE,uCAAuC;SAC1D;KACF;IACD,cAAc,EAAE;QACd;YACE,KAAK,EAAE,IAAI,CAAC,2BAA2B,CAAC,KAAK;YAC7C,wBAAwB,EAAE,IAAI;YAC9B,mBAAmB,EAAE,KAAK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,CAAC;4BACb,IAAI,EAAE,WAAW;4BACjB,SAAS,EAAE,kBAAkB;yBAC9B,CAAC,CAAC;qBACJ;iBACF;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -129,6 +129,101 @@ interface Foo {
|
|
|
129
129
|
|
|
130
130
|
## Options
|
|
131
131
|
|
|
132
|
+
### `allow`
|
|
133
|
+
|
|
134
|
+
Some complex types cannot easily be made readonly, for example the `HTMLElement` type or the `JQueryStatic` type from `@types/jquery`. This option allows you to globally disable reporting of such types.
|
|
135
|
+
|
|
136
|
+
Each item must be one of:
|
|
137
|
+
|
|
138
|
+
- A type defined in a file (`{from: "file", name: "Foo", path: "src/foo-file.ts"}` with `path` being an optional path relative to the project root directory)
|
|
139
|
+
- A type from the default library (`{from: "lib", name: "Foo"}`)
|
|
140
|
+
- A type from a package (`{from: "package", name: "Foo", package: "foo-lib"}`, this also works for types defined in a typings package).
|
|
141
|
+
|
|
142
|
+
Additionally, a type may be defined just as a simple string, which then matches the type independently of its origin.
|
|
143
|
+
|
|
144
|
+
Examples of code for this rule with:
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"allow": [
|
|
149
|
+
"$",
|
|
150
|
+
{ "source": "file", "name": "Foo" },
|
|
151
|
+
{ "source": "lib", "name": "HTMLElement" },
|
|
152
|
+
{ "from": "package", "name": "Bar", "package": "bar-lib" }
|
|
153
|
+
]
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
<!--tabs-->
|
|
158
|
+
|
|
159
|
+
#### ❌ Incorrect
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
interface ThisIsMutable {
|
|
163
|
+
prop: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface Wrapper {
|
|
167
|
+
sub: ThisIsMutable;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface WrapperWithOther {
|
|
171
|
+
readonly sub: Foo;
|
|
172
|
+
otherProp: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function fn1(arg: ThisIsMutable) {} // Incorrect because ThisIsMutable is not readonly
|
|
176
|
+
function fn2(arg: Wrapper) {} // Incorrect because Wrapper.sub is not readonly
|
|
177
|
+
function fn3(arg: WrapperWithOther) {} // Incorrect because WrapperWithOther.otherProp is not readonly and not in the allowlist
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { Foo } from 'some-lib';
|
|
182
|
+
import { Bar } from 'incorrect-lib';
|
|
183
|
+
|
|
184
|
+
interface HTMLElement {
|
|
185
|
+
prop: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function fn1(arg: Foo) {} // Incorrect because Foo is not a local type
|
|
189
|
+
function fn2(arg: HTMLElement) {} // Incorrect because HTMLElement is not from the default library
|
|
190
|
+
function fn3(arg: Bar) {} // Incorrect because Bar is not from "bar-lib"
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### ✅ Correct
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
interface Foo {
|
|
197
|
+
prop: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface Wrapper {
|
|
201
|
+
readonly sub: Foo;
|
|
202
|
+
readonly otherProp: string;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function fn1(arg: Foo) {} // Works because Foo is allowed
|
|
206
|
+
function fn2(arg: Wrapper) {} // Works even when Foo is nested somewhere in the type, with other properties still being checked
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
import { Bar } from 'bar-lib';
|
|
211
|
+
|
|
212
|
+
interface Foo {
|
|
213
|
+
prop: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function fn1(arg: Foo) {} // Works because Foo is a local type
|
|
217
|
+
function fn2(arg: HTMLElement) {} // Works because HTMLElement is from the default library
|
|
218
|
+
function fn3(arg: Bar) {} // Works because Bar is from "bar-lib"
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
import { Foo } from './foo';
|
|
223
|
+
|
|
224
|
+
function fn(arg: Foo) {} // Works because Foo is still a local type - it has to be in the same package
|
|
225
|
+
```
|
|
226
|
+
|
|
132
227
|
### `checkParameterProperties`
|
|
133
228
|
|
|
134
229
|
This option allows you to enable or disable the checking of parameter properties.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
|
3
|
-
"version": "6.0.0-alpha.
|
|
3
|
+
"version": "6.0.0-alpha.95+c9427b78b",
|
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@eslint-community/regexpp": "^4.4.0",
|
|
58
|
-
"@typescript-eslint/scope-manager": "6.0.0-alpha.
|
|
59
|
-
"@typescript-eslint/type-utils": "6.0.0-alpha.
|
|
60
|
-
"@typescript-eslint/utils": "6.0.0-alpha.
|
|
58
|
+
"@typescript-eslint/scope-manager": "6.0.0-alpha.95+c9427b78b",
|
|
59
|
+
"@typescript-eslint/type-utils": "6.0.0-alpha.95+c9427b78b",
|
|
60
|
+
"@typescript-eslint/utils": "6.0.0-alpha.95+c9427b78b",
|
|
61
61
|
"debug": "^4.3.4",
|
|
62
62
|
"grapheme-splitter": "^1.0.4",
|
|
63
63
|
"ignore": "^5.2.0",
|
|
@@ -92,5 +92,5 @@
|
|
|
92
92
|
"type": "opencollective",
|
|
93
93
|
"url": "https://opencollective.com/typescript-eslint"
|
|
94
94
|
},
|
|
95
|
-
"gitHead": "
|
|
95
|
+
"gitHead": "c9427b78b69f1a6a2453ef2df2be5bf96b7b00bc"
|
|
96
96
|
}
|