@tcy/eslint-rules 1.8.0 → 1.9.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/coverage/clover.xml +50 -4
- package/coverage/coverage-final.json +1 -0
- package/coverage/lcov-report/index.html +19 -19
- package/coverage/lcov-report/src/array-elements-per-line.js.html +1 -1
- package/coverage/lcov-report/src/back/back.js.html +1 -1
- package/coverage/lcov-report/src/back/http.js.html +1 -1
- package/coverage/lcov-report/src/back/index.html +1 -1
- package/coverage/lcov-report/src/back/recommended.js.html +1 -1
- package/coverage/lcov-report/src/bin/eslint-cache-guard.js.html +1 -1
- package/coverage/lcov-report/src/bin/index.html +1 -1
- package/coverage/lcov-report/src/client/client.js.html +5 -2
- package/coverage/lcov-report/src/client/index.html +1 -1
- package/coverage/lcov-report/src/client/recommended.js.html +1 -1
- package/coverage/lcov-report/src/decorator-type-enforcement.js.html +1 -1
- package/coverage/lcov-report/src/enforce-tailwind-important.js.html +1 -1
- package/coverage/lcov-report/src/export-specifiers-per-line.js.html +1 -1
- package/coverage/lcov-report/src/import-specifiers-per-line.js.html +1 -1
- package/coverage/lcov-report/src/index.html +24 -9
- package/coverage/lcov-report/src/index.js.html +1 -1
- package/coverage/lcov-report/src/jsx-boolean-shorthand.js.html +1 -1
- package/coverage/lcov-report/src/jsx-expression-string-literals.js.html +1 -1
- package/coverage/lcov-report/src/jsx-ref-string-exists.js.html +1 -1
- package/coverage/lcov-report/src/no-static-key-in-map.js.html +631 -0
- package/coverage/lcov-report/src/plugin.js.html +8 -2
- package/coverage/lcov-report/src/recommended.js.html +1 -1
- package/coverage/lcov-report/src/require-fragment-key.js.html +1 -1
- package/coverage/lcov-report/src/route-dto-required.js.html +1 -1
- package/coverage/lcov-report/src/v-model-name-match.js.html +1 -1
- package/coverage/lcov-report/src/vuejs-define-component.js.html +1 -1
- package/coverage/lcov-report/src/vuejs-define-workspace.js.html +1 -1
- package/coverage/lcov.info +115 -0
- package/decl/back/back.d.ts +16 -0
- package/decl/client/client.d.ts +17 -0
- package/decl/no-static-key-in-map.d.ts +66 -0
- package/decl/plugin.d.ts +16 -0
- package/package.json +1 -1
- package/src/client/client.js +1 -0
- package/src/no-static-key-in-map.js +182 -0
- package/src/plugin.js +2 -0
- package/tests/no-static-key-in-map.spec.js +186 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import {
|
|
3
|
+
analyzeElement,
|
|
4
|
+
getStaticKeyAttr,
|
|
5
|
+
isListItemRoot,
|
|
6
|
+
isStaticExpression,
|
|
7
|
+
} from '../src/no-static-key-in-map';
|
|
8
|
+
|
|
9
|
+
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
const linkParents = (node, parent = null) => {
|
|
12
|
+
if (!node || typeof node.type !== 'string') return;
|
|
13
|
+
node.parent = parent;
|
|
14
|
+
Object.keys(node).forEach((key) => {
|
|
15
|
+
if (key === 'parent') return;
|
|
16
|
+
const value = node[key];
|
|
17
|
+
if (Array.isArray(value)) {
|
|
18
|
+
value.forEach((child) => linkParents(child, node));
|
|
19
|
+
} else if (value && typeof value.type === 'string') {
|
|
20
|
+
linkParents(value, node);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const collectElements = (node, out = []) => {
|
|
26
|
+
if (!node || typeof node.type !== 'string') return out;
|
|
27
|
+
if (node.type === 'JSXElement') out.push(node);
|
|
28
|
+
Object.keys(node).forEach((key) => {
|
|
29
|
+
if (key === 'parent') return;
|
|
30
|
+
const value = node[key];
|
|
31
|
+
if (Array.isArray(value)) value.forEach((child) => collectElements(child, out));
|
|
32
|
+
else if (value && typeof value.type === 'string') collectElements(value, out);
|
|
33
|
+
});
|
|
34
|
+
return out;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Parse `code`, return every messageId the rule would emit (in source order). */
|
|
38
|
+
const lint = (code) => {
|
|
39
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
40
|
+
linkParents(ast.program);
|
|
41
|
+
return collectElements(ast.program)
|
|
42
|
+
.map((node) => analyzeElement(node))
|
|
43
|
+
.filter(Boolean);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const firstElement = (code) => {
|
|
47
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
48
|
+
linkParents(ast.program);
|
|
49
|
+
return collectElements(ast.program)[0];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// ─── isStaticExpression ─────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
describe('GIVEN "no-static-key-in-map" / isStaticExpression', () => {
|
|
55
|
+
const expr = (code) => parse(`(${code})`, { plugins: ['typescript'] }).program.body[0].expression;
|
|
56
|
+
|
|
57
|
+
it.each([
|
|
58
|
+
['"row"', true],
|
|
59
|
+
['42', true],
|
|
60
|
+
['true', true],
|
|
61
|
+
['`row`', true],
|
|
62
|
+
['`row-${i}`', false],
|
|
63
|
+
['i', false],
|
|
64
|
+
['item.id', false],
|
|
65
|
+
['getKey()', false],
|
|
66
|
+
['"row" as string', true],
|
|
67
|
+
])('THEN %s → static:%s', (code, isStatic) => {
|
|
68
|
+
expect(isStaticExpression(expr(code))).toBe(isStatic);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// ─── getStaticKeyAttr ───────────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
describe('GIVEN "no-static-key-in-map" / getStaticKeyAttr', () => {
|
|
75
|
+
it('THEN a string key is static', () => {
|
|
76
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key="row" />;'))).toBeTruthy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('THEN a braced literal key is static', () => {
|
|
80
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={"row"} />;'))).toBeTruthy();
|
|
81
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={1} />;'))).toBeTruthy();
|
|
82
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={`row`} />;'))).toBeTruthy();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('THEN a <Fragment key="…"> is static', () => {
|
|
86
|
+
expect(getStaticKeyAttr(firstElement('const x = <Fragment key="rows" />;'))).toBeTruthy();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('THEN a dynamic key is NOT static', () => {
|
|
90
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={i} />;'))).toBeNull();
|
|
91
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={item.id} />;'))).toBeNull();
|
|
92
|
+
expect(getStaticKeyAttr(firstElement('const x = <A key={`row-${i}`} />;'))).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('THEN a missing key is NOT static', () => {
|
|
96
|
+
expect(getStaticKeyAttr(firstElement('const x = <A />;'))).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// ─── positions that DO trigger ─────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
describe('GIVEN "no-static-key-in-map" / flagged positions', () => {
|
|
103
|
+
it('THEN a static key on a .map arrow-body element', () => {
|
|
104
|
+
expect(lint('const x = list.map((i) => <Row key="row" />);')).toEqual(['staticKeyInIterator']);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('THEN a static key on a .map block-return element', () => {
|
|
108
|
+
expect(lint('const x = list.map((i) => { return <Row key="row" />; });')).toEqual(['staticKeyInIterator']);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('THEN a static <Fragment> key inside .map', () => {
|
|
112
|
+
const code = 'const x = list.map((i) => <Fragment key="rows"><A/><B/></Fragment>);';
|
|
113
|
+
expect(lint(code)).toEqual(['staticKeyInIterator']);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('THEN a conditional element inside .map with a static key', () => {
|
|
117
|
+
expect(lint('const x = list.map((i) => i.ok && <Row key="row" />);')).toEqual(['staticKeyInIterator']);
|
|
118
|
+
expect(lint('const x = list.map((i) => (i.ok ? <A key="a" /> : <B key="b" />));'))
|
|
119
|
+
.toEqual(['staticKeyInIterator', 'staticKeyInIterator']);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('THEN every static-keyed element of an array returned from .flatMap', () => {
|
|
123
|
+
const code = 'const x = list.flatMap((i) => [<A key="a" />, <B key="b" />]);';
|
|
124
|
+
expect(lint(code)).toEqual(['staticKeyInIterator', 'staticKeyInIterator']);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('THEN a parenthesised / cast list item is still flagged', () => {
|
|
128
|
+
expect(lint('const x = list.map((i) => (<Row key="row" />));')).toEqual(['staticKeyInIterator']);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ─── positions that do NOT trigger ─────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
describe('GIVEN "no-static-key-in-map" / allowed positions', () => {
|
|
135
|
+
it('THEN a dynamic key in .map is fine', () => {
|
|
136
|
+
expect(lint('const x = list.map((r) => <Row key={r.id} />);')).toEqual([]);
|
|
137
|
+
expect(lint('const x = list.map((r, i) => <Row key={i} />);')).toEqual([]);
|
|
138
|
+
expect(lint('const x = list.map((r, i) => <Row key={`row-${i}`} />);')).toEqual([]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('THEN a static key on a NESTED element (not the list-item root) is fine', () => {
|
|
142
|
+
const code = 'const x = list.map((r) => <ul key={r.id}><li key="only">{r.n}</li></ul>);';
|
|
143
|
+
expect(lint(code)).toEqual([]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('THEN static keys among the children of a fragment list-item root are fine', () => {
|
|
147
|
+
const code = 'const x = list.map((r) => <Fragment key={r.id}><A key="a" /><B key="b" /></Fragment>);';
|
|
148
|
+
expect(lint(code)).toEqual([]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('THEN a static key outside any iterator is fine', () => {
|
|
152
|
+
expect(lint('const x = <Row key="row" />;')).toEqual([]);
|
|
153
|
+
expect(lint('const x = cond && <Row key="row" />;')).toEqual([]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('THEN a missing key is not this rule\'s concern', () => {
|
|
157
|
+
expect(lint('const x = list.map((r) => <Row />);')).toEqual([]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('THEN forEach / other non-iterator callbacks are ignored', () => {
|
|
161
|
+
expect(lint('list.forEach((r) => render(<Row key="row" />));')).toEqual([]);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// ─── isListItemRoot (white-box) ────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
describe('GIVEN "no-static-key-in-map" / isListItemRoot', () => {
|
|
168
|
+
it('THEN the arrow body of .map is a list-item root', () => {
|
|
169
|
+
expect(isListItemRoot(firstElement('const x = list.map((i) => <A key="a" />);'))).toBe(true);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('THEN a nested element is not', () => {
|
|
173
|
+
// collectElements order is outer-first, so [1] is the inner <li>
|
|
174
|
+
const ast = parse('const x = list.map((i) => <ul><li key="x" /></ul>);', {
|
|
175
|
+
sourceType: 'module',
|
|
176
|
+
plugins: ['jsx'],
|
|
177
|
+
});
|
|
178
|
+
linkParents(ast.program);
|
|
179
|
+
const inner = collectElements(ast.program)[1];
|
|
180
|
+
expect(isListItemRoot(inner)).toBe(false);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('THEN an element outside a .map is not', () => {
|
|
184
|
+
expect(isListItemRoot(firstElement('const x = <A key="a" />;'))).toBe(false);
|
|
185
|
+
});
|
|
186
|
+
});
|