eslint-plugin-no-excess-properties 0.0.6 → 0.0.8
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/dist/object-literal.js +56 -42
- package/dist/object-literal.js.map +1 -1
- package/package.json +1 -1
- package/src/object-literal.test.ts +72 -0
- package/src/object-literal.ts +77 -67
package/dist/object-literal.js
CHANGED
|
@@ -40,48 +40,62 @@ 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
|
-
|
|
49
|
-
|
|
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
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
messageId: "noExcessProperties",
|
|
61
|
-
node: rightNode,
|
|
62
|
-
});
|
|
53
|
+
const arrayType = resolvedType.getNumberIndexType();
|
|
54
|
+
if (arrayType &&
|
|
55
|
+
resolvedType.symbol?.name === "Array") {
|
|
56
|
+
resolvedType = arrayType;
|
|
63
57
|
}
|
|
58
|
+
return resolvedType;
|
|
64
59
|
}
|
|
65
|
-
function
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
leftType
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
.
|
|
83
|
-
|
|
84
|
-
|
|
60
|
+
function compareTypes(leftType, rightType, rightNode, context) {
|
|
61
|
+
const allLeftTypes = tsutils.unionConstituents(leftType);
|
|
62
|
+
const allRightTypes = tsutils.unionConstituents(rightType);
|
|
63
|
+
for (const rightType of allRightTypes) {
|
|
64
|
+
const rightResolvedType = resolveType(rightType);
|
|
65
|
+
if (!isObjectLiteral(rightResolvedType)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const rightPropertyNames = rightResolvedType
|
|
69
|
+
.getProperties()
|
|
70
|
+
.map((p) => p.name);
|
|
71
|
+
let bestMatchExcessPropertyNames = null;
|
|
72
|
+
for (const leftType of allLeftTypes) {
|
|
73
|
+
const leftResolvedType = resolveType(leftType);
|
|
74
|
+
const leftPropertyNames = leftResolvedType
|
|
75
|
+
.getProperties()
|
|
76
|
+
.map((p) => p.name);
|
|
77
|
+
if (leftResolvedType.getStringIndexType() !== undefined ||
|
|
78
|
+
leftResolvedType.getNumberIndexType() !== undefined) {
|
|
79
|
+
bestMatchExcessPropertyNames = null;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
const excessPropertyNames = rightPropertyNames.filter((n) => !leftPropertyNames.includes(n));
|
|
83
|
+
if (leftPropertyNames.length > 0 &&
|
|
84
|
+
(bestMatchExcessPropertyNames === null ||
|
|
85
|
+
excessPropertyNames.length < bestMatchExcessPropertyNames.length)) {
|
|
86
|
+
bestMatchExcessPropertyNames = excessPropertyNames;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (bestMatchExcessPropertyNames &&
|
|
90
|
+
bestMatchExcessPropertyNames.length > 0) {
|
|
91
|
+
context.report({
|
|
92
|
+
data: {
|
|
93
|
+
excessPropertyNames: bestMatchExcessPropertyNames.join(", "),
|
|
94
|
+
},
|
|
95
|
+
messageId: "noExcessProperties",
|
|
96
|
+
node: rightNode,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
const noExcessProperties = createRule({
|
|
@@ -92,7 +106,7 @@ const noExcessProperties = createRule({
|
|
|
92
106
|
AssignmentExpression(node) {
|
|
93
107
|
const leftType = services.getTypeAtLocation(node.left);
|
|
94
108
|
const rightType = services.getTypeAtLocation(node.right);
|
|
95
|
-
|
|
109
|
+
compareTypes(leftType, rightType, node.right, context);
|
|
96
110
|
},
|
|
97
111
|
CallExpression(node) {
|
|
98
112
|
if (node.arguments.length <= 0) {
|
|
@@ -116,7 +130,7 @@ const noExcessProperties = createRule({
|
|
|
116
130
|
paramType = arrayType;
|
|
117
131
|
}
|
|
118
132
|
}
|
|
119
|
-
|
|
133
|
+
compareTypes(paramType, argType, node.arguments[i], context);
|
|
120
134
|
}
|
|
121
135
|
},
|
|
122
136
|
Property(node) {
|
|
@@ -129,7 +143,7 @@ const noExcessProperties = createRule({
|
|
|
129
143
|
if (!leftType) {
|
|
130
144
|
return;
|
|
131
145
|
}
|
|
132
|
-
|
|
146
|
+
compareTypes(leftType, rightType, node, context);
|
|
133
147
|
},
|
|
134
148
|
ReturnStatement(node) {
|
|
135
149
|
if (!node.argument) {
|
|
@@ -151,7 +165,7 @@ const noExcessProperties = createRule({
|
|
|
151
165
|
}
|
|
152
166
|
}
|
|
153
167
|
const argType = services.getTypeAtLocation(node.argument);
|
|
154
|
-
|
|
168
|
+
compareTypes(returnType, argType, node.argument, context);
|
|
155
169
|
},
|
|
156
170
|
VariableDeclarator(node) {
|
|
157
171
|
if (!node.id.typeAnnotation || !node.init) {
|
|
@@ -159,7 +173,7 @@ const noExcessProperties = createRule({
|
|
|
159
173
|
}
|
|
160
174
|
const leftType = services.getTypeAtLocation(node.id.typeAnnotation.typeAnnotation);
|
|
161
175
|
const rightType = services.getTypeAtLocation(node.init);
|
|
162
|
-
|
|
176
|
+
compareTypes(leftType, rightType, node.init, context);
|
|
163
177
|
},
|
|
164
178
|
};
|
|
165
179
|
},
|
|
@@ -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,CAAC,qEAAqE,CAC5E,CAAC;AAEF,SAAS,
|
|
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,eAAe,CAAC,IAAa;IACpC,OAAO,CACJ,IAA2B,CAAC,MAAM,KAAK,SAAS;QACjD,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,oBAAE,CAAC,WAAW,CAAC,aAAa,CAAC,CACnE,CAAC;AACJ,CAAC;AAED,SAAS,WAAW,CAAC,IAAa;IAChC,IAAI,YAAY,GAAG,IAAI,CAAC;IAExB,MAAM,cAAc,GAAG,YAAY,CAAC,iBAAiB,EAAE,CAAC;IACxD,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChC,YAAY,GAAG,cAA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oBAC/B,CAAC;gBACH,CAAC;gBAED,MAAM,OAAO,GAAG,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBAE1D,YAAY,CAAC,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAC5D,CAAC;YACD,kBAAkB,CAAC,IAAI;gBACrB,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,IAAI,CAAC,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.
|
|
5
|
+
"version": "0.0.8",
|
|
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
|
`
|
|
@@ -222,6 +291,9 @@ ruleTester.run("object-literal", objectLiteral, {
|
|
|
222
291
|
`
|
|
223
292
|
Object.keys({ prop1: 1 })
|
|
224
293
|
`,
|
|
294
|
+
`
|
|
295
|
+
const test: { prop1: number, [property: string]: number; }[] = [{ prop2: 1 }];
|
|
296
|
+
`,
|
|
225
297
|
],
|
|
226
298
|
invalid: [],
|
|
227
299
|
});
|
package/src/object-literal.ts
CHANGED
|
@@ -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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
40
|
-
|
|
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
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
const callSignatures = resolvedType.getCallSignatures();
|
|
31
|
+
if (callSignatures.length === 1) {
|
|
32
|
+
resolvedType = callSignatures[0].getReturnType();
|
|
33
|
+
}
|
|
52
34
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
35
|
+
const arrayType = resolvedType.getNumberIndexType();
|
|
36
|
+
if (
|
|
37
|
+
arrayType &&
|
|
38
|
+
(resolvedType as TypeOptionalSymbol).symbol?.name === "Array"
|
|
39
|
+
) {
|
|
40
|
+
resolvedType = arrayType;
|
|
59
41
|
}
|
|
42
|
+
|
|
43
|
+
return resolvedType;
|
|
60
44
|
}
|
|
61
45
|
|
|
62
|
-
function
|
|
46
|
+
function compareTypes(
|
|
63
47
|
leftType: ts.Type,
|
|
64
48
|
rightType: ts.Type,
|
|
65
49
|
rightNode: TSESTree.Node,
|
|
66
50
|
context: Readonly<RuleContext<"noExcessProperties", []>>,
|
|
67
51
|
): void {
|
|
68
|
-
const
|
|
69
|
-
|
|
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
|
-
|
|
52
|
+
const allLeftTypes = tsutils.unionConstituents(leftType);
|
|
53
|
+
const allRightTypes = tsutils.unionConstituents(rightType);
|
|
54
|
+
|
|
55
|
+
for (const rightType of allRightTypes) {
|
|
56
|
+
const rightResolvedType = resolveType(rightType);
|
|
57
|
+
|
|
58
|
+
if (!isObjectLiteral(rightResolvedType)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const rightPropertyNames = rightResolvedType
|
|
63
|
+
.getProperties()
|
|
64
|
+
.map((p) => p.name);
|
|
65
|
+
|
|
66
|
+
let bestMatchExcessPropertyNames: string[] | null = null;
|
|
67
|
+
for (const leftType of allLeftTypes) {
|
|
68
|
+
const leftResolvedType = resolveType(leftType);
|
|
69
|
+
const leftPropertyNames = leftResolvedType
|
|
70
|
+
.getProperties()
|
|
71
|
+
.map((p) => p.name);
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
leftResolvedType.getStringIndexType() !== undefined ||
|
|
75
|
+
leftResolvedType.getNumberIndexType() !== undefined
|
|
76
|
+
) {
|
|
77
|
+
bestMatchExcessPropertyNames = null;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
+
compareTypes(leftType, rightType, node.init, context);
|
|
206
216
|
},
|
|
207
217
|
};
|
|
208
218
|
},
|