@tcy/eslint-rules 1.9.0 → 1.9.2
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/README.md +4 -0
- package/coverage/clover.xml +69 -67
- package/coverage/coverage-final.json +1 -1
- 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 +1 -1
- 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 +241 -115
- 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 +20 -20
- 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 +1 -1
- package/coverage/lcov-report/src/plugin.js.html +1 -1
- 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 +153 -149
- package/decl/back/back.d.ts +13 -1
- package/decl/client/client.d.ts +13 -1
- package/decl/enforce-tailwind-important.d.ts +30 -1
- package/decl/plugin.d.ts +13 -1
- package/package.json +3 -3
- package/src/enforce-tailwind-important.js +63 -21
- package/tests/enforce-tailwind-important.spec.js +141 -1
|
@@ -5,31 +5,76 @@ export const isMissingImportant = (cls) => !/^[a-zA-Z0-9-%&>*.:',/[\]()_@]+(:?!)
|
|
|
5
5
|
|
|
6
6
|
export const addImportant = (cls) => `${cls}!`;
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Plain (non-Tailwind) classes that must NOT receive the "!" modifier.
|
|
10
|
+
*
|
|
11
|
+
* These ship in the widgets' bundled Bootstrap-style grid CSS and are matched by
|
|
12
|
+
* a bare `.class` selector. Appending "!" turns the token into `class!`, which
|
|
13
|
+
* matches no rule at all (Tailwind does not emit anything for it, and `.class`
|
|
14
|
+
* does not match the `class!` token) - silently breaking the layout.
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_IGNORED_CLASSES = [
|
|
17
|
+
'row',
|
|
18
|
+
'no-gutters',
|
|
19
|
+
'container',
|
|
20
|
+
'container-fluid',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether a class token must be flagged for a missing "!" modifier.
|
|
25
|
+
* @param {string} cls the class token
|
|
26
|
+
* @param {Set<string>} ignored classes that are allowed without "!"
|
|
27
|
+
*/
|
|
28
|
+
export const shouldFlag = (cls, ignored) => isMissingImportant(cls) && !ignored.has(cls);
|
|
29
|
+
|
|
30
|
+
/** Build the set of ignored classes from the rule options. */
|
|
31
|
+
export const resolveIgnored = (options) => new Set([
|
|
32
|
+
...DEFAULT_IGNORED_CLASSES,
|
|
33
|
+
...((options && options.ignore) || []),
|
|
34
|
+
]);
|
|
35
|
+
|
|
8
36
|
export default {
|
|
9
37
|
meta: {
|
|
10
38
|
type: 'layout',
|
|
11
39
|
docs: { description: 'Enforce Tailwind classes end with "!"', recommended: false },
|
|
12
40
|
fixable: 'code',
|
|
13
|
-
schema: [
|
|
41
|
+
schema: [
|
|
42
|
+
{
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: {
|
|
45
|
+
ignore: {
|
|
46
|
+
type: 'array',
|
|
47
|
+
items: { type: 'string' },
|
|
48
|
+
default: [],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
14
54
|
messages: { missingImportant: 'Tailwind class "{{cls}}" should end with "!"' },
|
|
15
55
|
},
|
|
16
56
|
|
|
17
57
|
create(context) {
|
|
18
58
|
const source = context.getSourceCode();
|
|
19
59
|
|
|
60
|
+
const ignored = resolveIgnored(context.options[0]);
|
|
61
|
+
|
|
62
|
+
// a class needs the "!" modifier unless it is explicitly ignored
|
|
63
|
+
const isFlagged = (cls) => shouldFlag(cls, ignored);
|
|
64
|
+
|
|
20
65
|
//----------------------------------------------------------------------
|
|
21
66
|
// Helpers
|
|
22
67
|
//----------------------------------------------------------------------
|
|
23
68
|
function reportIfMissing(node, value) {
|
|
24
69
|
const classes = value.split(/\s+/).map((cls) => cls.trim()).filter(Boolean);
|
|
25
|
-
const missing = classes.filter(
|
|
70
|
+
const missing = classes.filter(isFlagged);
|
|
26
71
|
|
|
27
72
|
// nothing to fix
|
|
28
73
|
if (!missing.length) {
|
|
29
74
|
return;
|
|
30
75
|
}
|
|
31
76
|
|
|
32
|
-
const fixed = classes.map((c) => (
|
|
77
|
+
const fixed = classes.map((c) => (isFlagged(c) ? addImportant(c) : c)).join(' ');
|
|
33
78
|
context.report({
|
|
34
79
|
node,
|
|
35
80
|
messageId: 'missingImportant',
|
|
@@ -51,7 +96,7 @@ export default {
|
|
|
51
96
|
for (let i = 0; i < quasis.length; i += 1) {
|
|
52
97
|
// fix literal part
|
|
53
98
|
const parts = quasis[i].value.raw.split(/\s+/).filter(Boolean);
|
|
54
|
-
const fixed = parts.map((p) => (
|
|
99
|
+
const fixed = parts.map((p) => (isFlagged(p) ? addImportant(p) : p)).join(' ');
|
|
55
100
|
out += fixed;
|
|
56
101
|
|
|
57
102
|
// add space before expression if needed
|
|
@@ -71,24 +116,13 @@ export default {
|
|
|
71
116
|
|
|
72
117
|
switch (node.type) {
|
|
73
118
|
case 'Literal':
|
|
74
|
-
|
|
119
|
+
// skip non-string literals, e.g. `cond && 0`
|
|
120
|
+
if (typeof node.value === 'string') {
|
|
121
|
+
reportIfMissing(node, node.value);
|
|
122
|
+
}
|
|
75
123
|
break;
|
|
76
124
|
case 'ArrayExpression':
|
|
77
|
-
node.elements.forEach(
|
|
78
|
-
if (!el) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (el.type === 'Literal') {
|
|
83
|
-
reportIfMissing(el, el.value);
|
|
84
|
-
} else if (el.type === 'ObjectExpression') {
|
|
85
|
-
el.properties.forEach((p) => {
|
|
86
|
-
if (p.key?.type === 'Literal' && typeof p.key.value === 'string') {
|
|
87
|
-
reportIfMissing(p.key, p.key.value);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
});
|
|
125
|
+
node.elements.forEach(processExpression);
|
|
92
126
|
break;
|
|
93
127
|
case 'ObjectExpression':
|
|
94
128
|
node.properties.forEach((p) => {
|
|
@@ -102,7 +136,7 @@ export default {
|
|
|
102
136
|
.some((q) => q.value.raw
|
|
103
137
|
.split(/\s+/)
|
|
104
138
|
.filter(Boolean)
|
|
105
|
-
.some(
|
|
139
|
+
.some(isFlagged));
|
|
106
140
|
|
|
107
141
|
if (!hasMissing) {
|
|
108
142
|
return;
|
|
@@ -123,6 +157,14 @@ export default {
|
|
|
123
157
|
processExpression(node.consequent);
|
|
124
158
|
processExpression(node.alternate);
|
|
125
159
|
break;
|
|
160
|
+
case 'LogicalExpression':
|
|
161
|
+
// `cond && 'cls'`: only the right side is a class
|
|
162
|
+
// `cls || 'cls'` / `cls ?? 'cls'`: both sides can be classes
|
|
163
|
+
if (node.operator !== '&&') {
|
|
164
|
+
processExpression(node.left);
|
|
165
|
+
}
|
|
166
|
+
processExpression(node.right);
|
|
167
|
+
break;
|
|
126
168
|
default:
|
|
127
169
|
break;
|
|
128
170
|
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import rule, {
|
|
3
|
+
DEFAULT_IGNORED_CLASSES,
|
|
4
|
+
isMissingImportant,
|
|
5
|
+
resolveIgnored,
|
|
6
|
+
shouldFlag,
|
|
7
|
+
} from '../src/enforce-tailwind-important';
|
|
2
8
|
|
|
3
9
|
describe('GIVEN "enforce-tailwind-important"', () => {
|
|
4
10
|
describe('WHEN "test" important class', () => {
|
|
@@ -12,4 +18,138 @@ describe('GIVEN "enforce-tailwind-important"', () => {
|
|
|
12
18
|
expect(isImportantClass).toBe(true);
|
|
13
19
|
});
|
|
14
20
|
});
|
|
21
|
+
|
|
22
|
+
describe('WHEN a class is in the ignore list', () => {
|
|
23
|
+
it('THEN "no-gutters" and "container-fluid" are ignored by default', () => {
|
|
24
|
+
expect(DEFAULT_IGNORED_CLASSES).toEqual(
|
|
25
|
+
expect.arrayContaining(['no-gutters', 'container-fluid']),
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('THEN a default-ignored class is not flagged', () => {
|
|
30
|
+
const ignored = resolveIgnored();
|
|
31
|
+
|
|
32
|
+
expect(shouldFlag('no-gutters', ignored)).toBe(false);
|
|
33
|
+
expect(shouldFlag('container-fluid', ignored)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('THEN a real Tailwind class is still flagged', () => {
|
|
37
|
+
const ignored = resolveIgnored();
|
|
38
|
+
|
|
39
|
+
expect(shouldFlag('w-full', ignored)).toBe(true);
|
|
40
|
+
expect(shouldFlag('max-w-[800px]', ignored)).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('THEN an already-important class is never flagged', () => {
|
|
44
|
+
const ignored = resolveIgnored();
|
|
45
|
+
|
|
46
|
+
expect(shouldFlag('w-full!', ignored)).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('THEN classes from the "ignore" option are merged with the defaults', () => {
|
|
50
|
+
const ignored = resolveIgnored({ ignore: ['my-legacy-class'] });
|
|
51
|
+
|
|
52
|
+
expect(shouldFlag('my-legacy-class', ignored)).toBe(false);
|
|
53
|
+
expect(shouldFlag('no-gutters', ignored)).toBe(false);
|
|
54
|
+
expect(shouldFlag('text-red-500', ignored)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ─── rule (end-to-end) ──────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
/** Run the rule on `code`, return reported classes and the auto-fixed source. */
|
|
62
|
+
const lint = (code, options = []) => {
|
|
63
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'estree'] });
|
|
64
|
+
const reports = [];
|
|
65
|
+
const context = {
|
|
66
|
+
options,
|
|
67
|
+
getSourceCode: () => ({ getText: (node) => code.slice(node.start, node.end) }),
|
|
68
|
+
report: (r) => reports.push(r),
|
|
69
|
+
};
|
|
70
|
+
const visitors = rule.create(context);
|
|
71
|
+
|
|
72
|
+
const walk = (node) => {
|
|
73
|
+
if (!node || typeof node.type !== 'string') return;
|
|
74
|
+
visitors[node.type]?.(node);
|
|
75
|
+
Object.keys(node).forEach((key) => {
|
|
76
|
+
const value = node[key];
|
|
77
|
+
if (Array.isArray(value)) value.forEach(walk);
|
|
78
|
+
else if (value && typeof value.type === 'string') walk(value);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
walk(ast.program);
|
|
82
|
+
|
|
83
|
+
// apply fixes from the end so offsets stay valid
|
|
84
|
+
let output = code;
|
|
85
|
+
reports
|
|
86
|
+
.map((r) => r.fix({ replaceText: (node, text) => ({ node, text }) }))
|
|
87
|
+
.sort((a, b) => b.node.start - a.node.start)
|
|
88
|
+
.forEach(({ node, text }) => {
|
|
89
|
+
output = output.slice(0, node.start) + text + output.slice(node.end);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return { reports, output };
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
describe('GIVEN "enforce-tailwind-important" / logical expressions', () => {
|
|
96
|
+
it('THEN `cond && "cls"` is flagged and fixed', () => {
|
|
97
|
+
const { reports, output } = lint("const x = <div class={readonly && 'hidden'} />;");
|
|
98
|
+
|
|
99
|
+
expect(reports).toHaveLength(1);
|
|
100
|
+
expect(output).toBe("const x = <div class={readonly && 'hidden!'} />;");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('THEN only the missing classes are fixed', () => {
|
|
104
|
+
const { output } = lint("const x = <div className={readonly && 'something! else'} />;");
|
|
105
|
+
|
|
106
|
+
expect(output).toBe("const x = <div className={readonly && 'something! else!'} />;");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('THEN already-important classes are not flagged', () => {
|
|
110
|
+
const { reports } = lint("const x = <div class={readonly && 'something! else!'} />;");
|
|
111
|
+
|
|
112
|
+
expect(reports).toHaveLength(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('THEN both sides of `||` / `??` are checked', () => {
|
|
116
|
+
const { output } = lint("const x = <div class={(a || 'flex') ?? 'grid'} />;");
|
|
117
|
+
|
|
118
|
+
expect(output).toBe("const x = <div class={(a || 'flex!') ?? 'grid!'} />;");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('THEN the left side of `&&` is not treated as a class', () => {
|
|
122
|
+
const { reports } = lint("const x = <div class={'foo' && 'bar!'} />;");
|
|
123
|
+
|
|
124
|
+
expect(reports).toHaveLength(0);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('THEN nested ternary / logical expressions are checked', () => {
|
|
128
|
+
const { output } = lint("const x = <div class={a ? b && 'p-2' : 'm-2'} />;");
|
|
129
|
+
|
|
130
|
+
expect(output).toBe("const x = <div class={a ? b && 'p-2!' : 'm-2!'} />;");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('THEN logical expressions inside an array are checked', () => {
|
|
134
|
+
const { reports, output } = lint(
|
|
135
|
+
"const x = <div class={[readonly && 'hidden', 'something', 'else']} />;",
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
expect(reports).toHaveLength(3);
|
|
139
|
+
expect(output).toBe(
|
|
140
|
+
"const x = <div class={[readonly && 'hidden!', 'something!', 'else!']} />;",
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('THEN array elements that are already important are not flagged', () => {
|
|
145
|
+
const { reports } = lint(
|
|
146
|
+
"const x = <div class={[readonly && 'hidden!', 'something!', { 'p-2!': a }]} />;",
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
expect(reports).toHaveLength(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('THEN non-string literals do not crash', () => {
|
|
153
|
+
expect(() => lint('const x = <div class={count && 0} />;')).not.toThrow();
|
|
154
|
+
});
|
|
15
155
|
});
|