eslint-plugin-no-excess-properties 0.0.4 → 0.0.6
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/README.md +76 -76
- package/dist/object-literal.js +26 -23
- package/dist/object-literal.js.map +1 -1
- package/eslint.config.mjs +19 -19
- package/package.json +2 -1
- package/src/index.ts +31 -31
- package/src/object-literal.test.ts +227 -198
- package/src/object-literal.ts +225 -218
package/README.md
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
# eslint-plugin-no-excess-properties
|
|
2
|
-
|
|
3
|
-
ESLint plugin for TypeScript that warns when there are excess properties on object literals
|
|
4
|
-
|
|
5
|
-
Currently has a single rule `no-excess-properties/object-literal` that uses [typed linting](https://typescript-eslint.io/getting-started/typed-linting)
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
npm install --save-dev eslint-plugin-no-excess-properties
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Config
|
|
14
|
-
|
|
15
|
-
In the `eslint.config.mjs` file:
|
|
16
|
-
|
|
17
|
-
### Basic
|
|
18
|
-
|
|
19
|
-
```
|
|
20
|
-
import tseslint from "typescript-eslint";
|
|
21
|
-
import noExcessProperties from "eslint-plugin-no-excess-properties";
|
|
22
|
-
|
|
23
|
-
export default tseslint.config({
|
|
24
|
-
extends: [
|
|
25
|
-
noExcessProperties.configs.recommended,
|
|
26
|
-
],
|
|
27
|
-
})
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### Fancy
|
|
31
|
-
|
|
32
|
-
```
|
|
33
|
-
import tseslint from "typescript-eslint";
|
|
34
|
-
import noExcessProperties from "eslint-plugin-no-excess-properties";
|
|
35
|
-
|
|
36
|
-
export default tseslint.config({
|
|
37
|
-
plugins: {
|
|
38
|
-
"no-excess-properties": noExcessProperties,
|
|
39
|
-
},
|
|
40
|
-
rules: {
|
|
41
|
-
"no-excess-properties/object-literal": "error",
|
|
42
|
-
},
|
|
43
|
-
})
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Example Linted Code
|
|
47
|
-
|
|
48
|
-
See the [test file](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/src/master/src/object-literal.test.ts) for more examples
|
|
49
|
-
|
|
50
|
-
### Incorrect
|
|
51
|
-
|
|
52
|
-
```
|
|
53
|
-
let test1: { prop1: number; } = { prop1: 1 };
|
|
54
|
-
const test2 = { prop1: 2, extraPropertyNotInTest1: 3 };
|
|
55
|
-
test1 = test2; // Error
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Correct
|
|
59
|
-
|
|
60
|
-
```
|
|
61
|
-
let test1: { prop1: number; } = { prop1: 1 };
|
|
62
|
-
const test2 = { prop1: 2 };
|
|
63
|
-
test1 = test2; // OK
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## More Info
|
|
67
|
-
|
|
68
|
-
Related typescript-eslint issue: https://github.com/typescript-eslint/typescript-eslint/issues/10234
|
|
69
|
-
|
|
70
|
-
## Feedback
|
|
71
|
-
|
|
72
|
-
[Submit](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/issues/new) bug reports and other feedback in the [issues](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/issues?status=new&status=open) section
|
|
73
|
-
|
|
74
|
-
## License
|
|
75
|
-
|
|
76
|
-
MIT
|
|
1
|
+
# eslint-plugin-no-excess-properties
|
|
2
|
+
|
|
3
|
+
ESLint plugin for TypeScript that warns when there are excess properties on object literals
|
|
4
|
+
|
|
5
|
+
Currently has a single rule `no-excess-properties/object-literal` that uses [typed linting](https://typescript-eslint.io/getting-started/typed-linting)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install --save-dev eslint-plugin-no-excess-properties
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Config
|
|
14
|
+
|
|
15
|
+
In the `eslint.config.mjs` file:
|
|
16
|
+
|
|
17
|
+
### Basic
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
import tseslint from "typescript-eslint";
|
|
21
|
+
import noExcessProperties from "eslint-plugin-no-excess-properties";
|
|
22
|
+
|
|
23
|
+
export default tseslint.config({
|
|
24
|
+
extends: [
|
|
25
|
+
noExcessProperties.configs.recommended,
|
|
26
|
+
],
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Fancy
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
import tseslint from "typescript-eslint";
|
|
34
|
+
import noExcessProperties from "eslint-plugin-no-excess-properties";
|
|
35
|
+
|
|
36
|
+
export default tseslint.config({
|
|
37
|
+
plugins: {
|
|
38
|
+
"no-excess-properties": noExcessProperties,
|
|
39
|
+
},
|
|
40
|
+
rules: {
|
|
41
|
+
"no-excess-properties/object-literal": "error",
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Example Linted Code
|
|
47
|
+
|
|
48
|
+
See the [test file](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/src/master/src/object-literal.test.ts) for more examples
|
|
49
|
+
|
|
50
|
+
### Incorrect
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
let test1: { prop1: number; } = { prop1: 1 };
|
|
54
|
+
const test2 = { prop1: 2, extraPropertyNotInTest1: 3 };
|
|
55
|
+
test1 = test2; // Error
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Correct
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
let test1: { prop1: number; } = { prop1: 1 };
|
|
62
|
+
const test2 = { prop1: 2 };
|
|
63
|
+
test1 = test2; // OK
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## More Info
|
|
67
|
+
|
|
68
|
+
Related typescript-eslint issue: https://github.com/typescript-eslint/typescript-eslint/issues/10234
|
|
69
|
+
|
|
70
|
+
## Feedback
|
|
71
|
+
|
|
72
|
+
[Submit](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/issues/new) bug reports and other feedback in the [issues](https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/issues?status=new&status=open) section
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
package/dist/object-literal.js
CHANGED
|
@@ -39,7 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
const utils_1 = require("@typescript-eslint/utils");
|
|
40
40
|
const typescript_1 = __importDefault(require("typescript"));
|
|
41
41
|
const tsutils = __importStar(require("ts-api-utils"));
|
|
42
|
-
const createRule = utils_1.ESLintUtils.RuleCreator(() => "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties
|
|
42
|
+
const createRule = utils_1.ESLintUtils.RuleCreator(() => "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties");
|
|
43
43
|
function getAllPropertyNames(type) {
|
|
44
44
|
const allTypes = tsutils.typeConstituents(type);
|
|
45
45
|
return allTypes.reduce((all, t) => all.concat(...t.getProperties().map((p) => p.name)), []);
|
|
@@ -63,30 +63,26 @@ function compareNames(leftPropertyNames, rightPropertyNames, rightNode, context)
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
function compareSymbols(leftType, rightType, rightNode, context) {
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
const leftCallSignatures = leftType.getCallSignatures();
|
|
67
|
+
if (leftCallSignatures.length === 1) {
|
|
68
|
+
leftType = leftCallSignatures[0].getReturnType();
|
|
69
|
+
}
|
|
70
|
+
const rightCallSignatures = rightType.getCallSignatures();
|
|
71
|
+
if (rightCallSignatures.length === 1) {
|
|
72
|
+
rightType = rightCallSignatures[0].getReturnType();
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
const leftArrayType = leftType.getNumberIndexType();
|
|
75
|
+
const rightArrayType = rightType.getNumberIndexType();
|
|
76
|
+
if (leftArrayType && rightArrayType) {
|
|
77
|
+
leftType = leftArrayType;
|
|
78
|
+
rightType = rightArrayType;
|
|
74
79
|
}
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
const rightCallSignatures = rightType.getCallSignatures();
|
|
82
|
-
if (rightCallSignatures.length === 1) {
|
|
83
|
-
const returnType = rightCallSignatures[0].getReturnType();
|
|
84
|
-
if (isObjectLiteral(returnType)) {
|
|
85
|
-
rightPropertyNames = getAllPropertyNames(returnType);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
80
|
+
if (isObjectLiteral(rightType) &&
|
|
81
|
+
tsutils
|
|
82
|
+
.typeConstituents(leftType)
|
|
83
|
+
.every((t) => !t.getStringIndexType() && !t.getNumberIndexType())) {
|
|
84
|
+
compareNames(getAllPropertyNames(leftType), getAllPropertyNames(rightType), rightNode, context);
|
|
88
85
|
}
|
|
89
|
-
compareNames(leftPropertyNames, rightPropertyNames, rightNode, context);
|
|
90
86
|
}
|
|
91
87
|
const noExcessProperties = createRule({
|
|
92
88
|
create: function (context) {
|
|
@@ -112,7 +108,14 @@ const noExcessProperties = createRule({
|
|
|
112
108
|
break;
|
|
113
109
|
}
|
|
114
110
|
const argType = services.getTypeAtLocation(node.arguments[i]);
|
|
115
|
-
|
|
111
|
+
let paramType = typeChecker.getTypeOfSymbolAtLocation(functionSignature.parameters[i], functionNode);
|
|
112
|
+
const declarations = functionSignature.parameters[i].declarations;
|
|
113
|
+
if (declarations?.some((d) => typescript_1.default.isParameter(d) && d.dotDotDotToken)) {
|
|
114
|
+
const arrayType = paramType.getNumberIndexType();
|
|
115
|
+
if (arrayType) {
|
|
116
|
+
paramType = arrayType;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
116
119
|
compareSymbols(paramType, argType, node.arguments[i], context);
|
|
117
120
|
}
|
|
118
121
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object-literal.js","sourceRoot":"","sources":["../src/object-literal.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA2E;AAE3E,4DAA4B;AAC5B,sDAAwC;AAYxC,MAAM,UAAU,GAAG,mBAAW,CAAC,WAAW,CACxC,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"object-literal.js","sourceRoot":"","sources":["../src/object-literal.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA2E;AAE3E,4DAA4B;AAC5B,sDAAwC;AAYxC,MAAM,UAAU,GAAG,mBAAW,CAAC,WAAW,CACxC,GAAG,EAAE,CAAC,qEAAqE,CAC5E,CAAC;AAEF,SAAS,mBAAmB,CAAC,IAAa;IACxC,MAAM,QAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAEhD,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAC/D,EAAE,CACH,CAAC;AACJ,CAAC;AAED,SAAS,eAAe,CAAC,IAAa;IACpC,MAAM,QAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAEhD,OAAO,QAAQ,CAAC,IAAI,CAClB,CAAC,CAAC,EAAE,EAAE,CACH,CAAwB,CAAC,MAAM,KAAK,SAAS;QAC9C,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,EAAE,oBAAE,CAAC,WAAW,CAAC,aAAa,CAAC,CAClE,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,iBAA2B,EAC3B,kBAA4B,EAC5B,SAAwB,EACxB,OAAwD;IAExD,IAAI,iBAAiB,CAAC,MAAM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AQ,CAAC,iBAAiB,CACzC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,cAAc,CACtC,CAAC;gBACF,MAAM,SAAS,GAAG,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAExD,cAAc,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC1D,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;IAClB,IAAI,EAAE;QACJ,IAAI,EAAE;YACJ,WAAW,EAAE,0DAA0D;YACvE,WAAW,EAAE,IAAI;YACjB,oBAAoB,EAAE,IAAI;SAC3B;QACD,QAAQ,EAAE;YACR,kBAAkB,EAAE,qDAAqD;SAC1E;QACD,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,YAAY;KACnB;IACD,IAAI,EAAE,gBAAgB;CACvB,CAAC,CAAC;AAEH,kBAAe,kBAAkB,CAAC"}
|
package/eslint.config.mjs
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import eslint from "@eslint/js";
|
|
2
|
-
import tseslint from "typescript-eslint";
|
|
3
|
-
import eslintPlugin from "eslint-plugin-eslint-plugin";
|
|
4
|
-
|
|
5
|
-
export default tseslint.config({
|
|
6
|
-
extends: [
|
|
7
|
-
eslint.configs.recommended,
|
|
8
|
-
eslintPlugin.configs["flat/recommended"],
|
|
9
|
-
tseslint.configs.strictTypeChecked,
|
|
10
|
-
tseslint.configs.stylisticTypeChecked,
|
|
11
|
-
],
|
|
12
|
-
ignores: ["dist/**", "*.mjs", "vitest.config.ts"],
|
|
13
|
-
languageOptions: {
|
|
14
|
-
parserOptions: {
|
|
15
|
-
projectService: true,
|
|
16
|
-
tsconfigRootDir: import.meta.dirname,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
});
|
|
1
|
+
import eslint from "@eslint/js";
|
|
2
|
+
import tseslint from "typescript-eslint";
|
|
3
|
+
import eslintPlugin from "eslint-plugin-eslint-plugin";
|
|
4
|
+
|
|
5
|
+
export default tseslint.config({
|
|
6
|
+
extends: [
|
|
7
|
+
eslint.configs.recommended,
|
|
8
|
+
eslintPlugin.configs["flat/recommended"],
|
|
9
|
+
tseslint.configs.strictTypeChecked,
|
|
10
|
+
tseslint.configs.stylisticTypeChecked,
|
|
11
|
+
],
|
|
12
|
+
ignores: ["dist/**", "*.mjs", "vitest.config.ts"],
|
|
13
|
+
languageOptions: {
|
|
14
|
+
parserOptions: {
|
|
15
|
+
projectService: true,
|
|
16
|
+
tsconfigRootDir: import.meta.dirname,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "eslint-plugin-no-excess-properties",
|
|
3
3
|
"description": "Excess properties are not allowed on object literals",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.6",
|
|
6
6
|
"homepage": "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"eslint-plugin",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"main": "dist/index.js",
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "tsc -p tsconfig.build.json",
|
|
26
|
+
"format": "prettier --write .",
|
|
26
27
|
"lint": "eslint .",
|
|
27
28
|
"prepare": "npm run build",
|
|
28
29
|
"test": "vitest"
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import objectLiteral from "./object-literal";
|
|
3
|
-
|
|
4
|
-
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8")) as {
|
|
5
|
-
name: string;
|
|
6
|
-
version: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const plugin = {
|
|
10
|
-
configs: {
|
|
11
|
-
get recommended() {
|
|
12
|
-
return {
|
|
13
|
-
plugins: {
|
|
14
|
-
"no-excess-properties": plugin,
|
|
15
|
-
},
|
|
16
|
-
rules: {
|
|
17
|
-
"no-excess-properties/object-literal": "warn",
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
meta: {
|
|
23
|
-
name: pkg.name,
|
|
24
|
-
version: pkg.version,
|
|
25
|
-
},
|
|
26
|
-
rules: {
|
|
27
|
-
"object-literal": objectLiteral,
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export = plugin;
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import objectLiteral from "./object-literal";
|
|
3
|
+
|
|
4
|
+
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8")) as {
|
|
5
|
+
name: string;
|
|
6
|
+
version: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const plugin = {
|
|
10
|
+
configs: {
|
|
11
|
+
get recommended() {
|
|
12
|
+
return {
|
|
13
|
+
plugins: {
|
|
14
|
+
"no-excess-properties": plugin,
|
|
15
|
+
},
|
|
16
|
+
rules: {
|
|
17
|
+
"no-excess-properties/object-literal": "warn",
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
meta: {
|
|
23
|
+
name: pkg.name,
|
|
24
|
+
version: pkg.version,
|
|
25
|
+
},
|
|
26
|
+
rules: {
|
|
27
|
+
"object-literal": objectLiteral,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export = plugin;
|
|
@@ -1,198 +1,227 @@
|
|
|
1
|
-
import { RuleTester, TestCaseError } from "@typescript-eslint/rule-tester";
|
|
2
|
-
import objectLiteral from "./object-literal";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import * as vitest from "vitest";
|
|
5
|
-
|
|
6
|
-
RuleTester.afterAll = vitest.afterAll;
|
|
7
|
-
RuleTester.it = vitest.it;
|
|
8
|
-
RuleTester.itOnly = vitest.it.only;
|
|
9
|
-
RuleTester.describe = vitest.describe;
|
|
10
|
-
|
|
11
|
-
function createError(props: {
|
|
12
|
-
line: number;
|
|
13
|
-
column: number;
|
|
14
|
-
endColumn: number;
|
|
15
|
-
endLine?: number;
|
|
16
|
-
}): TestCaseError<"noExcessProperties"> {
|
|
17
|
-
return {
|
|
18
|
-
column: props.column,
|
|
19
|
-
endColumn: props.endColumn,
|
|
20
|
-
endLine: props.endLine ?? props.line,
|
|
21
|
-
line: props.line,
|
|
22
|
-
messageId: "noExcessProperties",
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const ruleTester = new RuleTester({
|
|
27
|
-
languageOptions: {
|
|
28
|
-
parserOptions: {
|
|
29
|
-
projectService: {
|
|
30
|
-
allowDefaultProject: ["*.ts*"],
|
|
31
|
-
defaultProject: "tsconfig.json",
|
|
32
|
-
},
|
|
33
|
-
tsconfigRootDir: path.join(__dirname, ".."),
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
ruleTester.run("object-literal", objectLiteral, {
|
|
39
|
-
valid: [
|
|
40
|
-
`
|
|
41
|
-
let test1: { prop1: number; } = { prop1: 1 };
|
|
42
|
-
const test2 = { prop1: 2 };
|
|
43
|
-
test1 = test2;
|
|
44
|
-
`,
|
|
45
|
-
`
|
|
46
|
-
let test1: () => { prop1: number; } = () => ({ prop1: 1 });
|
|
47
|
-
const test2 = () => ({ prop1: 2 });
|
|
48
|
-
test1 = test2;
|
|
49
|
-
`,
|
|
50
|
-
],
|
|
51
|
-
invalid: [
|
|
52
|
-
{
|
|
53
|
-
code: `
|
|
54
|
-
let test1: { prop1: number; } = { prop1: 1 };
|
|
55
|
-
const test2 = { prop1: 2, prop2: 3 };
|
|
56
|
-
test1 = test2;
|
|
57
|
-
`,
|
|
58
|
-
errors: [createError({ column: 17, endColumn: 22, line: 4 })],
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
code: `
|
|
62
|
-
let test1: () => { prop1: number; } = () => ({ prop1: 1 });
|
|
63
|
-
const test2 = () => ({ prop1: 2, prop2: 3 });
|
|
64
|
-
test1 = test2;
|
|
65
|
-
`,
|
|
66
|
-
errors: [createError({ column: 17, endColumn: 22, line: 4 })],
|
|
67
|
-
},
|
|
68
|
-
],
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
ruleTester.run("object-literal", objectLiteral, {
|
|
72
|
-
valid: [
|
|
73
|
-
`
|
|
74
|
-
const test: () => { prop1: number; } = () => ({ prop1: 1 })
|
|
75
|
-
`,
|
|
76
|
-
`
|
|
77
|
-
const test1: { prop2: number; } = { prop2: 1 };
|
|
78
|
-
const test2: { prop2: number } = { ...test1 };
|
|
79
|
-
`,
|
|
80
|
-
`
|
|
81
|
-
const test1 = { prop1: 2 };
|
|
82
|
-
const test2: { prop2: { prop3: { prop1: number; } } } = { prop2: { prop3: test1 } };
|
|
83
|
-
`,
|
|
84
|
-
],
|
|
85
|
-
invalid: [
|
|
86
|
-
{
|
|
87
|
-
code: `
|
|
88
|
-
const test: () => { prop1: number; } = () => ({ prop1: 1, prop2: 2 })
|
|
89
|
-
`,
|
|
90
|
-
errors: [createError({ column: 48, endColumn: 78, line: 2 })],
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
code: `
|
|
94
|
-
const test1: { prop1: number; } = { prop1: 1 };
|
|
95
|
-
const test2: { prop2: number } = { ...test1, prop2: 2 };
|
|
96
|
-
`,
|
|
97
|
-
errors: [createError({ column: 42, endColumn: 64, line: 3 })],
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
code: `
|
|
101
|
-
const test1 = { prop1: 1, prop4: 2 };
|
|
102
|
-
const test2: { prop2: { prop3: { prop1: number; } } } = { prop2: { prop3: test1 } };
|
|
103
|
-
`,
|
|
104
|
-
errors: [createError({ column: 76, endColumn: 88, line: 3 })],
|
|
105
|
-
},
|
|
106
|
-
],
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
ruleTester.run("object-literal", objectLiteral, {
|
|
110
|
-
valid: [
|
|
111
|
-
`
|
|
112
|
-
function test(param1: { prop1: number }) {}
|
|
113
|
-
test({ prop1: 1 });
|
|
114
|
-
`,
|
|
115
|
-
`
|
|
116
|
-
function test(param1: () => { prop1: number }) {}
|
|
117
|
-
test(() => ({ prop1: 1 }));
|
|
118
|
-
`,
|
|
119
|
-
`
|
|
120
|
-
function test(param1: number, param2: { prop1: number } | null) {}
|
|
121
|
-
test(1, true ? { prop1: 1 } : null);
|
|
122
|
-
`,
|
|
123
|
-
],
|
|
124
|
-
invalid: [
|
|
125
|
-
{
|
|
126
|
-
code: `
|
|
127
|
-
function test(param1: { prop1: number }) {}
|
|
128
|
-
test({ prop1: 1, prop2: 2 });
|
|
129
|
-
`,
|
|
130
|
-
errors: [createError({ column: 14, endColumn: 36, line: 3 })],
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
code: `
|
|
134
|
-
function test(param1: () => { prop1: number }) {}
|
|
135
|
-
test(() => ({ prop1: 1, prop2: 2 }));
|
|
136
|
-
`,
|
|
137
|
-
errors: [createError({ column: 14, endColumn: 44, line: 3 })],
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
code: `
|
|
141
|
-
function test(param1: number, param2: { prop1: number } | null) {}
|
|
142
|
-
test(1, true ? { prop1: 1, prop2: 2 } : null);
|
|
143
|
-
`,
|
|
144
|
-
errors: [createError({ column: 17, endColumn: 53, line: 3 })],
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
ruleTester.run("object-literal", objectLiteral, {
|
|
150
|
-
valid: [
|
|
151
|
-
`
|
|
152
|
-
const test1 = { prop1: 1 };
|
|
153
|
-
function test(): { prop1: number } { return test1 }
|
|
154
|
-
`,
|
|
155
|
-
`
|
|
156
|
-
const test1 = { prop1: 1 };
|
|
157
|
-
async function test(): Promise<{ prop1: number }> { return test1 }
|
|
158
|
-
`,
|
|
159
|
-
],
|
|
160
|
-
invalid: [
|
|
161
|
-
{
|
|
162
|
-
code: `
|
|
163
|
-
const test1 = { prop1: 1, prop2: 2 };
|
|
164
|
-
function test(): { prop1: number } { return test1 }
|
|
165
|
-
`,
|
|
166
|
-
errors: [createError({ column: 53, endColumn: 58, line: 3 })],
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
code: `
|
|
170
|
-
const test1 = { prop1: 1, prop2: 2 };
|
|
171
|
-
async function test(): Promise<{ prop1: number }> { return test1 }
|
|
172
|
-
`,
|
|
173
|
-
errors: [createError({ column: 68, endColumn: 73, line: 3 })],
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
ruleTester.run("object-literal", objectLiteral, {
|
|
179
|
-
valid: [
|
|
180
|
-
`
|
|
181
|
-
const
|
|
182
|
-
`,
|
|
183
|
-
`
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
})
|
|
1
|
+
import { RuleTester, TestCaseError } from "@typescript-eslint/rule-tester";
|
|
2
|
+
import objectLiteral from "./object-literal";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import * as vitest from "vitest";
|
|
5
|
+
|
|
6
|
+
RuleTester.afterAll = vitest.afterAll;
|
|
7
|
+
RuleTester.it = vitest.it;
|
|
8
|
+
RuleTester.itOnly = vitest.it.only;
|
|
9
|
+
RuleTester.describe = vitest.describe;
|
|
10
|
+
|
|
11
|
+
function createError(props: {
|
|
12
|
+
line: number;
|
|
13
|
+
column: number;
|
|
14
|
+
endColumn: number;
|
|
15
|
+
endLine?: number;
|
|
16
|
+
}): TestCaseError<"noExcessProperties"> {
|
|
17
|
+
return {
|
|
18
|
+
column: props.column,
|
|
19
|
+
endColumn: props.endColumn,
|
|
20
|
+
endLine: props.endLine ?? props.line,
|
|
21
|
+
line: props.line,
|
|
22
|
+
messageId: "noExcessProperties",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ruleTester = new RuleTester({
|
|
27
|
+
languageOptions: {
|
|
28
|
+
parserOptions: {
|
|
29
|
+
projectService: {
|
|
30
|
+
allowDefaultProject: ["*.ts*"],
|
|
31
|
+
defaultProject: "tsconfig.json",
|
|
32
|
+
},
|
|
33
|
+
tsconfigRootDir: path.join(__dirname, ".."),
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
39
|
+
valid: [
|
|
40
|
+
`
|
|
41
|
+
let test1: { prop1: number; } = { prop1: 1 };
|
|
42
|
+
const test2 = { prop1: 2 };
|
|
43
|
+
test1 = test2;
|
|
44
|
+
`,
|
|
45
|
+
`
|
|
46
|
+
let test1: () => { prop1: number; } = () => ({ prop1: 1 });
|
|
47
|
+
const test2 = () => ({ prop1: 2 });
|
|
48
|
+
test1 = test2;
|
|
49
|
+
`,
|
|
50
|
+
],
|
|
51
|
+
invalid: [
|
|
52
|
+
{
|
|
53
|
+
code: `
|
|
54
|
+
let test1: { prop1: number; } = { prop1: 1 };
|
|
55
|
+
const test2 = { prop1: 2, prop2: 3 };
|
|
56
|
+
test1 = test2;
|
|
57
|
+
`,
|
|
58
|
+
errors: [createError({ column: 17, endColumn: 22, line: 4 })],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
code: `
|
|
62
|
+
let test1: () => { prop1: number; } = () => ({ prop1: 1 });
|
|
63
|
+
const test2 = () => ({ prop1: 2, prop2: 3 });
|
|
64
|
+
test1 = test2;
|
|
65
|
+
`,
|
|
66
|
+
errors: [createError({ column: 17, endColumn: 22, line: 4 })],
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
72
|
+
valid: [
|
|
73
|
+
`
|
|
74
|
+
const test: () => { prop1: number; } = () => ({ prop1: 1 })
|
|
75
|
+
`,
|
|
76
|
+
`
|
|
77
|
+
const test1: { prop2: number; } = { prop2: 1 };
|
|
78
|
+
const test2: { prop2: number } = { ...test1 };
|
|
79
|
+
`,
|
|
80
|
+
`
|
|
81
|
+
const test1 = { prop1: 2 };
|
|
82
|
+
const test2: { prop2: { prop3: { prop1: number; } } } = { prop2: { prop3: test1 } };
|
|
83
|
+
`,
|
|
84
|
+
],
|
|
85
|
+
invalid: [
|
|
86
|
+
{
|
|
87
|
+
code: `
|
|
88
|
+
const test: () => { prop1: number; } = () => ({ prop1: 1, prop2: 2 })
|
|
89
|
+
`,
|
|
90
|
+
errors: [createError({ column: 48, endColumn: 78, line: 2 })],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
code: `
|
|
94
|
+
const test1: { prop1: number; } = { prop1: 1 };
|
|
95
|
+
const test2: { prop2: number } = { ...test1, prop2: 2 };
|
|
96
|
+
`,
|
|
97
|
+
errors: [createError({ column: 42, endColumn: 64, line: 3 })],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
code: `
|
|
101
|
+
const test1 = { prop1: 1, prop4: 2 };
|
|
102
|
+
const test2: { prop2: { prop3: { prop1: number; } } } = { prop2: { prop3: test1 } };
|
|
103
|
+
`,
|
|
104
|
+
errors: [createError({ column: 76, endColumn: 88, line: 3 })],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
110
|
+
valid: [
|
|
111
|
+
`
|
|
112
|
+
function test(param1: { prop1: number }) {}
|
|
113
|
+
test({ prop1: 1 });
|
|
114
|
+
`,
|
|
115
|
+
`
|
|
116
|
+
function test(param1: () => { prop1: number }) {}
|
|
117
|
+
test(() => ({ prop1: 1 }));
|
|
118
|
+
`,
|
|
119
|
+
`
|
|
120
|
+
function test(param1: number, param2: { prop1: number } | null) {}
|
|
121
|
+
test(1, true ? { prop1: 1 } : null);
|
|
122
|
+
`,
|
|
123
|
+
],
|
|
124
|
+
invalid: [
|
|
125
|
+
{
|
|
126
|
+
code: `
|
|
127
|
+
function test(param1: { prop1: number }) {}
|
|
128
|
+
test({ prop1: 1, prop2: 2 });
|
|
129
|
+
`,
|
|
130
|
+
errors: [createError({ column: 14, endColumn: 36, line: 3 })],
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
code: `
|
|
134
|
+
function test(param1: () => { prop1: number }) {}
|
|
135
|
+
test(() => ({ prop1: 1, prop2: 2 }));
|
|
136
|
+
`,
|
|
137
|
+
errors: [createError({ column: 14, endColumn: 44, line: 3 })],
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
code: `
|
|
141
|
+
function test(param1: number, param2: { prop1: number } | null) {}
|
|
142
|
+
test(1, true ? { prop1: 1, prop2: 2 } : null);
|
|
143
|
+
`,
|
|
144
|
+
errors: [createError({ column: 17, endColumn: 53, line: 3 })],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
150
|
+
valid: [
|
|
151
|
+
`
|
|
152
|
+
const test1 = { prop1: 1 };
|
|
153
|
+
function test(): { prop1: number } { return test1 }
|
|
154
|
+
`,
|
|
155
|
+
`
|
|
156
|
+
const test1 = { prop1: 1 };
|
|
157
|
+
async function test(): Promise<{ prop1: number }> { return test1 }
|
|
158
|
+
`,
|
|
159
|
+
],
|
|
160
|
+
invalid: [
|
|
161
|
+
{
|
|
162
|
+
code: `
|
|
163
|
+
const test1 = { prop1: 1, prop2: 2 };
|
|
164
|
+
function test(): { prop1: number } { return test1 }
|
|
165
|
+
`,
|
|
166
|
+
errors: [createError({ column: 53, endColumn: 58, line: 3 })],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
code: `
|
|
170
|
+
const test1 = { prop1: 1, prop2: 2 };
|
|
171
|
+
async function test(): Promise<{ prop1: number }> { return test1 }
|
|
172
|
+
`,
|
|
173
|
+
errors: [createError({ column: 68, endColumn: 73, line: 3 })],
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
179
|
+
valid: [
|
|
180
|
+
`
|
|
181
|
+
const test1: { prop1: 1 }[] = [{ prop1: 1 }].map(a => ({ ...a, prop1: 2 }))
|
|
182
|
+
`,
|
|
183
|
+
`
|
|
184
|
+
const test: { prop1: number; }[] = [];
|
|
185
|
+
test.push({ prop1: 1 })
|
|
186
|
+
`,
|
|
187
|
+
],
|
|
188
|
+
invalid: [
|
|
189
|
+
{
|
|
190
|
+
code: `
|
|
191
|
+
const test1: { prop1: 1 }[] = [{ prop1: 1 }].map(a => ({ ...a, prop2: 2 }))
|
|
192
|
+
`,
|
|
193
|
+
errors: [createError({ column: 39, endColumn: 84, line: 2 })],
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
code: `
|
|
197
|
+
const test: { prop1: number; }[] = [];
|
|
198
|
+
test.push({ prop1: 1, prop: 2 })
|
|
199
|
+
`,
|
|
200
|
+
errors: [createError({ column: 19, endColumn: 40, line: 3 })],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
ruleTester.run("object-literal", objectLiteral, {
|
|
206
|
+
valid: [
|
|
207
|
+
`
|
|
208
|
+
const test: Record<string, number> & { prop2: 1 } = { prop1: 1 };
|
|
209
|
+
`,
|
|
210
|
+
`
|
|
211
|
+
const test: { prop2: 1 } | Record<string, number> = { prop1: 1 };
|
|
212
|
+
`,
|
|
213
|
+
`
|
|
214
|
+
const test: Record<number, number> & { prop2: 1 } = { 1: 1 };
|
|
215
|
+
`,
|
|
216
|
+
`
|
|
217
|
+
interface Test1 { prop1: number }
|
|
218
|
+
interface Test2 extends Test1 { prop2: number }
|
|
219
|
+
const test1: Test2 = { prop1: 1, prop2: 1 }
|
|
220
|
+
const test2: Test1 = test1
|
|
221
|
+
`,
|
|
222
|
+
`
|
|
223
|
+
Object.keys({ prop1: 1 })
|
|
224
|
+
`,
|
|
225
|
+
],
|
|
226
|
+
invalid: [],
|
|
227
|
+
});
|
package/src/object-literal.ts
CHANGED
|
@@ -1,218 +1,225 @@
|
|
|
1
|
-
import { ASTUtils, ESLintUtils, TSESTree } from "@typescript-eslint/utils";
|
|
2
|
-
import { RuleContext } from "@typescript-eslint/utils/ts-eslint";
|
|
3
|
-
import ts from "typescript";
|
|
4
|
-
import * as tsutils from "ts-api-utils";
|
|
5
|
-
|
|
6
|
-
export interface PluginDocs {
|
|
7
|
-
description: string;
|
|
8
|
-
recommended?: boolean;
|
|
9
|
-
requiresTypeChecking?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type TypeOptionalSymbol = Omit<ts.Type, "symbol"> & {
|
|
13
|
-
symbol: ts.Symbol | undefined;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const createRule = ESLintUtils.RuleCreator<PluginDocs>(
|
|
17
|
-
() =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
(t
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
if (
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (!
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
1
|
+
import { ASTUtils, ESLintUtils, TSESTree } from "@typescript-eslint/utils";
|
|
2
|
+
import { RuleContext } from "@typescript-eslint/utils/ts-eslint";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import * as tsutils from "ts-api-utils";
|
|
5
|
+
|
|
6
|
+
export interface PluginDocs {
|
|
7
|
+
description: string;
|
|
8
|
+
recommended?: boolean;
|
|
9
|
+
requiresTypeChecking?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type TypeOptionalSymbol = Omit<ts.Type, "symbol"> & {
|
|
13
|
+
symbol: ts.Symbol | undefined;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const createRule = ESLintUtils.RuleCreator<PluginDocs>(
|
|
17
|
+
() => "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties",
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
function getAllPropertyNames(type: ts.Type): string[] {
|
|
21
|
+
const allTypes = tsutils.typeConstituents(type);
|
|
22
|
+
|
|
23
|
+
return allTypes.reduce<string[]>(
|
|
24
|
+
(all, t) => all.concat(...t.getProperties().map((p) => p.name)),
|
|
25
|
+
[],
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isObjectLiteral(type: ts.Type): boolean {
|
|
30
|
+
const allTypes = tsutils.typeConstituents(type);
|
|
31
|
+
|
|
32
|
+
return allTypes.some(
|
|
33
|
+
(t) =>
|
|
34
|
+
(t as TypeOptionalSymbol).symbol !== undefined &&
|
|
35
|
+
tsutils.isSymbolFlagSet(t.symbol, ts.SymbolFlags.ObjectLiteral),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function compareNames(
|
|
40
|
+
leftPropertyNames: string[],
|
|
41
|
+
rightPropertyNames: string[],
|
|
42
|
+
rightNode: TSESTree.Node,
|
|
43
|
+
context: Readonly<RuleContext<"noExcessProperties", []>>,
|
|
44
|
+
): void {
|
|
45
|
+
if (leftPropertyNames.length <= 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const excessPropertyNames = rightPropertyNames.filter(
|
|
50
|
+
(n) => !leftPropertyNames.includes(n),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (excessPropertyNames.length > 0) {
|
|
54
|
+
context.report({
|
|
55
|
+
data: { excessPropertyNames: excessPropertyNames.join(", ") },
|
|
56
|
+
messageId: "noExcessProperties",
|
|
57
|
+
node: rightNode,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function compareSymbols(
|
|
63
|
+
leftType: ts.Type,
|
|
64
|
+
rightType: ts.Type,
|
|
65
|
+
rightNode: TSESTree.Node,
|
|
66
|
+
context: Readonly<RuleContext<"noExcessProperties", []>>,
|
|
67
|
+
): void {
|
|
68
|
+
const leftCallSignatures = leftType.getCallSignatures();
|
|
69
|
+
if (leftCallSignatures.length === 1) {
|
|
70
|
+
leftType = leftCallSignatures[0].getReturnType();
|
|
71
|
+
}
|
|
72
|
+
const rightCallSignatures = rightType.getCallSignatures();
|
|
73
|
+
if (rightCallSignatures.length === 1) {
|
|
74
|
+
rightType = rightCallSignatures[0].getReturnType();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const leftArrayType = leftType.getNumberIndexType();
|
|
78
|
+
const rightArrayType = rightType.getNumberIndexType();
|
|
79
|
+
if (leftArrayType && rightArrayType) {
|
|
80
|
+
leftType = leftArrayType;
|
|
81
|
+
rightType = rightArrayType;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (
|
|
85
|
+
isObjectLiteral(rightType) &&
|
|
86
|
+
tsutils
|
|
87
|
+
.typeConstituents(leftType)
|
|
88
|
+
.every((t) => !t.getStringIndexType() && !t.getNumberIndexType())
|
|
89
|
+
) {
|
|
90
|
+
compareNames(
|
|
91
|
+
getAllPropertyNames(leftType),
|
|
92
|
+
getAllPropertyNames(rightType),
|
|
93
|
+
rightNode,
|
|
94
|
+
context,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const noExcessProperties = createRule({
|
|
100
|
+
create: function (context) {
|
|
101
|
+
const services = ESLintUtils.getParserServices(context);
|
|
102
|
+
const typeChecker = services.program.getTypeChecker();
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
AssignmentExpression(node) {
|
|
106
|
+
const leftType = services.getTypeAtLocation(node.left);
|
|
107
|
+
const rightType = services.getTypeAtLocation(node.right);
|
|
108
|
+
|
|
109
|
+
compareSymbols(leftType, rightType, node.right, context);
|
|
110
|
+
},
|
|
111
|
+
CallExpression(node) {
|
|
112
|
+
if (node.arguments.length <= 0) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const functionNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
117
|
+
const functionSignature =
|
|
118
|
+
typeChecker.getResolvedSignature(functionNode);
|
|
119
|
+
|
|
120
|
+
if (!functionSignature) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (let i = 0; i < functionSignature.parameters.length; i++) {
|
|
125
|
+
if (i > node.arguments.length - 1) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const argType = services.getTypeAtLocation(node.arguments[i]);
|
|
130
|
+
let paramType = typeChecker.getTypeOfSymbolAtLocation(
|
|
131
|
+
functionSignature.parameters[i],
|
|
132
|
+
functionNode,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const declarations = functionSignature.parameters[i].declarations;
|
|
136
|
+
if (
|
|
137
|
+
declarations?.some((d) => ts.isParameter(d) && d.dotDotDotToken)
|
|
138
|
+
) {
|
|
139
|
+
const arrayType = paramType.getNumberIndexType();
|
|
140
|
+
if (arrayType) {
|
|
141
|
+
paramType = arrayType;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
compareSymbols(paramType, argType, node.arguments[i], context);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
Property(node) {
|
|
149
|
+
const leftNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
150
|
+
|
|
151
|
+
if (leftNode.kind !== ts.SyntaxKind.PropertyAssignment) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const leftType = typeChecker.getContextualType(leftNode.initializer);
|
|
156
|
+
const rightType = services.getTypeAtLocation(node);
|
|
157
|
+
|
|
158
|
+
if (!leftType) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
compareSymbols(leftType, rightType, node, context);
|
|
163
|
+
},
|
|
164
|
+
ReturnStatement(node) {
|
|
165
|
+
if (!node.argument) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let functionNode: TSESTree.Node | undefined = node.parent;
|
|
170
|
+
while (functionNode && !ASTUtils.isFunction(functionNode)) {
|
|
171
|
+
functionNode = functionNode.parent;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (!functionNode?.returnType) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let returnType = services.getTypeAtLocation(
|
|
179
|
+
functionNode.returnType.typeAnnotation,
|
|
180
|
+
);
|
|
181
|
+
if (
|
|
182
|
+
(returnType as TypeOptionalSymbol).symbol?.name === "Promise" &&
|
|
183
|
+
tsutils.isTypeReference(returnType)
|
|
184
|
+
) {
|
|
185
|
+
const promiseTypes = typeChecker.getTypeArguments(returnType);
|
|
186
|
+
if (promiseTypes.length === 1) {
|
|
187
|
+
returnType = promiseTypes[0];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const argType = services.getTypeAtLocation(node.argument);
|
|
192
|
+
|
|
193
|
+
compareSymbols(returnType, argType, node.argument, context);
|
|
194
|
+
},
|
|
195
|
+
VariableDeclarator(node) {
|
|
196
|
+
if (!node.id.typeAnnotation || !node.init) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const leftType = services.getTypeAtLocation(
|
|
201
|
+
node.id.typeAnnotation.typeAnnotation,
|
|
202
|
+
);
|
|
203
|
+
const rightType = services.getTypeAtLocation(node.init);
|
|
204
|
+
|
|
205
|
+
compareSymbols(leftType, rightType, node.init, context);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
defaultOptions: [],
|
|
210
|
+
meta: {
|
|
211
|
+
docs: {
|
|
212
|
+
description: "Warn when excess properties are found on object literals",
|
|
213
|
+
recommended: true,
|
|
214
|
+
requiresTypeChecking: true,
|
|
215
|
+
},
|
|
216
|
+
messages: {
|
|
217
|
+
noExcessProperties: "Excess properties '{{ excessPropertyNames }}' found",
|
|
218
|
+
},
|
|
219
|
+
schema: [],
|
|
220
|
+
type: "suggestion",
|
|
221
|
+
},
|
|
222
|
+
name: "object-literal",
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
export default noExcessProperties;
|