eslint-plugin-object-map 0.1.0
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 +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +11 -0
- package/dist/rules/prefer-object-spread-for-exact-object-map.d.ts +10 -0
- package/dist/rules/prefer-object-spread-for-exact-object-map.js +64 -0
- package/dist/rules/prefer-pick-for-object-subset-map.d.ts +10 -0
- package/dist/rules/prefer-pick-for-object-subset-map.js +72 -0
- package/dist/shared/exact-property-map.d.ts +11 -0
- package/dist/shared/exact-property-map.js +83 -0
- package/dist/shared/type-shape.d.ts +10 -0
- package/dist/shared/type-shape.js +40 -0
- package/package.json +89 -0
- package/src/index.ts +13 -0
- package/src/rules/prefer-object-spread-for-exact-object-map.ts +86 -0
- package/src/rules/prefer-pick-for-object-subset-map.ts +96 -0
- package/src/shared/exact-property-map.ts +128 -0
- package/src/shared/type-shape.ts +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zach Sents
|
|
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,75 @@
|
|
|
1
|
+
# eslint-plugin-object-map
|
|
2
|
+
|
|
3
|
+
Type-aware ESLint rules for object literals that repeatedly map same-name
|
|
4
|
+
properties from a source object.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
return {
|
|
8
|
+
createdAt: deployment.createdAt,
|
|
9
|
+
id: deployment.id,
|
|
10
|
+
projectId: deployment.projectId,
|
|
11
|
+
status: deployment.status,
|
|
12
|
+
workflows: syncedWorkflows,
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
If TypeScript proves those mapped keys are the complete known shape of
|
|
17
|
+
`deployment`, the plugin recommends a spread. If TypeScript proves `deployment`
|
|
18
|
+
has additional known keys, it recommends a `pick` utility such as Remeda's
|
|
19
|
+
`pick`.
|
|
20
|
+
|
|
21
|
+
## Rules
|
|
22
|
+
|
|
23
|
+
- `object-map/prefer-object-spread-for-exact-object-map`
|
|
24
|
+
|
|
25
|
+
- Reports repeated same-name property maps when the mapped keys exactly match
|
|
26
|
+
the source object's known properties.
|
|
27
|
+
- Recommendation: `{ ...deployment }`.
|
|
28
|
+
|
|
29
|
+
- `object-map/prefer-pick-for-object-subset-map`
|
|
30
|
+
- Reports repeated same-name property maps when the mapped keys are a subset
|
|
31
|
+
of the source object's known properties, or the source has an index
|
|
32
|
+
signature.
|
|
33
|
+
- Recommendation: `pick(deployment, ["createdAt", "id", "projectId", "status"])`.
|
|
34
|
+
|
|
35
|
+
Both rules intentionally skip renamed properties, computed properties,
|
|
36
|
+
already-spread objects, single-property mappings, unions, and unknown-like
|
|
37
|
+
source types.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// eslint.config.ts
|
|
43
|
+
import objectMap from "eslint-plugin-object-map"
|
|
44
|
+
import parser from "@typescript-eslint/parser"
|
|
45
|
+
|
|
46
|
+
export default [
|
|
47
|
+
{
|
|
48
|
+
files: ["**/*.ts"],
|
|
49
|
+
languageOptions: {
|
|
50
|
+
parser,
|
|
51
|
+
parserOptions: {
|
|
52
|
+
projectService: true,
|
|
53
|
+
tsconfigRootDir: import.meta.dirname,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
plugins: {
|
|
57
|
+
"object-map": objectMap,
|
|
58
|
+
},
|
|
59
|
+
rules: {
|
|
60
|
+
"object-map/prefer-object-spread-for-exact-object-map": "warn",
|
|
61
|
+
"object-map/prefer-pick-for-object-subset-map": "warn",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Development
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
bun install
|
|
71
|
+
bun run check
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The package uses Bun, Prettier, oxlint with type-aware linting, and zshy for
|
|
75
|
+
build output.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
meta: {
|
|
3
|
+
name: string;
|
|
4
|
+
};
|
|
5
|
+
rules: {
|
|
6
|
+
"prefer-object-spread-for-exact-object-map": import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferSpread", [({
|
|
7
|
+
minProperties?: number;
|
|
8
|
+
} | undefined)?], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
"prefer-pick-for-object-subset-map": import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferPick", [({
|
|
12
|
+
minProperties?: number;
|
|
13
|
+
} | undefined)?], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
14
|
+
name: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import preferObjectSpreadForExactObjectMap from "./rules/prefer-object-spread-for-exact-object-map.js";
|
|
2
|
+
import preferPickForObjectSubsetMap from "./rules/prefer-pick-for-object-subset-map.js";
|
|
3
|
+
export default {
|
|
4
|
+
meta: {
|
|
5
|
+
name: "object-map",
|
|
6
|
+
},
|
|
7
|
+
rules: {
|
|
8
|
+
"prefer-object-spread-for-exact-object-map": preferObjectSpreadForExactObjectMap,
|
|
9
|
+
"prefer-pick-for-object-subset-map": preferPickForObjectSubsetMap,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
type Options = [
|
|
3
|
+
{
|
|
4
|
+
minProperties?: number;
|
|
5
|
+
}?
|
|
6
|
+
];
|
|
7
|
+
declare const _default: ESLintUtils.RuleModule<"preferSpread", Options, unknown, ESLintUtils.RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
import { findSameNamedSourceMappings } from "../shared/exact-property-map.js";
|
|
3
|
+
import { getSourceShape } from "../shared/type-shape.js";
|
|
4
|
+
const createRule = ESLintUtils.RuleCreator((name) => `https://example.invalid/rules/${name}`);
|
|
5
|
+
function hasExactPropertySet(mappedKeys, sourceProperties) {
|
|
6
|
+
if (mappedKeys.size !== sourceProperties.size)
|
|
7
|
+
return false;
|
|
8
|
+
for (const propertyName of sourceProperties) {
|
|
9
|
+
if (!mappedKeys.has(propertyName))
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
export default createRule({
|
|
15
|
+
name: "prefer-object-spread-for-exact-object-map",
|
|
16
|
+
meta: {
|
|
17
|
+
type: "suggestion",
|
|
18
|
+
docs: {
|
|
19
|
+
description: "Prefer object spread when an object literal remaps all same-named properties from a source object",
|
|
20
|
+
},
|
|
21
|
+
schema: [
|
|
22
|
+
{
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
minProperties: {
|
|
26
|
+
type: "number",
|
|
27
|
+
minimum: 2,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
messages: {
|
|
34
|
+
preferSpread: "`{{source}}` is remapped by identical property names for all of its known properties. Prefer `{ ...{{source}} }`.",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
defaultOptions: [{ minProperties: 2 }],
|
|
38
|
+
create(context, [options]) {
|
|
39
|
+
const services = ESLintUtils.getParserServices(context);
|
|
40
|
+
const minProperties = options?.minProperties ?? 2;
|
|
41
|
+
return {
|
|
42
|
+
ObjectExpression(node) {
|
|
43
|
+
for (const group of findSameNamedSourceMappings(node, minProperties)) {
|
|
44
|
+
const shape = getSourceShape(services, group.sourceNode);
|
|
45
|
+
if (shape.kind !== "known" || shape.hasIndexSignature)
|
|
46
|
+
continue;
|
|
47
|
+
const mappedKeys = new Set(group.mappings.map((mapping) => mapping.keyName));
|
|
48
|
+
if (!hasExactPropertySet(mappedKeys, shape.propertyNames))
|
|
49
|
+
continue;
|
|
50
|
+
const firstMapping = group.mappings.at(0);
|
|
51
|
+
if (!firstMapping)
|
|
52
|
+
continue;
|
|
53
|
+
context.report({
|
|
54
|
+
node: firstMapping.node,
|
|
55
|
+
messageId: "preferSpread",
|
|
56
|
+
data: {
|
|
57
|
+
source: group.sourceName,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
type Options = [
|
|
3
|
+
{
|
|
4
|
+
minProperties?: number;
|
|
5
|
+
}?
|
|
6
|
+
];
|
|
7
|
+
declare const _default: ESLintUtils.RuleModule<"preferPick", Options, unknown, ESLintUtils.RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
import { findSameNamedSourceMappings } from "../shared/exact-property-map.js";
|
|
3
|
+
import { getSourceShape } from "../shared/type-shape.js";
|
|
4
|
+
const createRule = ESLintUtils.RuleCreator((name) => `https://example.invalid/rules/${name}`);
|
|
5
|
+
function isStrictSubset(mappedKeys, sourceProperties) {
|
|
6
|
+
if (mappedKeys.size >= sourceProperties.size)
|
|
7
|
+
return false;
|
|
8
|
+
for (const key of mappedKeys) {
|
|
9
|
+
if (!sourceProperties.has(key))
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
export default createRule({
|
|
15
|
+
name: "prefer-pick-for-object-subset-map",
|
|
16
|
+
meta: {
|
|
17
|
+
type: "suggestion",
|
|
18
|
+
docs: {
|
|
19
|
+
description: "Prefer a pick utility when an object literal remaps a same-named property subset from a source object",
|
|
20
|
+
},
|
|
21
|
+
schema: [
|
|
22
|
+
{
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
minProperties: {
|
|
26
|
+
type: "number",
|
|
27
|
+
minimum: 2,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
messages: {
|
|
34
|
+
preferPick: "`{{source}}` is remapped by {{count}} identical property names but has other known properties. Prefer `pick({{source}}, [{{keys}}])` or equivalent.",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
defaultOptions: [{ minProperties: 2 }],
|
|
38
|
+
create(context, [options]) {
|
|
39
|
+
const services = ESLintUtils.getParserServices(context);
|
|
40
|
+
const minProperties = options?.minProperties ?? 2;
|
|
41
|
+
return {
|
|
42
|
+
ObjectExpression(node) {
|
|
43
|
+
for (const group of findSameNamedSourceMappings(node, minProperties)) {
|
|
44
|
+
const shape = getSourceShape(services, group.sourceNode);
|
|
45
|
+
if (shape.kind !== "known")
|
|
46
|
+
continue;
|
|
47
|
+
const mappedKeys = new Set(group.mappings.map((mapping) => mapping.keyName));
|
|
48
|
+
const shouldPick = shape.hasIndexSignature ||
|
|
49
|
+
isStrictSubset(mappedKeys, shape.propertyNames);
|
|
50
|
+
if (!shouldPick)
|
|
51
|
+
continue;
|
|
52
|
+
const firstMapping = group.mappings.at(0);
|
|
53
|
+
if (!firstMapping)
|
|
54
|
+
continue;
|
|
55
|
+
const keys = Array.from(mappedKeys)
|
|
56
|
+
.toSorted()
|
|
57
|
+
.map((key) => `"${key}"`)
|
|
58
|
+
.join(", ");
|
|
59
|
+
context.report({
|
|
60
|
+
node: firstMapping.node,
|
|
61
|
+
messageId: "preferPick",
|
|
62
|
+
data: {
|
|
63
|
+
source: group.sourceName,
|
|
64
|
+
count: String(mappedKeys.size),
|
|
65
|
+
keys,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TSESTree } from "@typescript-eslint/utils";
|
|
2
|
+
export type ExactPropertyMapping = {
|
|
3
|
+
keyName: string;
|
|
4
|
+
node: TSESTree.Property;
|
|
5
|
+
};
|
|
6
|
+
export type SourceMappingGroup = {
|
|
7
|
+
sourceName: string;
|
|
8
|
+
sourceNode: TSESTree.Identifier;
|
|
9
|
+
mappings: ExactPropertyMapping[];
|
|
10
|
+
};
|
|
11
|
+
export declare function findSameNamedSourceMappings(objectExpression: TSESTree.ObjectExpression, minProperties: number): SourceMappingGroup[];
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
|
|
2
|
+
function getStaticPropertyName(key) {
|
|
3
|
+
if (key.type === AST_NODE_TYPES.Identifier) {
|
|
4
|
+
return key.name;
|
|
5
|
+
}
|
|
6
|
+
if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") {
|
|
7
|
+
return key.value;
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
function getIdentifierMemberName(member) {
|
|
12
|
+
if (member.computed || member.property.type !== AST_NODE_TYPES.Identifier) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return member.property.name;
|
|
16
|
+
}
|
|
17
|
+
function getSourceIdentifier(expression) {
|
|
18
|
+
if (expression.type === AST_NODE_TYPES.Identifier) {
|
|
19
|
+
return expression;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
function isSameNamedIdentifierMemberMapping(property) {
|
|
24
|
+
if (property.computed ||
|
|
25
|
+
property.kind !== "init" ||
|
|
26
|
+
property.method ||
|
|
27
|
+
property.value.type !== AST_NODE_TYPES.MemberExpression) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
const keyName = getStaticPropertyName(property.key);
|
|
31
|
+
const memberName = getIdentifierMemberName(property.value);
|
|
32
|
+
const source = getSourceIdentifier(property.value.object);
|
|
33
|
+
if (!keyName || !memberName || !source || keyName !== memberName) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return { source, keyName };
|
|
37
|
+
}
|
|
38
|
+
function getSpreadIdentifier(property) {
|
|
39
|
+
return property.argument.type === AST_NODE_TYPES.Identifier
|
|
40
|
+
? property.argument
|
|
41
|
+
: null;
|
|
42
|
+
}
|
|
43
|
+
export function findSameNamedSourceMappings(objectExpression, minProperties) {
|
|
44
|
+
const groups = new Map();
|
|
45
|
+
for (const property of objectExpression.properties) {
|
|
46
|
+
if (property.type === AST_NODE_TYPES.SpreadElement) {
|
|
47
|
+
const spreadSource = getSpreadIdentifier(property);
|
|
48
|
+
if (spreadSource) {
|
|
49
|
+
const group = groups.get(spreadSource.name);
|
|
50
|
+
if (group) {
|
|
51
|
+
group.hasExistingSpread = true;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
groups.set(spreadSource.name, {
|
|
55
|
+
sourceName: spreadSource.name,
|
|
56
|
+
sourceNode: spreadSource,
|
|
57
|
+
mappings: [],
|
|
58
|
+
hasExistingSpread: true,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const mapping = isSameNamedIdentifierMemberMapping(property);
|
|
65
|
+
if (!mapping)
|
|
66
|
+
continue;
|
|
67
|
+
const group = groups.get(mapping.source.name);
|
|
68
|
+
if (group) {
|
|
69
|
+
group.mappings.push({ keyName: mapping.keyName, node: property });
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
groups.set(mapping.source.name, {
|
|
73
|
+
sourceName: mapping.source.name,
|
|
74
|
+
sourceNode: mapping.source,
|
|
75
|
+
mappings: [{ keyName: mapping.keyName, node: property }],
|
|
76
|
+
hasExistingSpread: false,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return Array.from(groups.values()).filter((group) => !group.hasExistingSpread &&
|
|
81
|
+
new Set(group.mappings.map((mapping) => mapping.keyName)).size >=
|
|
82
|
+
minProperties);
|
|
83
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TSESTree } from "@typescript-eslint/utils";
|
|
2
|
+
import type { ParserServicesWithTypeInformation } from "@typescript-eslint/utils";
|
|
3
|
+
export type SourceShape = {
|
|
4
|
+
kind: "known";
|
|
5
|
+
propertyNames: Set<string>;
|
|
6
|
+
hasIndexSignature: boolean;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "unknown";
|
|
9
|
+
};
|
|
10
|
+
export declare function getSourceShape(services: ParserServicesWithTypeInformation, node: TSESTree.Node): SourceShape;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
function isUnknownLikeType(type) {
|
|
3
|
+
return Boolean(type.flags &
|
|
4
|
+
(ts.TypeFlags.Any |
|
|
5
|
+
ts.TypeFlags.Unknown |
|
|
6
|
+
ts.TypeFlags.Never |
|
|
7
|
+
ts.TypeFlags.TypeParameter));
|
|
8
|
+
}
|
|
9
|
+
function hasIndexSignature(type, checker) {
|
|
10
|
+
const apparent = checker.getApparentType(type);
|
|
11
|
+
return (checker.getIndexInfoOfType(apparent, ts.IndexKind.String) !== undefined ||
|
|
12
|
+
checker.getIndexInfoOfType(apparent, ts.IndexKind.Number) !== undefined);
|
|
13
|
+
}
|
|
14
|
+
function getStringPropertyName(symbol) {
|
|
15
|
+
const name = symbol.getName();
|
|
16
|
+
return name.startsWith("__@") ? null : name;
|
|
17
|
+
}
|
|
18
|
+
export function getSourceShape(services, node) {
|
|
19
|
+
const type = services.getTypeAtLocation(node);
|
|
20
|
+
const checker = services.program.getTypeChecker();
|
|
21
|
+
if (isUnknownLikeType(type) || type.isUnion()) {
|
|
22
|
+
return { kind: "unknown" };
|
|
23
|
+
}
|
|
24
|
+
const apparent = checker.getApparentType(type);
|
|
25
|
+
if (isUnknownLikeType(apparent) || apparent.getCallSignatures().length > 0) {
|
|
26
|
+
return { kind: "unknown" };
|
|
27
|
+
}
|
|
28
|
+
const propertyNames = new Set(checker
|
|
29
|
+
.getPropertiesOfType(apparent)
|
|
30
|
+
.map(getStringPropertyName)
|
|
31
|
+
.filter((name) => name !== null));
|
|
32
|
+
if (propertyNames.size === 0) {
|
|
33
|
+
return { kind: "unknown" };
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
kind: "known",
|
|
37
|
+
propertyNames,
|
|
38
|
+
hasIndexSignature: hasIndexSignature(apparent, checker),
|
|
39
|
+
};
|
|
40
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-object-map",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Type-aware ESLint rules for replacing repeated same-name object property maps with spread or pick.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "bun@1.3.12",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zachsents/eslint-plugin-object-map.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zachsents/eslint-plugin-object-map/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/zachsents/eslint-plugin-object-map#readme",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"eslint",
|
|
18
|
+
"eslint-plugin",
|
|
19
|
+
"typescript-eslint",
|
|
20
|
+
"type-aware",
|
|
21
|
+
"object-spread",
|
|
22
|
+
"pick"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"zshy": {
|
|
28
|
+
"exports": {
|
|
29
|
+
".": "./src/index.ts",
|
|
30
|
+
"./rules/*": "./src/rules/*"
|
|
31
|
+
},
|
|
32
|
+
"cjs": false,
|
|
33
|
+
"conditions": {
|
|
34
|
+
"bun": "src"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"bun": "./src/index.ts",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"default": "./dist/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./rules/*": {
|
|
44
|
+
"bun": "./src/rules/*",
|
|
45
|
+
"types": "./dist/rules/*",
|
|
46
|
+
"default": "./dist/rules/*"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"typecheck": "command -v tsgo >/dev/null && tsgo --noEmit || tsc --noEmit",
|
|
51
|
+
"lint": "oxlint --type-aware",
|
|
52
|
+
"lint:fix": "oxlint --type-aware --fix --fix-suggestions",
|
|
53
|
+
"format": "prettier . --write",
|
|
54
|
+
"format:check": "prettier . --check",
|
|
55
|
+
"test": "bun test",
|
|
56
|
+
"build": "zshy",
|
|
57
|
+
"check": "bun run format:check && bun run lint && bun run typecheck && bun run test && bun run build",
|
|
58
|
+
"prepublishOnly": "bun run check"
|
|
59
|
+
},
|
|
60
|
+
"files": [
|
|
61
|
+
"dist",
|
|
62
|
+
"src"
|
|
63
|
+
],
|
|
64
|
+
"main": "./dist/index.js",
|
|
65
|
+
"module": "./dist/index.js",
|
|
66
|
+
"types": "./dist/index.d.ts",
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=20.9"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"eslint": "^9.0.0",
|
|
72
|
+
"typescript": ">=5.0.0"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"@typescript-eslint/utils": "^8.62.0"
|
|
76
|
+
},
|
|
77
|
+
"devDependencies": {
|
|
78
|
+
"@types/bun": "latest",
|
|
79
|
+
"@types/node": "^24.10.1",
|
|
80
|
+
"@typescript-eslint/parser": "^8.62.0",
|
|
81
|
+
"eslint": "^9.39.4",
|
|
82
|
+
"oxlint": "^1.71.0",
|
|
83
|
+
"oxlint-tsgolint": "^0.23.0",
|
|
84
|
+
"prettier": "3.4.2",
|
|
85
|
+
"prettier-plugin-jsdoc": "^1.8.0",
|
|
86
|
+
"typescript": "^5.9.3",
|
|
87
|
+
"zshy": "^0.7.2"
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import preferObjectSpreadForExactObjectMap from "./rules/prefer-object-spread-for-exact-object-map"
|
|
2
|
+
import preferPickForObjectSubsetMap from "./rules/prefer-pick-for-object-subset-map"
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
meta: {
|
|
6
|
+
name: "object-map",
|
|
7
|
+
},
|
|
8
|
+
rules: {
|
|
9
|
+
"prefer-object-spread-for-exact-object-map":
|
|
10
|
+
preferObjectSpreadForExactObjectMap,
|
|
11
|
+
"prefer-pick-for-object-subset-map": preferPickForObjectSubsetMap,
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils"
|
|
2
|
+
import { findSameNamedSourceMappings } from "../shared/exact-property-map"
|
|
3
|
+
import { getSourceShape } from "../shared/type-shape"
|
|
4
|
+
|
|
5
|
+
type Options = [
|
|
6
|
+
{
|
|
7
|
+
minProperties?: number
|
|
8
|
+
}?,
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
type MessageIds = "preferSpread"
|
|
12
|
+
|
|
13
|
+
const createRule = ESLintUtils.RuleCreator(
|
|
14
|
+
(name) => `https://example.invalid/rules/${name}`,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
function hasExactPropertySet(
|
|
18
|
+
mappedKeys: Set<string>,
|
|
19
|
+
sourceProperties: Set<string>,
|
|
20
|
+
): boolean {
|
|
21
|
+
if (mappedKeys.size !== sourceProperties.size) return false
|
|
22
|
+
|
|
23
|
+
for (const propertyName of sourceProperties) {
|
|
24
|
+
if (!mappedKeys.has(propertyName)) return false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default createRule<Options, MessageIds>({
|
|
31
|
+
name: "prefer-object-spread-for-exact-object-map",
|
|
32
|
+
meta: {
|
|
33
|
+
type: "suggestion",
|
|
34
|
+
docs: {
|
|
35
|
+
description:
|
|
36
|
+
"Prefer object spread when an object literal remaps all same-named properties from a source object",
|
|
37
|
+
},
|
|
38
|
+
schema: [
|
|
39
|
+
{
|
|
40
|
+
type: "object",
|
|
41
|
+
properties: {
|
|
42
|
+
minProperties: {
|
|
43
|
+
type: "number",
|
|
44
|
+
minimum: 2,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
messages: {
|
|
51
|
+
preferSpread:
|
|
52
|
+
"`{{source}}` is remapped by identical property names for all of its known properties. Prefer `{ ...{{source}} }`.",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
defaultOptions: [{ minProperties: 2 }],
|
|
56
|
+
create(context, [options]) {
|
|
57
|
+
const services = ESLintUtils.getParserServices(context)
|
|
58
|
+
const minProperties = options?.minProperties ?? 2
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
ObjectExpression(node: TSESTree.ObjectExpression) {
|
|
62
|
+
for (const group of findSameNamedSourceMappings(node, minProperties)) {
|
|
63
|
+
const shape = getSourceShape(services, group.sourceNode)
|
|
64
|
+
if (shape.kind !== "known" || shape.hasIndexSignature) continue
|
|
65
|
+
|
|
66
|
+
const mappedKeys = new Set(
|
|
67
|
+
group.mappings.map((mapping) => mapping.keyName),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
if (!hasExactPropertySet(mappedKeys, shape.propertyNames)) continue
|
|
71
|
+
|
|
72
|
+
const firstMapping = group.mappings.at(0)
|
|
73
|
+
if (!firstMapping) continue
|
|
74
|
+
|
|
75
|
+
context.report({
|
|
76
|
+
node: firstMapping.node,
|
|
77
|
+
messageId: "preferSpread",
|
|
78
|
+
data: {
|
|
79
|
+
source: group.sourceName,
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils"
|
|
2
|
+
import { findSameNamedSourceMappings } from "../shared/exact-property-map"
|
|
3
|
+
import { getSourceShape } from "../shared/type-shape"
|
|
4
|
+
|
|
5
|
+
type Options = [
|
|
6
|
+
{
|
|
7
|
+
minProperties?: number
|
|
8
|
+
}?,
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
type MessageIds = "preferPick"
|
|
12
|
+
|
|
13
|
+
const createRule = ESLintUtils.RuleCreator(
|
|
14
|
+
(name) => `https://example.invalid/rules/${name}`,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
function isStrictSubset(
|
|
18
|
+
mappedKeys: Set<string>,
|
|
19
|
+
sourceProperties: Set<string>,
|
|
20
|
+
): boolean {
|
|
21
|
+
if (mappedKeys.size >= sourceProperties.size) return false
|
|
22
|
+
|
|
23
|
+
for (const key of mappedKeys) {
|
|
24
|
+
if (!sourceProperties.has(key)) return false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default createRule<Options, MessageIds>({
|
|
31
|
+
name: "prefer-pick-for-object-subset-map",
|
|
32
|
+
meta: {
|
|
33
|
+
type: "suggestion",
|
|
34
|
+
docs: {
|
|
35
|
+
description:
|
|
36
|
+
"Prefer a pick utility when an object literal remaps a same-named property subset from a source object",
|
|
37
|
+
},
|
|
38
|
+
schema: [
|
|
39
|
+
{
|
|
40
|
+
type: "object",
|
|
41
|
+
properties: {
|
|
42
|
+
minProperties: {
|
|
43
|
+
type: "number",
|
|
44
|
+
minimum: 2,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
messages: {
|
|
51
|
+
preferPick:
|
|
52
|
+
"`{{source}}` is remapped by {{count}} identical property names but has other known properties. Prefer `pick({{source}}, [{{keys}}])` or equivalent.",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
defaultOptions: [{ minProperties: 2 }],
|
|
56
|
+
create(context, [options]) {
|
|
57
|
+
const services = ESLintUtils.getParserServices(context)
|
|
58
|
+
const minProperties = options?.minProperties ?? 2
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
ObjectExpression(node: TSESTree.ObjectExpression) {
|
|
62
|
+
for (const group of findSameNamedSourceMappings(node, minProperties)) {
|
|
63
|
+
const shape = getSourceShape(services, group.sourceNode)
|
|
64
|
+
if (shape.kind !== "known") continue
|
|
65
|
+
|
|
66
|
+
const mappedKeys = new Set(
|
|
67
|
+
group.mappings.map((mapping) => mapping.keyName),
|
|
68
|
+
)
|
|
69
|
+
const shouldPick =
|
|
70
|
+
shape.hasIndexSignature ||
|
|
71
|
+
isStrictSubset(mappedKeys, shape.propertyNames)
|
|
72
|
+
|
|
73
|
+
if (!shouldPick) continue
|
|
74
|
+
|
|
75
|
+
const firstMapping = group.mappings.at(0)
|
|
76
|
+
if (!firstMapping) continue
|
|
77
|
+
|
|
78
|
+
const keys = Array.from(mappedKeys)
|
|
79
|
+
.toSorted()
|
|
80
|
+
.map((key) => `"${key}"`)
|
|
81
|
+
.join(", ")
|
|
82
|
+
|
|
83
|
+
context.report({
|
|
84
|
+
node: firstMapping.node,
|
|
85
|
+
messageId: "preferPick",
|
|
86
|
+
data: {
|
|
87
|
+
source: group.sourceName,
|
|
88
|
+
count: String(mappedKeys.size),
|
|
89
|
+
keys,
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
})
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
|
|
2
|
+
|
|
3
|
+
export type ExactPropertyMapping = {
|
|
4
|
+
keyName: string
|
|
5
|
+
node: TSESTree.Property
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type SourceMappingGroup = {
|
|
9
|
+
sourceName: string
|
|
10
|
+
sourceNode: TSESTree.Identifier
|
|
11
|
+
mappings: ExactPropertyMapping[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type GroupState = SourceMappingGroup & {
|
|
15
|
+
hasExistingSpread: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getStaticPropertyName(key: TSESTree.Property["key"]): string | null {
|
|
19
|
+
if (key.type === AST_NODE_TYPES.Identifier) {
|
|
20
|
+
return key.name
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") {
|
|
24
|
+
return key.value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getIdentifierMemberName(
|
|
31
|
+
member: TSESTree.MemberExpression,
|
|
32
|
+
): string | null {
|
|
33
|
+
if (member.computed || member.property.type !== AST_NODE_TYPES.Identifier) {
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return member.property.name
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getSourceIdentifier(
|
|
41
|
+
expression: TSESTree.Expression,
|
|
42
|
+
): TSESTree.Identifier | null {
|
|
43
|
+
if (expression.type === AST_NODE_TYPES.Identifier) {
|
|
44
|
+
return expression
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isSameNamedIdentifierMemberMapping(
|
|
51
|
+
property: TSESTree.Property,
|
|
52
|
+
): { source: TSESTree.Identifier; keyName: string } | null {
|
|
53
|
+
if (
|
|
54
|
+
property.computed ||
|
|
55
|
+
property.kind !== "init" ||
|
|
56
|
+
property.method ||
|
|
57
|
+
property.value.type !== AST_NODE_TYPES.MemberExpression
|
|
58
|
+
) {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const keyName = getStaticPropertyName(property.key)
|
|
63
|
+
const memberName = getIdentifierMemberName(property.value)
|
|
64
|
+
const source = getSourceIdentifier(property.value.object)
|
|
65
|
+
|
|
66
|
+
if (!keyName || !memberName || !source || keyName !== memberName) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return { source, keyName }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getSpreadIdentifier(
|
|
74
|
+
property: TSESTree.SpreadElement,
|
|
75
|
+
): TSESTree.Identifier | null {
|
|
76
|
+
return property.argument.type === AST_NODE_TYPES.Identifier
|
|
77
|
+
? property.argument
|
|
78
|
+
: null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function findSameNamedSourceMappings(
|
|
82
|
+
objectExpression: TSESTree.ObjectExpression,
|
|
83
|
+
minProperties: number,
|
|
84
|
+
): SourceMappingGroup[] {
|
|
85
|
+
const groups = new Map<string, GroupState>()
|
|
86
|
+
|
|
87
|
+
for (const property of objectExpression.properties) {
|
|
88
|
+
if (property.type === AST_NODE_TYPES.SpreadElement) {
|
|
89
|
+
const spreadSource = getSpreadIdentifier(property)
|
|
90
|
+
if (spreadSource) {
|
|
91
|
+
const group = groups.get(spreadSource.name)
|
|
92
|
+
if (group) {
|
|
93
|
+
group.hasExistingSpread = true
|
|
94
|
+
} else {
|
|
95
|
+
groups.set(spreadSource.name, {
|
|
96
|
+
sourceName: spreadSource.name,
|
|
97
|
+
sourceNode: spreadSource,
|
|
98
|
+
mappings: [],
|
|
99
|
+
hasExistingSpread: true,
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const mapping = isSameNamedIdentifierMemberMapping(property)
|
|
107
|
+
if (!mapping) continue
|
|
108
|
+
|
|
109
|
+
const group = groups.get(mapping.source.name)
|
|
110
|
+
if (group) {
|
|
111
|
+
group.mappings.push({ keyName: mapping.keyName, node: property })
|
|
112
|
+
} else {
|
|
113
|
+
groups.set(mapping.source.name, {
|
|
114
|
+
sourceName: mapping.source.name,
|
|
115
|
+
sourceNode: mapping.source,
|
|
116
|
+
mappings: [{ keyName: mapping.keyName, node: property }],
|
|
117
|
+
hasExistingSpread: false,
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return Array.from(groups.values()).filter(
|
|
123
|
+
(group) =>
|
|
124
|
+
!group.hasExistingSpread &&
|
|
125
|
+
new Set(group.mappings.map((mapping) => mapping.keyName)).size >=
|
|
126
|
+
minProperties,
|
|
127
|
+
)
|
|
128
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { TSESTree } from "@typescript-eslint/utils"
|
|
2
|
+
import type { ParserServicesWithTypeInformation } from "@typescript-eslint/utils"
|
|
3
|
+
import ts from "typescript"
|
|
4
|
+
|
|
5
|
+
export type SourceShape =
|
|
6
|
+
| {
|
|
7
|
+
kind: "known"
|
|
8
|
+
propertyNames: Set<string>
|
|
9
|
+
hasIndexSignature: boolean
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
kind: "unknown"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isUnknownLikeType(type: ts.Type): boolean {
|
|
16
|
+
return Boolean(
|
|
17
|
+
type.flags &
|
|
18
|
+
(ts.TypeFlags.Any |
|
|
19
|
+
ts.TypeFlags.Unknown |
|
|
20
|
+
ts.TypeFlags.Never |
|
|
21
|
+
ts.TypeFlags.TypeParameter),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hasIndexSignature(type: ts.Type, checker: ts.TypeChecker): boolean {
|
|
26
|
+
const apparent = checker.getApparentType(type)
|
|
27
|
+
return (
|
|
28
|
+
checker.getIndexInfoOfType(apparent, ts.IndexKind.String) !== undefined ||
|
|
29
|
+
checker.getIndexInfoOfType(apparent, ts.IndexKind.Number) !== undefined
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getStringPropertyName(symbol: ts.Symbol): string | null {
|
|
34
|
+
const name = symbol.getName()
|
|
35
|
+
return name.startsWith("__@") ? null : name
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getSourceShape(
|
|
39
|
+
services: ParserServicesWithTypeInformation,
|
|
40
|
+
node: TSESTree.Node,
|
|
41
|
+
): SourceShape {
|
|
42
|
+
const type = services.getTypeAtLocation(node)
|
|
43
|
+
const checker = services.program.getTypeChecker()
|
|
44
|
+
|
|
45
|
+
if (isUnknownLikeType(type) || type.isUnion()) {
|
|
46
|
+
return { kind: "unknown" }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const apparent = checker.getApparentType(type)
|
|
50
|
+
|
|
51
|
+
if (isUnknownLikeType(apparent) || apparent.getCallSignatures().length > 0) {
|
|
52
|
+
return { kind: "unknown" }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const propertyNames = new Set(
|
|
56
|
+
checker
|
|
57
|
+
.getPropertiesOfType(apparent)
|
|
58
|
+
.map(getStringPropertyName)
|
|
59
|
+
.filter((name): name is string => name !== null),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
if (propertyNames.size === 0) {
|
|
63
|
+
return { kind: "unknown" }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
kind: "known",
|
|
68
|
+
propertyNames,
|
|
69
|
+
hasIndexSignature: hasIndexSignature(apparent, checker),
|
|
70
|
+
}
|
|
71
|
+
}
|