eslint-plugin-no-excess-properties 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +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
@@ -0,0 +1,20 @@
1
+ declare const plugin: {
2
+ configs: {
3
+ readonly recommended: {
4
+ plugins: {
5
+ "no-excess-properties": /*elided*/ any;
6
+ };
7
+ rules: {
8
+ "no-excess-properties/object-literal": string;
9
+ };
10
+ };
11
+ };
12
+ meta: {
13
+ name: string;
14
+ version: string;
15
+ };
16
+ rules: {
17
+ "object-literal": import("@typescript-eslint/utils/ts-eslint").RuleModule<"noExcessProperties", [], import("./object-literal").PluginDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
18
+ };
19
+ };
20
+ export = plugin;
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const fs_1 = __importDefault(require("fs"));
6
+ const object_literal_1 = __importDefault(require("./object-literal"));
7
+ const pkg = JSON.parse(fs_1.default.readFileSync("./package.json", "utf8"));
8
+ const plugin = {
9
+ configs: {
10
+ get recommended() {
11
+ return {
12
+ plugins: {
13
+ "no-excess-properties": plugin,
14
+ },
15
+ rules: {
16
+ "no-excess-properties/object-literal": "warn",
17
+ },
18
+ };
19
+ },
20
+ },
21
+ meta: {
22
+ name: pkg.name,
23
+ version: pkg.version,
24
+ },
25
+ rules: {
26
+ "object-literal": object_literal_1.default,
27
+ },
28
+ };
29
+ module.exports = plugin;
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,4CAAoB;AACpB,sEAA6C;AAE7C,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAG/D,CAAC;AAEF,MAAM,MAAM,GAAG;IACb,OAAO,EAAE;QACP,IAAI,WAAW;YACb,OAAO;gBACL,OAAO,EAAE;oBACP,sBAAsB,EAAE,MAAM;iBAC/B;gBACD,KAAK,EAAE;oBACL,qCAAqC,EAAE,MAAM;iBAC9C;aACF,CAAC;QACJ,CAAC;KACF;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,OAAO,EAAE,GAAG,CAAC,OAAO;KACrB;IACD,KAAK,EAAE;QACL,gBAAgB,EAAE,wBAAa;KAChC;CACF,CAAC;AAEF,iBAAS,MAAM,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { ESLintUtils } from "@typescript-eslint/utils";
2
+ export interface PluginDocs {
3
+ description: string;
4
+ recommended?: boolean;
5
+ requiresTypeChecking?: boolean;
6
+ }
7
+ declare const noExcessProperties: ESLintUtils.RuleModule<"noExcessProperties", [], PluginDocs, ESLintUtils.RuleListener>;
8
+ export default noExcessProperties;
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const utils_1 = require("@typescript-eslint/utils");
40
+ const typescript_1 = __importDefault(require("typescript"));
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");
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
+ 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));
51
+ }
52
+ function compareNames(leftPropertyNames, rightPropertyNames, rightNode, context) {
53
+ if (leftPropertyNames.length <= 0) {
54
+ return;
55
+ }
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
+ });
63
+ }
64
+ }
65
+ function compareSymbols(leftType, rightType, rightNode, context) {
66
+ if (leftType.getStringIndexType() || leftType.getNumberIndexType()) {
67
+ return;
68
+ }
69
+ let leftPropertyNames = [];
70
+ let rightPropertyNames = [];
71
+ if (isObjectLiteral(rightType)) {
72
+ leftPropertyNames = getAllPropertyNames(leftType);
73
+ rightPropertyNames = getAllPropertyNames(rightType);
74
+ }
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
+ }
88
+ }
89
+ compareNames(leftPropertyNames, rightPropertyNames, rightNode, context);
90
+ }
91
+ const noExcessProperties = createRule({
92
+ create: function (context) {
93
+ const services = utils_1.ESLintUtils.getParserServices(context);
94
+ const typeChecker = services.program.getTypeChecker();
95
+ return {
96
+ AssignmentExpression(node) {
97
+ const leftType = services.getTypeAtLocation(node.left);
98
+ const rightType = services.getTypeAtLocation(node.right);
99
+ compareSymbols(leftType, rightType, node.right, context);
100
+ },
101
+ CallExpression(node) {
102
+ if (node.arguments.length <= 0) {
103
+ return;
104
+ }
105
+ const functionNode = services.esTreeNodeToTSNodeMap.get(node);
106
+ const functionSignature = typeChecker.getResolvedSignature(functionNode);
107
+ if (!functionSignature) {
108
+ return;
109
+ }
110
+ for (let i = 0; i < functionSignature.parameters.length; i++) {
111
+ if (i > node.arguments.length - 1) {
112
+ break;
113
+ }
114
+ const arg = services.getTypeAtLocation(node.arguments[i]);
115
+ const paramType = typeChecker.getTypeOfSymbolAtLocation(functionSignature.parameters[i], functionNode);
116
+ compareSymbols(paramType, arg, node.arguments[i], context);
117
+ }
118
+ },
119
+ VariableDeclarator(node) {
120
+ if (!node.id.typeAnnotation || !node.init) {
121
+ return;
122
+ }
123
+ const leftType = services.getTypeAtLocation(node.id.typeAnnotation.typeAnnotation);
124
+ const rightType = services.getTypeAtLocation(node.init);
125
+ compareSymbols(leftType, rightType, node.init, context);
126
+ },
127
+ };
128
+ },
129
+ defaultOptions: [],
130
+ meta: {
131
+ docs: {
132
+ description: "Excess properties are not allowed in assignments",
133
+ recommended: true,
134
+ requiresTypeChecking: true,
135
+ },
136
+ messages: {
137
+ noExcessProperties: "Excess properties '{{ excessPropertyNames }}' found",
138
+ },
139
+ type: "suggestion",
140
+ schema: [],
141
+ },
142
+ name: "no-excess-properties",
143
+ });
144
+ exports.default = noExcessProperties;
145
+ //# sourceMappingURL=no-excess-properties.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-excess-properties.js","sourceRoot":"","sources":["../src/no-excess-properties.ts"],"names":[],"mappings":";;;;;;;;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iB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC1D,MAAM,SAAS,GAAG,WAAW,CAAC,yBAAyB,CACrD,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC,EAC/B,YAAY,CACb,CAAC;oBAEF,cAAc,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;gBAC7D,CAAC;YACH,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,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,kDAAkD;YAC/D,WAAW,EAAE,IAAI;YACjB,oBAAoB,EAAE,IAAI;SAC3B;QACD,QAAQ,EAAE;YACR,kBAAkB,EAAE,qDAAqD;SAC1E;QACD,IAAI,EAAE,YAAY;QAClB,MAAM,EAAE,EAAE;KACX;IACD,IAAI,EAAE,sBAAsB;CAC7B,CAAC,CAAC;AAEH,kBAAe,kBAAkB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { ESLintUtils } from "@typescript-eslint/utils";
2
+ export interface PluginDocs {
3
+ description: string;
4
+ recommended?: boolean;
5
+ requiresTypeChecking?: boolean;
6
+ }
7
+ declare const noExcessProperties: ESLintUtils.RuleModule<"noExcessProperties", [], PluginDocs, ESLintUtils.RuleListener>;
8
+ export default noExcessProperties;
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const utils_1 = require("@typescript-eslint/utils");
40
+ const typescript_1 = __importDefault(require("typescript"));
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");
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
+ 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));
51
+ }
52
+ function compareNames(leftPropertyNames, rightPropertyNames, rightNode, context) {
53
+ if (leftPropertyNames.length <= 0) {
54
+ return;
55
+ }
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
+ });
63
+ }
64
+ }
65
+ function compareSymbols(leftType, rightType, rightNode, context) {
66
+ if (leftType.getStringIndexType() || leftType.getNumberIndexType()) {
67
+ return;
68
+ }
69
+ let leftPropertyNames = [];
70
+ let rightPropertyNames = [];
71
+ if (isObjectLiteral(rightType)) {
72
+ leftPropertyNames = getAllPropertyNames(leftType);
73
+ rightPropertyNames = getAllPropertyNames(rightType);
74
+ }
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
+ }
88
+ }
89
+ compareNames(leftPropertyNames, rightPropertyNames, rightNode, context);
90
+ }
91
+ const noExcessProperties = createRule({
92
+ create: function (context) {
93
+ const services = utils_1.ESLintUtils.getParserServices(context);
94
+ const typeChecker = services.program.getTypeChecker();
95
+ return {
96
+ AssignmentExpression(node) {
97
+ const leftType = services.getTypeAtLocation(node.left);
98
+ const rightType = services.getTypeAtLocation(node.right);
99
+ compareSymbols(leftType, rightType, node.right, context);
100
+ },
101
+ CallExpression(node) {
102
+ if (node.arguments.length <= 0) {
103
+ return;
104
+ }
105
+ const functionNode = services.esTreeNodeToTSNodeMap.get(node);
106
+ const functionSignature = typeChecker.getResolvedSignature(functionNode);
107
+ if (!functionSignature) {
108
+ return;
109
+ }
110
+ for (let i = 0; i < functionSignature.parameters.length; i++) {
111
+ if (i > node.arguments.length - 1) {
112
+ break;
113
+ }
114
+ const argType = services.getTypeAtLocation(node.arguments[i]);
115
+ const paramType = typeChecker.getTypeOfSymbolAtLocation(functionSignature.parameters[i], functionNode);
116
+ compareSymbols(paramType, argType, node.arguments[i], context);
117
+ }
118
+ },
119
+ Property(node) {
120
+ const leftNode = services.esTreeNodeToTSNodeMap.get(node);
121
+ if (leftNode.kind !== typescript_1.default.SyntaxKind.PropertyAssignment) {
122
+ return;
123
+ }
124
+ const leftType = typeChecker.getContextualType(leftNode.initializer);
125
+ const rightType = services.getTypeAtLocation(node);
126
+ if (!leftType) {
127
+ return;
128
+ }
129
+ compareSymbols(leftType, rightType, node, context);
130
+ },
131
+ ReturnStatement(node) {
132
+ if (!node.argument) {
133
+ return;
134
+ }
135
+ let functionNode = node.parent;
136
+ while (functionNode && !utils_1.ASTUtils.isFunction(functionNode)) {
137
+ functionNode = functionNode.parent;
138
+ }
139
+ if (!functionNode?.returnType) {
140
+ return;
141
+ }
142
+ let returnType = services.getTypeAtLocation(functionNode.returnType.typeAnnotation);
143
+ if (tsutils.isTypeReference(returnType)) {
144
+ const promiseTypes = typeChecker.getTypeArguments(returnType);
145
+ if (promiseTypes.length === 1) {
146
+ returnType = promiseTypes[0];
147
+ }
148
+ }
149
+ const argType = services.getTypeAtLocation(node.argument);
150
+ compareSymbols(returnType, argType, node.argument, context);
151
+ },
152
+ VariableDeclarator(node) {
153
+ if (!node.id.typeAnnotation || !node.init) {
154
+ return;
155
+ }
156
+ const leftType = services.getTypeAtLocation(node.id.typeAnnotation.typeAnnotation);
157
+ const rightType = services.getTypeAtLocation(node.init);
158
+ compareSymbols(leftType, rightType, node.init, context);
159
+ },
160
+ };
161
+ },
162
+ defaultOptions: [],
163
+ meta: {
164
+ docs: {
165
+ description: "Warn when excess properties are found on object literals",
166
+ recommended: true,
167
+ requiresTypeChecking: true,
168
+ },
169
+ messages: {
170
+ noExcessProperties: "Excess properties '{{ excessPropertyNames }}' found",
171
+ },
172
+ type: "suggestion",
173
+ schema: [],
174
+ },
175
+ name: "object-literal",
176
+ });
177
+ exports.default = noExcessProperties;
178
+ //# sourceMappingURL=object-literal.js.map
@@ -0,0 +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,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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AAQ,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,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,IAAI,EAAE,YAAY;QAClB,MAAM,EAAE,EAAE;KACX;IACD,IAAI,EAAE,gBAAgB;CACvB,CAAC,CAAC;AAEH,kBAAe,kBAAkB,CAAC"}
@@ -0,0 +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
+ });
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "eslint-plugin-no-excess-properties",
3
+ "description": "Excess properties are not allowed on object literals",
4
+ "license": "MIT",
5
+ "version": "0.0.1",
6
+ "homepage": "https://bitbucket.org/unimorphic/eslint-plugin-no-excess-properties",
7
+ "keywords": [
8
+ "eslint-plugin",
9
+ "eslint",
10
+ "eslintplugin",
11
+ "typescript"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://unimorphic@bitbucket.org/unimorphic/eslint-plugin-no-excess-properties.git"
16
+ },
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "main": "dist/index.js",
24
+ "scripts": {
25
+ "build": "tsc -p tsconfig.build.json",
26
+ "lint": "eslint .",
27
+ "prepare": "npm run build",
28
+ "test": "vitest"
29
+ },
30
+ "dependencies": {
31
+ "@typescript-eslint/utils": "8.37.0"
32
+ },
33
+ "peerDependencies": {
34
+ "@typescript-eslint/parser": "^8.37.0",
35
+ "eslint": ">=9.0.0",
36
+ "typescript": "^5.8.3"
37
+ },
38
+ "devDependencies": {
39
+ "@eslint/js": "9.31.0",
40
+ "@types/node": "24.0.14",
41
+ "@typescript-eslint/rule-tester": "8.37.0",
42
+ "eslint-plugin-eslint-plugin": "6.5.0",
43
+ "prettier": "3.6.2",
44
+ "typescript-eslint": "8.37.0",
45
+ "vitest": "3.2.4"
46
+ }
47
+ }
package/src/index.ts ADDED
@@ -0,0 +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;
@@ -0,0 +1,217 @@
1
+ import { RuleTester } 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
+ const ruleTester = new RuleTester({
12
+ languageOptions: {
13
+ parserOptions: {
14
+ projectService: {
15
+ allowDefaultProject: ["*.ts*"],
16
+ defaultProject: "tsconfig.json",
17
+ },
18
+ tsconfigRootDir: path.join(__dirname, ".."),
19
+ },
20
+ },
21
+ });
22
+
23
+ ruleTester.run("no-excess-properties", objectLiteral, {
24
+ valid: [
25
+ `
26
+ let test1: { prop1: number; } = { prop1: 1 };
27
+ const test2 = { prop1: 2 };
28
+ test1 = test2;
29
+ `,
30
+ `
31
+ let test1: () => { prop1: number; } = () => ({ prop1: 1 });
32
+ const test2 = () => ({ prop1: 2 });
33
+ test1 = test2;
34
+ `,
35
+ `
36
+ const test: () => { prop1: number; } = () => ({ prop1: 1 })
37
+ `,
38
+ `
39
+ function test(param1: { prop1: number } | null) {}
40
+ test(true ? { prop1: 1 } : null);
41
+ `,
42
+ `
43
+ function test(param1: () => { prop1: number }) {}
44
+ test(() => ({ prop1: 1 }));
45
+ `,
46
+ `
47
+ const test1: { prop2: number; } = { prop2: 1 };
48
+ const test2: { prop2: number } = { ...test1 };
49
+ `,
50
+ `
51
+ const test1 = { prop4: 2 };
52
+ const test2: { prop2: { prop3: { prop4: number; } } } = { prop2: { prop3: test1 } };
53
+ `,
54
+ `
55
+ const test1 = { prop1: 1 };
56
+ function test(): { prop1: number } { return test1 }
57
+ `,
58
+ `
59
+ const test1 = { prop1: 1 };
60
+ async function test(): Promise<{ prop1: number }> { return test1 }
61
+ `,
62
+ `
63
+ Object.keys({ prop1: 1 })
64
+ `,
65
+ `
66
+ const test2: Record<string, number> & { prop2: 1 } = { prop1: 1 };
67
+ `,
68
+ `
69
+ const test: { param1: number; }[] = [];
70
+ test.push({ param1: 1 })
71
+ `,
72
+ `
73
+ interface Test1 { prop1: number }
74
+ interface Test2 extends Test1 { prop2: number }
75
+ const test1: Test2 = { prop2: 1 }
76
+ const test2: Test1 = test1
77
+ `,
78
+ ],
79
+ invalid: [
80
+ {
81
+ code: `
82
+ let test1: { prop1: number; } = { prop1: 1 };
83
+ const test2 = { prop1: 2, prop2: 3 };
84
+ test1 = test2;
85
+ `,
86
+ errors: [
87
+ {
88
+ column: 17,
89
+ endColumn: 22,
90
+ endLine: 4,
91
+ line: 4,
92
+ messageId: "noExcessProperties",
93
+ },
94
+ ],
95
+ },
96
+ {
97
+ code: `
98
+ let test1: () => { prop1: number; } = () => ({ prop1: 1 });
99
+ const test2 = () => ({ prop1: 2, prop2: 3 });
100
+ test1 = test2;
101
+ `,
102
+ errors: [
103
+ {
104
+ column: 17,
105
+ endColumn: 22,
106
+ endLine: 4,
107
+ line: 4,
108
+ messageId: "noExcessProperties",
109
+ },
110
+ ],
111
+ },
112
+ {
113
+ code: `
114
+ const test: () => { prop1: number; } = () => ({ prop1: 1, prop2: 2 })
115
+ `,
116
+ errors: [
117
+ {
118
+ column: 48,
119
+ endColumn: 78,
120
+ endLine: 2,
121
+ line: 2,
122
+ messageId: "noExcessProperties",
123
+ },
124
+ ],
125
+ },
126
+ {
127
+ code: `
128
+ function test(param1: { prop1: number } | null) {}
129
+ test(true ? { prop1: 1, prop2: 2 } : null);
130
+ `,
131
+ errors: [
132
+ {
133
+ column: 14,
134
+ endColumn: 50,
135
+ endLine: 3,
136
+ line: 3,
137
+ messageId: "noExcessProperties",
138
+ },
139
+ ],
140
+ },
141
+ {
142
+ code: `
143
+ function test(param1: () => { prop1: number }) {}
144
+ test(() => ({ prop1: 1, prop2: 2 }));
145
+ `,
146
+ errors: [
147
+ {
148
+ column: 14,
149
+ endColumn: 44,
150
+ endLine: 3,
151
+ line: 3,
152
+ messageId: "noExcessProperties",
153
+ },
154
+ ],
155
+ },
156
+ {
157
+ code: `
158
+ const test1: { prop1: number; } = { prop1: 1 };
159
+ const test2: { prop2: number } = { ...test1, prop2: 2 };
160
+ `,
161
+ errors: [
162
+ {
163
+ column: 42,
164
+ endColumn: 64,
165
+ endLine: 3,
166
+ line: 3,
167
+ messageId: "noExcessProperties",
168
+ },
169
+ ],
170
+ },
171
+ {
172
+ code: `
173
+ const test1 = { prop1: 1, prop4: 2 };
174
+ const test2: { prop2: { prop3: { prop4: number; } } } = { prop2: { prop3: test1 } };
175
+ `,
176
+ errors: [
177
+ {
178
+ column: 76,
179
+ endColumn: 88,
180
+ endLine: 3,
181
+ line: 3,
182
+ messageId: "noExcessProperties",
183
+ },
184
+ ],
185
+ },
186
+ {
187
+ code: `
188
+ const test1 = { prop1: 1, prop2: 2 };
189
+ function test(): { prop1: number } { return test1 }
190
+ `,
191
+ errors: [
192
+ {
193
+ column: 53,
194
+ endColumn: 58,
195
+ endLine: 3,
196
+ line: 3,
197
+ messageId: "noExcessProperties",
198
+ },
199
+ ],
200
+ },
201
+ {
202
+ code: `
203
+ const test1 = { prop1: 1, prop2: 2 };
204
+ async function test(): Promise<{ prop1: number }> { return test1 }
205
+ `,
206
+ errors: [
207
+ {
208
+ column: 68,
209
+ endColumn: 73,
210
+ endLine: 3,
211
+ line: 3,
212
+ messageId: "noExcessProperties",
213
+ },
214
+ ],
215
+ },
216
+ ],
217
+ });
@@ -0,0 +1,215 @@
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 (tsutils.isTypeReference(returnType)) {
175
+ const promiseTypes = typeChecker.getTypeArguments(returnType);
176
+ if (promiseTypes.length === 1) {
177
+ returnType = promiseTypes[0];
178
+ }
179
+ }
180
+
181
+ const argType = services.getTypeAtLocation(node.argument);
182
+
183
+ compareSymbols(returnType, argType, node.argument, context);
184
+ },
185
+ VariableDeclarator(node) {
186
+ if (!node.id.typeAnnotation || !node.init) {
187
+ return;
188
+ }
189
+
190
+ const leftType = services.getTypeAtLocation(
191
+ node.id.typeAnnotation.typeAnnotation
192
+ );
193
+ const rightType = services.getTypeAtLocation(node.init);
194
+
195
+ compareSymbols(leftType, rightType, node.init, context);
196
+ },
197
+ };
198
+ },
199
+ defaultOptions: [],
200
+ meta: {
201
+ docs: {
202
+ description: "Warn when excess properties are found on object literals",
203
+ recommended: true,
204
+ requiresTypeChecking: true,
205
+ },
206
+ messages: {
207
+ noExcessProperties: "Excess properties '{{ excessPropertyNames }}' found",
208
+ },
209
+ type: "suggestion",
210
+ schema: [],
211
+ },
212
+ name: "object-literal",
213
+ });
214
+
215
+ export default noExcessProperties;
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist"
5
+ },
6
+ "exclude": ["src/**/*.test.ts"]
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "esModuleInterop": true,
5
+ "module": "NodeNext",
6
+ "resolveJsonModule": true,
7
+ "rootDir": "src",
8
+ "skipLibCheck": true,
9
+ "sourceMap": true,
10
+ "strict": true,
11
+ "target": "ES2022"
12
+ },
13
+ "include": ["./src"]
14
+ }