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 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
@@ -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/README.md");
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
- if (leftType.getStringIndexType() || leftType.getNumberIndexType()) {
67
- return;
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
- let leftPropertyNames = [];
70
- let rightPropertyNames = [];
71
- if (isObjectLiteral(rightType)) {
72
- leftPropertyNames = getAllPropertyNames(leftType);
73
- rightPropertyNames = getAllPropertyNames(rightType);
74
+ const leftArrayType = leftType.getNumberIndexType();
75
+ const rightArrayType = rightType.getNumberIndexType();
76
+ if (leftArrayType && rightArrayType) {
77
+ leftType = leftArrayType;
78
+ rightType = rightArrayType;
74
79
  }
75
- if (leftPropertyNames.length <= 0 || rightPropertyNames.length <= 0) {
76
- const leftCallSignatures = leftType.getCallSignatures();
77
- if (leftCallSignatures.length === 1) {
78
- const returnType = leftCallSignatures[0].getReturnType();
79
- leftPropertyNames = getAllPropertyNames(returnType);
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
- const paramType = typeChecker.getTypeOfSymbolAtLocation(functionSignature.parameters[i], functionNode);
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,CACH,+EAA+E,CAClF,CAAC;AAEF,SAAS,mBAAmB,CAAC,IA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E;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.4",
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 test: Record<string, number> & { prop2: 1 } = { prop1: 1 };
182
- `,
183
- `
184
- interface Test1 { prop1: number }
185
- interface Test2 extends Test1 { prop2: number }
186
- const test1: Test2 = { prop1: 1, prop2: 1 }
187
- const test2: Test1 = test1
188
- `,
189
- `
190
- Object.keys({ prop1: 1 })
191
- `,
192
- `
193
- const test: { prop1: number; }[] = [];
194
- test.push({ prop1: 1 })
195
- `,
196
- ],
197
- invalid: [],
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
+ });
@@ -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
- "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties/README.md"
19
- );
20
-
21
- function getAllPropertyNames(type: ts.Type): string[] {
22
- const allTypes = tsutils.typeConstituents(type);
23
-
24
- return allTypes.reduce<string[]>(
25
- (all, t) => all.concat(...t.getProperties().map((p) => p.name)),
26
- []
27
- );
28
- }
29
-
30
- function isObjectLiteral(type: ts.Type): boolean {
31
- const allTypes = tsutils.typeConstituents(type);
32
-
33
- return allTypes.some(
34
- (t) =>
35
- (t as TypeOptionalSymbol).symbol !== undefined &&
36
- tsutils.isSymbolFlagSet(t.symbol, ts.SymbolFlags.ObjectLiteral)
37
- );
38
- }
39
-
40
- function compareNames(
41
- leftPropertyNames: string[],
42
- rightPropertyNames: string[],
43
- rightNode: TSESTree.Node,
44
- context: Readonly<RuleContext<"noExcessProperties", []>>
45
- ): void {
46
- if (leftPropertyNames.length <= 0) {
47
- return;
48
- }
49
-
50
- const excessPropertyNames = rightPropertyNames.filter(
51
- (n) => !leftPropertyNames.includes(n)
52
- );
53
-
54
- if (excessPropertyNames.length > 0) {
55
- context.report({
56
- data: { excessPropertyNames: excessPropertyNames.join(", ") },
57
- messageId: "noExcessProperties",
58
- node: rightNode,
59
- });
60
- }
61
- }
62
-
63
- function compareSymbols(
64
- leftType: ts.Type,
65
- rightType: ts.Type,
66
- rightNode: TSESTree.Node,
67
- context: Readonly<RuleContext<"noExcessProperties", []>>
68
- ): void {
69
- if (leftType.getStringIndexType() || leftType.getNumberIndexType()) {
70
- return;
71
- }
72
-
73
- let leftPropertyNames: string[] = [];
74
- let rightPropertyNames: string[] = [];
75
-
76
- if (isObjectLiteral(rightType)) {
77
- leftPropertyNames = getAllPropertyNames(leftType);
78
- rightPropertyNames = getAllPropertyNames(rightType);
79
- }
80
-
81
- if (leftPropertyNames.length <= 0 || rightPropertyNames.length <= 0) {
82
- const leftCallSignatures = leftType.getCallSignatures();
83
- if (leftCallSignatures.length === 1) {
84
- const returnType = leftCallSignatures[0].getReturnType();
85
-
86
- leftPropertyNames = getAllPropertyNames(returnType);
87
- }
88
-
89
- const rightCallSignatures = rightType.getCallSignatures();
90
- if (rightCallSignatures.length === 1) {
91
- const returnType = rightCallSignatures[0].getReturnType();
92
-
93
- if (isObjectLiteral(returnType)) {
94
- rightPropertyNames = getAllPropertyNames(returnType);
95
- }
96
- }
97
- }
98
-
99
- compareNames(leftPropertyNames, rightPropertyNames, rightNode, context);
100
- }
101
-
102
- const noExcessProperties = createRule({
103
- create: function (context) {
104
- const services = ESLintUtils.getParserServices(context);
105
- const typeChecker = services.program.getTypeChecker();
106
-
107
- return {
108
- AssignmentExpression(node) {
109
- const leftType = services.getTypeAtLocation(node.left);
110
- const rightType = services.getTypeAtLocation(node.right);
111
-
112
- compareSymbols(leftType, rightType, node.right, context);
113
- },
114
- CallExpression(node) {
115
- if (node.arguments.length <= 0) {
116
- return;
117
- }
118
-
119
- const functionNode = services.esTreeNodeToTSNodeMap.get(node);
120
- const functionSignature =
121
- typeChecker.getResolvedSignature(functionNode);
122
-
123
- if (!functionSignature) {
124
- return;
125
- }
126
-
127
- for (let i = 0; i < functionSignature.parameters.length; i++) {
128
- if (i > node.arguments.length - 1) {
129
- break;
130
- }
131
-
132
- const argType = services.getTypeAtLocation(node.arguments[i]);
133
- const paramType = typeChecker.getTypeOfSymbolAtLocation(
134
- functionSignature.parameters[i],
135
- functionNode
136
- );
137
-
138
- compareSymbols(paramType, argType, node.arguments[i], context);
139
- }
140
- },
141
- Property(node) {
142
- const leftNode = services.esTreeNodeToTSNodeMap.get(node);
143
-
144
- if (leftNode.kind !== ts.SyntaxKind.PropertyAssignment) {
145
- return;
146
- }
147
-
148
- const leftType = typeChecker.getContextualType(leftNode.initializer);
149
- const rightType = services.getTypeAtLocation(node);
150
-
151
- if (!leftType) {
152
- return;
153
- }
154
-
155
- compareSymbols(leftType, rightType, node, context);
156
- },
157
- ReturnStatement(node) {
158
- if (!node.argument) {
159
- return;
160
- }
161
-
162
- let functionNode: TSESTree.Node | undefined = node.parent;
163
- while (functionNode && !ASTUtils.isFunction(functionNode)) {
164
- functionNode = functionNode.parent;
165
- }
166
-
167
- if (!functionNode?.returnType) {
168
- return;
169
- }
170
-
171
- let returnType = services.getTypeAtLocation(
172
- functionNode.returnType.typeAnnotation
173
- );
174
- if (
175
- (returnType as TypeOptionalSymbol).symbol?.name === "Promise" &&
176
- tsutils.isTypeReference(returnType)
177
- ) {
178
- const promiseTypes = typeChecker.getTypeArguments(returnType);
179
- if (promiseTypes.length === 1) {
180
- returnType = promiseTypes[0];
181
- }
182
- }
183
-
184
- const argType = services.getTypeAtLocation(node.argument);
185
-
186
- compareSymbols(returnType, argType, node.argument, context);
187
- },
188
- VariableDeclarator(node) {
189
- if (!node.id.typeAnnotation || !node.init) {
190
- return;
191
- }
192
-
193
- const leftType = services.getTypeAtLocation(
194
- node.id.typeAnnotation.typeAnnotation
195
- );
196
- const rightType = services.getTypeAtLocation(node.init);
197
-
198
- compareSymbols(leftType, rightType, node.init, context);
199
- },
200
- };
201
- },
202
- defaultOptions: [],
203
- meta: {
204
- docs: {
205
- description: "Warn when excess properties are found on object literals",
206
- recommended: true,
207
- requiresTypeChecking: true,
208
- },
209
- messages: {
210
- noExcessProperties: "Excess properties '{{ excessPropertyNames }}' found",
211
- },
212
- schema: [],
213
- type: "suggestion",
214
- },
215
- name: "object-literal",
216
- });
217
-
218
- export default noExcessProperties;
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;