@tcy/eslint-rules 1.7.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 +122 -4
- package/coverage/coverage-final.json +2 -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 +8 -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 +39 -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 +14 -2
- package/coverage/lcov-report/src/recommended.js.html +1 -1
- package/coverage/lcov-report/src/require-fragment-key.js.html +787 -0
- 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 +316 -0
- package/decl/back/back.d.ts +42 -0
- package/decl/client/client.d.ts +44 -0
- package/decl/no-static-key-in-map.d.ts +66 -0
- package/decl/plugin.d.ts +42 -0
- package/decl/require-fragment-key.d.ts +69 -0
- package/package.json +9 -9
- package/src/client/client.js +2 -0
- package/src/no-static-key-in-map.js +182 -0
- package/src/plugin.js +4 -0
- package/src/require-fragment-key.js +234 -0
- package/tests/no-static-key-in-map.spec.js +186 -0
- package/tests/require-fragment-key.spec.js +160 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import { analyzeFragment, classifyFragment, isKeylessFragment } from '../src/require-fragment-key';
|
|
3
|
+
|
|
4
|
+
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
const linkParents = (node, parent = null) => {
|
|
7
|
+
if (!node || typeof node.type !== 'string') return;
|
|
8
|
+
node.parent = parent;
|
|
9
|
+
Object.keys(node).forEach((key) => {
|
|
10
|
+
if (key === 'parent') return;
|
|
11
|
+
const value = node[key];
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
value.forEach((child) => linkParents(child, node));
|
|
14
|
+
} else if (value && typeof value.type === 'string') {
|
|
15
|
+
linkParents(value, node);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const collectFragments = (node, out = []) => {
|
|
21
|
+
if (!node || typeof node.type !== 'string') return out;
|
|
22
|
+
if (node.type === 'JSXFragment' || node.type === 'JSXElement') out.push(node);
|
|
23
|
+
Object.keys(node).forEach((key) => {
|
|
24
|
+
if (key === 'parent') return;
|
|
25
|
+
const value = node[key];
|
|
26
|
+
if (Array.isArray(value)) value.forEach((child) => collectFragments(child, out));
|
|
27
|
+
else if (value && typeof value.type === 'string') collectFragments(value, out);
|
|
28
|
+
});
|
|
29
|
+
return out;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Parse `code`, return every messageId the rule would emit (in source order). */
|
|
33
|
+
const lint = (code, options) => {
|
|
34
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
35
|
+
linkParents(ast.program);
|
|
36
|
+
return collectFragments(ast.program)
|
|
37
|
+
.map((node) => analyzeFragment(node, options))
|
|
38
|
+
.filter(Boolean);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// ─── isKeylessFragment ──────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
describe('GIVEN "require-fragment-key" / isKeylessFragment', () => {
|
|
44
|
+
const first = (code) => {
|
|
45
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
46
|
+
return collectFragments(ast.program).find(
|
|
47
|
+
(n) => n.type === 'JSXFragment'
|
|
48
|
+
|| (n.type === 'JSXElement' && n.openingElement.name.name === 'Fragment'),
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
it('THEN a shorthand fragment is key-less', () => {
|
|
53
|
+
expect(isKeylessFragment(first('const x = <><A/></>;'))).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('THEN a <Fragment> without key is key-less', () => {
|
|
57
|
+
expect(isKeylessFragment(first('const x = <Fragment><A/></Fragment>;'))).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('THEN a <Fragment key=…> is NOT key-less', () => {
|
|
61
|
+
expect(isKeylessFragment(first('const x = <Fragment key="k"><A/></Fragment>;'))).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('THEN a <Fragment {...spread}> is treated as keyed (no guessing)', () => {
|
|
65
|
+
expect(isKeylessFragment(first('const x = <Fragment {...p}><A/></Fragment>;'))).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ─── positions that DO require a key ────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
describe('GIVEN "require-fragment-key" / required positions', () => {
|
|
72
|
+
it('THEN a fragment returned from .map is a list item', () => {
|
|
73
|
+
expect(lint('const x = list.map((i) => (<><A/><B/></>));')).toEqual(['listItem']);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('THEN a conditional fragment inside .map is a list item', () => {
|
|
77
|
+
expect(lint('const x = list.map((i) => i.ok && (<><A/></>));')).toEqual(['listItem']);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('THEN a fragment returned from a .map block statement is a list item', () => {
|
|
81
|
+
expect(lint('const x = list.map((i) => { return <><A/></>; });')).toEqual(['listItem']);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('THEN a conditional fragment beside another dynamic sibling must be keyed', () => {
|
|
85
|
+
const code = 'const x = (<P><L/>{a && <X/>}{c && (<><A/><B/></>)}<S/></P>);';
|
|
86
|
+
expect(lint(code)).toEqual(['conditionalSibling']);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('THEN a v-if / v-else pair of fragments must both be keyed', () => {
|
|
90
|
+
const code = 'const x = (<P>{c ? (<><A/></>) : (<><B/></>)}{other && <Y/>}</P>);';
|
|
91
|
+
expect(lint(code)).toEqual(['conditionalSibling', 'conditionalSibling']);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('THEN a conditional fragment beside a .map sibling must be keyed', () => {
|
|
95
|
+
const code = 'const x = (<P><L/>{c && (<><A/></>)}{rows.map((r) => <Row />)}</P>);';
|
|
96
|
+
expect(lint(code)).toEqual(['conditionalSibling']);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('THEN a lone conditional fragment in a static list is flagged only when strict:false', () => {
|
|
100
|
+
const code = 'const x = (<P><L/>{c && (<><A/><B/></>)}</P>);';
|
|
101
|
+
expect(lint(code)).toEqual([]);
|
|
102
|
+
expect(lint(code, { strict: false })).toEqual(['conditionalSibling']);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// ─── positions that do NOT require a key ────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
describe('GIVEN "require-fragment-key" / allowed positions', () => {
|
|
109
|
+
it('THEN the sole return of an arrow is fine', () => {
|
|
110
|
+
expect(lint('const f = () => (<><A/><B/></>);')).toEqual([]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('THEN the sole return of a render function is fine', () => {
|
|
114
|
+
expect(lint('function r() { return (<><A/><B/></>); }')).toEqual([]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('THEN a slot function returning a fragment is fine', () => {
|
|
118
|
+
expect(lint('const s = { default: () => (<><A/><B/></>) };')).toEqual([]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('THEN a conditional fragment that is the whole return is fine', () => {
|
|
122
|
+
expect(lint('const f = () => cond && (<><A/><B/></>);')).toEqual([]);
|
|
123
|
+
expect(lint('const f = () => (cond ? (<><A/></>) : (<><B/></>));')).toEqual([]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('THEN a fragment that is the only child of a parent is fine', () => {
|
|
127
|
+
expect(lint('const x = (<Parent>{cond && (<><A/><B/></>)}</Parent>);')).toEqual([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('THEN an already-keyed fragment is fine', () => {
|
|
131
|
+
const code = 'const x = (<P><L/>{c && (<Fragment key="k"><A/></Fragment>)}{d && <Y/>}</P>);';
|
|
132
|
+
expect(lint(code)).toEqual([]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('THEN a static (non-conditional) sibling fragment is fine', () => {
|
|
136
|
+
expect(lint('const x = (<Parent><Legend/><><A/><B/></></Parent>);')).toEqual([]);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// ─── classifyFragment (white-box) ──────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
describe('GIVEN "require-fragment-key" / classifyFragment', () => {
|
|
143
|
+
const classifyFirst = (code) => {
|
|
144
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
145
|
+
linkParents(ast.program);
|
|
146
|
+
return classifyFragment(collectFragments(ast.program).find((n) => n.type === 'JSXFragment'));
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
it('THEN it counts significant siblings and dynamic siblings', () => {
|
|
150
|
+
const info = classifyFirst('const x = (<P><L/>{a && <X/>}{c && (<><A/></>)}<S/></P>);');
|
|
151
|
+
expect(info.kind).toBe('jsx-child');
|
|
152
|
+
expect(info.conditional).toBe(true);
|
|
153
|
+
expect(info.siblingCount).toBe(3);
|
|
154
|
+
expect(info.dynamicSibling).toBe(true);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('THEN a sole return classifies as "return"', () => {
|
|
158
|
+
expect(classifyFirst('const f = () => (<><A/></>);').kind).toBe('return');
|
|
159
|
+
});
|
|
160
|
+
});
|