eslint-plugin-no-excess-properties 0.0.6 → 0.0.7

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.
@@ -40,48 +40,61 @@ 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
42
  const createRule = utils_1.ESLintUtils.RuleCreator(() => "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties");
43
- function getAllPropertyNames(type) {
44
- const allTypes = tsutils.typeConstituents(type);
45
- return allTypes.reduce((all, t) => all.concat(...t.getProperties().map((p) => p.name)), []);
46
- }
47
43
  function isObjectLiteral(type) {
48
- const allTypes = tsutils.typeConstituents(type);
49
- return allTypes.some((t) => t.symbol !== undefined &&
50
- tsutils.isSymbolFlagSet(t.symbol, typescript_1.default.SymbolFlags.ObjectLiteral));
44
+ return (type.symbol !== undefined &&
45
+ tsutils.isSymbolFlagSet(type.symbol, typescript_1.default.SymbolFlags.ObjectLiteral));
51
46
  }
52
- function compareNames(leftPropertyNames, rightPropertyNames, rightNode, context) {
53
- if (leftPropertyNames.length <= 0) {
54
- return;
47
+ function resolveType(type) {
48
+ let resolvedType = type;
49
+ const callSignatures = resolvedType.getCallSignatures();
50
+ if (callSignatures.length === 1) {
51
+ resolvedType = callSignatures[0].getReturnType();
55
52
  }
56
- const excessPropertyNames = rightPropertyNames.filter((n) => !leftPropertyNames.includes(n));
57
- if (excessPropertyNames.length > 0) {
58
- context.report({
59
- data: { excessPropertyNames: excessPropertyNames.join(", ") },
60
- messageId: "noExcessProperties",
61
- node: rightNode,
62
- });
53
+ const arrayType = resolvedType.getNumberIndexType();
54
+ if (arrayType) {
55
+ resolvedType = arrayType;
63
56
  }
57
+ return resolvedType;
64
58
  }
65
- function compareSymbols(leftType, rightType, rightNode, context) {
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();
73
- }
74
- const leftArrayType = leftType.getNumberIndexType();
75
- const rightArrayType = rightType.getNumberIndexType();
76
- if (leftArrayType && rightArrayType) {
77
- leftType = leftArrayType;
78
- rightType = rightArrayType;
59
+ function compareTypes(leftType, rightType, rightNode, context) {
60
+ const allLeftTypes = tsutils.unionConstituents(leftType);
61
+ const allRightTypes = tsutils.unionConstituents(rightType);
62
+ if (allLeftTypes.some((t) => t.getStringIndexType() !== undefined ||
63
+ (t.getNumberIndexType() !== undefined &&
64
+ t.symbol?.name !== "Array"))) {
65
+ return;
79
66
  }
80
- if (isObjectLiteral(rightType) &&
81
- tsutils
82
- .typeConstituents(leftType)
83
- .every((t) => !t.getStringIndexType() && !t.getNumberIndexType())) {
84
- compareNames(getAllPropertyNames(leftType), getAllPropertyNames(rightType), rightNode, context);
67
+ for (const rightType of allRightTypes) {
68
+ const rightResolvedType = resolveType(rightType);
69
+ if (!isObjectLiteral(rightResolvedType)) {
70
+ continue;
71
+ }
72
+ const rightPropertyNames = rightResolvedType
73
+ .getProperties()
74
+ .map((p) => p.name);
75
+ let bestMatchExcessPropertyNames = null;
76
+ for (const leftType of allLeftTypes) {
77
+ const leftResolvedType = resolveType(leftType);
78
+ const leftPropertyNames = leftResolvedType
79
+ .getProperties()
80
+ .map((p) => p.name);
81
+ const excessPropertyNames = rightPropertyNames.filter((n) => !leftPropertyNames.includes(n));
82
+ if (leftPropertyNames.length > 0 &&
83
+ (bestMatchExcessPropertyNames === null ||
84
+ excessPropertyNames.length < bestMatchExcessPropertyNames.length)) {
85
+ bestMatchExcessPropertyNames = excessPropertyNames;
86
+ }
87
+ }
88
+ if (bestMatchExcessPropertyNames &&
89
+ bestMatchExcessPropertyNames.length > 0) {
90
+ context.report({
91
+ data: {
92
+ excessPropertyNames: bestMatchExcessPropertyNames.join(", "),
93
+ },
94
+ messageId: "noExcessProperties",
95
+ node: rightNode,
96
+ });
97
+ }
85
98
  }
86
99
  }
87
100
  const noExcessProperties = createRule({
@@ -92,7 +105,7 @@ const noExcessProperties = createRule({
92
105
  AssignmentExpression(node) {
93
106
  const leftType = services.getTypeAtLocation(node.left);
94
107
  const rightType = services.getTypeAtLocation(node.right);
95
- compareSymbols(leftType, rightType, node.right, context);
108
+ compareTypes(leftType, rightType, node.right, context);
96
109
  },
97
110
  CallExpression(node) {
98
111
  if (node.arguments.length <= 0) {
@@ -116,7 +129,7 @@ const noExcessProperties = createRule({
116
129
  paramType = arrayType;
117
130
  }
118
131
  }
119
- compareSymbols(paramType, argType, node.arguments[i], context);
132
+ compareTypes(paramType, argType, node.arguments[i], context);
120
133
  }
121
134
  },
122
135
  Property(node) {
@@ -129,7 +142,7 @@ const noExcessProperties = createRule({
129
142
  if (!leftType) {
130
143
  return;
131
144
  }
132
- compareSymbols(leftType, rightType, node, context);
145
+ compareTypes(leftType, rightType, node, context);
133
146
  },
134
147
  ReturnStatement(node) {
135
148
  if (!node.argument) {
@@ -151,7 +164,7 @@ const noExcessProperties = createRule({
151
164
  }
152
165
  }
153
166
  const argType = services.getTypeAtLocation(node.argument);
154
- compareSymbols(returnType, argType, node.argument, context);
167
+ compareTypes(returnType, argType, node.argument, context);
155
168
  },
156
169
  VariableDeclarator(node) {
157
170
  if (!node.id.typeAnnotation || !node.init) {
@@ -159,7 +172,7 @@ const noExcessProperties = createRule({
159
172
  }
160
173
  const leftType = services.getTypeAtLocation(node.id.typeAnnotation.typeAnnotation);
161
174
  const rightType = services.getTypeAtLocation(node.init);
162
- compareSymbols(leftType, rightType, node.init, context);
175
+ compareTypes(leftType, rightType, node.init, context);
163
176
  },
164
177
  };
165
178
  },
@@ -1 +1 @@
1
- {"version":3,"file":"object-literal.js","sourceRoot":"","sources":["../src/object-literal.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA2E;A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,IAAI,CAAC,IAAI,EAAE,CAAC;oBAC1C,OAAO;gBACT,CAAC;gBAED,MAAM,QAAQ,GAAG,QAAQ,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,YAAY,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YACxD,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/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.6",
5
+ "version": "0.0.7",
6
6
  "homepage": "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties",
7
7
  "keywords": [
8
8
  "eslint-plugin",
@@ -202,6 +202,75 @@ ruleTester.run("object-literal", objectLiteral, {
202
202
  ],
203
203
  });
204
204
 
205
+ ruleTester.run("object-literal", objectLiteral, {
206
+ valid: [
207
+ `
208
+ function test(param1: { prop1: number }[] | (() => void)) {}
209
+ test([{ prop1: 1 }]);
210
+ `,
211
+ `
212
+ function test(param1: { prop1: number }[] | (() => { prop1: number })) {}
213
+ test(() => ({ prop1: 1 }));
214
+ `,
215
+ `
216
+ function test(param1: { prop1: number } | { prop1: number, prop2: number }) {}
217
+ test({ prop1: 1, prop2: 2 });
218
+ `,
219
+ `
220
+ function test(param1: { prop1: number } | { prop2: number }) {}
221
+ test({ prop2: 2 });
222
+ `,
223
+ ],
224
+ invalid: [
225
+ {
226
+ code: `
227
+ function test(param1: { prop1: number }[] | (() => void)) {}
228
+ test([{ prop1: 1, prop2: 2 }]);
229
+ `,
230
+ errors: [createError({ column: 14, endColumn: 38, line: 3 })],
231
+ },
232
+ {
233
+ code: `
234
+ function test(param1: { prop1: number }[] | (() => { prop1: number })) {}
235
+ test(() => ({ prop1: 1, prop2: 2 }));
236
+ `,
237
+ errors: [createError({ column: 14, endColumn: 44, line: 3 })],
238
+ },
239
+ {
240
+ code: `
241
+ function test(param1: { prop1: number } | { prop1: number, prop2: number }) {}
242
+ test({ prop1: 1, prop2: 2, prop3: 3 });
243
+ `,
244
+ errors: [createError({ column: 14, endColumn: 46, line: 3 })],
245
+ },
246
+ {
247
+ code: `
248
+ function test(param1: { prop1: number } | { prop2: number }) {}
249
+ test({ prop1: 1, prop2: 2 });
250
+ `,
251
+ errors: [createError({ column: 14, endColumn: 36, line: 3 })],
252
+ },
253
+ ],
254
+ });
255
+
256
+ ruleTester.run("object-literal", objectLiteral, {
257
+ valid: [
258
+ `
259
+ function test(param1: { prop1: number } & { prop2: number }) {}
260
+ test({ prop1: 1, prop2: 2 });
261
+ `,
262
+ ],
263
+ invalid: [
264
+ {
265
+ code: `
266
+ function test(param1: { prop1: number } & { prop2: number }) {}
267
+ test({ prop1: 1, prop2: 2, prop3: 3 });
268
+ `,
269
+ errors: [createError({ column: 14, endColumn: 46, line: 3 })],
270
+ },
271
+ ],
272
+ });
273
+
205
274
  ruleTester.run("object-literal", objectLiteral, {
206
275
  valid: [
207
276
  `
@@ -17,82 +17,92 @@ const createRule = ESLintUtils.RuleCreator<PluginDocs>(
17
17
  () => "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties",
18
18
  );
19
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
20
  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),
21
+ return (
22
+ (type as TypeOptionalSymbol).symbol !== undefined &&
23
+ tsutils.isSymbolFlagSet(type.symbol, ts.SymbolFlags.ObjectLiteral)
36
24
  );
37
25
  }
38
26
 
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
- }
27
+ function resolveType(type: ts.Type) {
28
+ let resolvedType = type;
48
29
 
49
- const excessPropertyNames = rightPropertyNames.filter(
50
- (n) => !leftPropertyNames.includes(n),
51
- );
30
+ const callSignatures = resolvedType.getCallSignatures();
31
+ if (callSignatures.length === 1) {
32
+ resolvedType = callSignatures[0].getReturnType();
33
+ }
52
34
 
53
- if (excessPropertyNames.length > 0) {
54
- context.report({
55
- data: { excessPropertyNames: excessPropertyNames.join(", ") },
56
- messageId: "noExcessProperties",
57
- node: rightNode,
58
- });
35
+ const arrayType = resolvedType.getNumberIndexType();
36
+ if (arrayType) {
37
+ resolvedType = arrayType;
59
38
  }
39
+
40
+ return resolvedType;
60
41
  }
61
42
 
62
- function compareSymbols(
43
+ function compareTypes(
63
44
  leftType: ts.Type,
64
45
  rightType: ts.Type,
65
46
  rightNode: TSESTree.Node,
66
47
  context: Readonly<RuleContext<"noExcessProperties", []>>,
67
48
  ): 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
- }
49
+ const allLeftTypes = tsutils.unionConstituents(leftType);
50
+ const allRightTypes = tsutils.unionConstituents(rightType);
83
51
 
84
52
  if (
85
- isObjectLiteral(rightType) &&
86
- tsutils
87
- .typeConstituents(leftType)
88
- .every((t) => !t.getStringIndexType() && !t.getNumberIndexType())
53
+ allLeftTypes.some(
54
+ (t) =>
55
+ t.getStringIndexType() !== undefined ||
56
+ (t.getNumberIndexType() !== undefined &&
57
+ (t as TypeOptionalSymbol).symbol?.name !== "Array"),
58
+ )
89
59
  ) {
90
- compareNames(
91
- getAllPropertyNames(leftType),
92
- getAllPropertyNames(rightType),
93
- rightNode,
94
- context,
95
- );
60
+ return;
61
+ }
62
+
63
+ for (const rightType of allRightTypes) {
64
+ const rightResolvedType = resolveType(rightType);
65
+
66
+ if (!isObjectLiteral(rightResolvedType)) {
67
+ continue;
68
+ }
69
+
70
+ const rightPropertyNames = rightResolvedType
71
+ .getProperties()
72
+ .map((p) => p.name);
73
+
74
+ let bestMatchExcessPropertyNames: string[] | null = null;
75
+ for (const leftType of allLeftTypes) {
76
+ const leftResolvedType = resolveType(leftType);
77
+ const leftPropertyNames = leftResolvedType
78
+ .getProperties()
79
+ .map((p) => p.name);
80
+
81
+ const excessPropertyNames = rightPropertyNames.filter(
82
+ (n) => !leftPropertyNames.includes(n),
83
+ );
84
+
85
+ if (
86
+ leftPropertyNames.length > 0 &&
87
+ (bestMatchExcessPropertyNames === null ||
88
+ excessPropertyNames.length < bestMatchExcessPropertyNames.length)
89
+ ) {
90
+ bestMatchExcessPropertyNames = excessPropertyNames;
91
+ }
92
+ }
93
+
94
+ if (
95
+ bestMatchExcessPropertyNames &&
96
+ bestMatchExcessPropertyNames.length > 0
97
+ ) {
98
+ context.report({
99
+ data: {
100
+ excessPropertyNames: bestMatchExcessPropertyNames.join(", "),
101
+ },
102
+ messageId: "noExcessProperties",
103
+ node: rightNode,
104
+ });
105
+ }
96
106
  }
97
107
  }
98
108
 
@@ -106,7 +116,7 @@ const noExcessProperties = createRule({
106
116
  const leftType = services.getTypeAtLocation(node.left);
107
117
  const rightType = services.getTypeAtLocation(node.right);
108
118
 
109
- compareSymbols(leftType, rightType, node.right, context);
119
+ compareTypes(leftType, rightType, node.right, context);
110
120
  },
111
121
  CallExpression(node) {
112
122
  if (node.arguments.length <= 0) {
@@ -142,7 +152,7 @@ const noExcessProperties = createRule({
142
152
  }
143
153
  }
144
154
 
145
- compareSymbols(paramType, argType, node.arguments[i], context);
155
+ compareTypes(paramType, argType, node.arguments[i], context);
146
156
  }
147
157
  },
148
158
  Property(node) {
@@ -159,7 +169,7 @@ const noExcessProperties = createRule({
159
169
  return;
160
170
  }
161
171
 
162
- compareSymbols(leftType, rightType, node, context);
172
+ compareTypes(leftType, rightType, node, context);
163
173
  },
164
174
  ReturnStatement(node) {
165
175
  if (!node.argument) {
@@ -190,7 +200,7 @@ const noExcessProperties = createRule({
190
200
 
191
201
  const argType = services.getTypeAtLocation(node.argument);
192
202
 
193
- compareSymbols(returnType, argType, node.argument, context);
203
+ compareTypes(returnType, argType, node.argument, context);
194
204
  },
195
205
  VariableDeclarator(node) {
196
206
  if (!node.id.typeAnnotation || !node.init) {
@@ -202,7 +212,7 @@ const noExcessProperties = createRule({
202
212
  );
203
213
  const rightType = services.getTypeAtLocation(node.init);
204
214
 
205
- compareSymbols(leftType, rightType, node.init, context);
215
+ compareTypes(leftType, rightType, node.init, context);
206
216
  },
207
217
  };
208
218
  },