@tcy/eslint-rules 1.0.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.
@@ -0,0 +1,48 @@
1
+ name: Publish to NPM
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ - test
7
+
8
+ permissions:
9
+ id-token: write # Required for OIDC
10
+ contents: read
11
+
12
+ jobs:
13
+ build_publish:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - name: detect package.version changes
17
+ uses: MontyD/package-json-updated-action@master
18
+ id: version-updated
19
+ env:
20
+ GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
21
+
22
+ - name: Checkout
23
+ uses: actions/checkout@v4
24
+ if: steps.version-updated.outputs.has-updated
25
+
26
+ - name: Setup Node
27
+ uses: actions/setup-node@v4
28
+ if: steps.version-updated.outputs.has-updated
29
+ with:
30
+ node-version: '24.x'
31
+ registry-url: 'https://registry.npmjs.org'
32
+
33
+ - name: Publish package on NPM 📦
34
+ if: steps.version-updated.outputs.has-updated
35
+ run: |
36
+ npm install -g npm@latest
37
+ # Get version from package.json
38
+ VERSION=$(node -p "require('./package.json').version")
39
+ echo "Package version: $VERSION"
40
+
41
+ # Check if version contains a prerelease identifier
42
+ if [[ "$VERSION" == *"-"* ]]; then
43
+ echo "Prerelease detected, publishing with tag 'beta'"
44
+ npm publish --tag beta
45
+ else
46
+ echo "Stable release, publishing with default tag 'latest'"
47
+ npm publish
48
+ fi
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Custom rules
2
+
3
+ ```typescript
4
+ import { plugin as customRules } from 'tcy@eslint-rules/src/index.mjs';
5
+
6
+ const customConfig = [
7
+ {
8
+ files: ['**/*.+(ts|tsx|mts|cts)'],
9
+ plugins: {
10
+ 'custom-rules': customRules,
11
+ },
12
+ rules: {
13
+ 'custom-rules/import-specifiers-per-line': ['error', { maxSpecifiers: 4 }],
14
+ 'custom-rules/export-specifiers-per-line': 'error',
15
+ 'custom-rules/array-elements-per-line': 'error',
16
+ }
17
+ }
18
+ ];
19
+
20
+
21
+ export default [
22
+ ...customConfig,
23
+ ];
24
+ ```
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@tcy/eslint-rules",
3
+ "version": "1.0.0",
4
+ "description": "```typescript import { plugin as customRules } from 'tcy@eslint-rules/src/index.mjs';",
5
+ "homepage": "https://github.com/tcastelly/eslint-rules#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/tcastelly/eslint-rules/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/tcastelly/eslint-rules.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "",
15
+ "type": "module",
16
+ "main": "src/index.js",
17
+ "scripts": {}
18
+ }
@@ -0,0 +1,61 @@
1
+ export default {
2
+ meta: {
3
+ type: 'layout',
4
+ docs: {
5
+ description: 'Enforce one array element per line when the array is multiline',
6
+ category: 'Stylistic Issues',
7
+ recommended: false,
8
+ },
9
+ fixable: 'code',
10
+ schema: [],
11
+ },
12
+ create(context) {
13
+ return {
14
+ ArrayExpression(node) {
15
+ if (node.elements.length <= 1) {
16
+ return;
17
+ }
18
+
19
+ const sourceCode = context.getSourceCode();
20
+ const first = node.elements[0];
21
+ if (!first) return;
22
+
23
+ const arrayStartLine = node.loc.start.line;
24
+ const firstLine = first.loc.start.line;
25
+
26
+ // Only apply if first element is NOT on same line as '['
27
+ if (firstLine === arrayStartLine) {
28
+ return;
29
+ }
30
+
31
+ // Check if already formatted correctly
32
+ let alreadyCorrect = true;
33
+ for (let i = 1; i < node.elements.length; i += 1) {
34
+ const prev = node.elements[i - 1];
35
+ const curr = node.elements[i];
36
+ if (prev && curr && prev.loc.start.line === curr.loc.start.line) {
37
+ alreadyCorrect = false;
38
+ break;
39
+ }
40
+ }
41
+ if (alreadyCorrect) return;
42
+
43
+ context.report({
44
+ node,
45
+ message: `Each array element must be on its own
46
+ line when first element is on a new line.`,
47
+ fix(fixer) {
48
+ const elementsText = node.elements.map((el) => {
49
+ if (!el) return '';
50
+ let text = sourceCode.getText(el).trim();
51
+ if (!text.endsWith(',')) text += ',';
52
+ return ` ${text}`;
53
+ }).join('\n');
54
+
55
+ return fixer.replaceText(node, `[\n${elementsText}\n]`);
56
+ },
57
+ });
58
+ },
59
+ };
60
+ },
61
+ };
@@ -0,0 +1,160 @@
1
+ /**
2
+ * @fileoverview Enforce all Tailwind classes to end with "!" (important modifier)
3
+ */
4
+ export const isMissingImportant = (cls) => !/^[a-zA-Z0-9-%&>*.:',/[\]()_]+(:?!)$/.test(cls);
5
+
6
+ export const addImportant = (cls) => `${cls}!`;
7
+
8
+ export default {
9
+ meta: {
10
+ type: 'layout',
11
+ docs: { description: 'Enforce Tailwind classes end with "!"', recommended: false },
12
+ fixable: 'code',
13
+ schema: [],
14
+ messages: { missingImportant: 'Tailwind class "{{cls}}" should end with "!"' },
15
+ },
16
+
17
+ create(context) {
18
+ const source = context.getSourceCode();
19
+
20
+ //----------------------------------------------------------------------
21
+ // Helpers
22
+ //----------------------------------------------------------------------
23
+ function reportIfMissing(node, value) {
24
+ const classes = value.split(/\s+/).map((cls) => cls.trim()).filter(Boolean);
25
+ const missing = classes.filter(isMissingImportant);
26
+
27
+ // nothing to fix
28
+ if (!missing.length) {
29
+ return;
30
+ }
31
+
32
+ const fixed = classes.map((c) => (isMissingImportant(c) ? addImportant(c) : c)).join(' ');
33
+ context.report({
34
+ node,
35
+ messageId: 'missingImportant',
36
+ data: { cls: missing.join(' ') },
37
+ fix(fixer) {
38
+ const text = source.getText(node);
39
+ const quote = text[0] || '"';
40
+
41
+ return fixer.replaceText(node, `${quote}${fixed}${quote}`);
42
+ },
43
+ });
44
+ }
45
+
46
+ function buildFixedTemplateText(templateLiteral) {
47
+ const exprTexts = templateLiteral.expressions.map((e) => source.getText(e));
48
+ const { quasis } = templateLiteral;
49
+
50
+ let out = '`';
51
+ for (let i = 0; i < quasis.length; i += 1) {
52
+ // fix literal part
53
+ const parts = quasis[i].value.raw.split(/\s+/).filter(Boolean);
54
+ const fixed = parts.map((p) => (isMissingImportant(p) ? addImportant(p) : p)).join(' ');
55
+ out += fixed;
56
+
57
+ // add space before expression if needed
58
+ if (i < exprTexts.length) {
59
+ if (fixed && !fixed.endsWith(' ')) out += ' ';
60
+ out += `\${${exprTexts[i]}}`;
61
+ }
62
+ }
63
+ out += '`';
64
+ return out;
65
+ }
66
+
67
+ function processExpression(node) {
68
+ if (!node) {
69
+ return;
70
+ }
71
+
72
+ switch (node.type) {
73
+ case 'Literal':
74
+ reportIfMissing(node, node.value);
75
+ break;
76
+ case 'ArrayExpression':
77
+ node.elements.forEach((el) => {
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
+ });
92
+ break;
93
+ case 'ObjectExpression':
94
+ node.properties.forEach((p) => {
95
+ if (p.key?.type === 'Literal' && typeof p.key.value === 'string') {
96
+ reportIfMissing(p.key, p.key.value);
97
+ }
98
+ });
99
+ break;
100
+ case 'TemplateLiteral': {
101
+ const hasMissing = node.quasis
102
+ .some((q) => q.value.raw
103
+ .split(/\s+/)
104
+ .filter(Boolean)
105
+ .some(isMissingImportant));
106
+
107
+ if (!hasMissing) {
108
+ return;
109
+ }
110
+
111
+ context.report({
112
+ node,
113
+ messageId: 'missingImportant',
114
+ fix(fixer) {
115
+ const newTemplate = buildFixedTemplateText(node);
116
+
117
+ return fixer.replaceText(node, newTemplate);
118
+ },
119
+ });
120
+ break;
121
+ }
122
+ default:
123
+ break;
124
+ }
125
+ }
126
+
127
+ //----------------------------------------------------------------------
128
+ // Main visitor
129
+ //----------------------------------------------------------------------
130
+ return {
131
+ JSXAttribute(node) {
132
+ if (!node.name) return;
133
+ if (!['class', 'className'].includes(node.name.name)) return;
134
+
135
+ const { value } = node;
136
+ if (!value) return;
137
+
138
+ if (value.type === 'Literal' && typeof value.value === 'string') {
139
+ reportIfMissing(value, value.value);
140
+ } else if (value.type === 'JSXExpressionContainer') {
141
+ processExpression(value.expression);
142
+ }
143
+ },
144
+
145
+ // also handle variables like const diffPosClass = 'text-green-700';
146
+ VariableDeclarator(node) {
147
+ if (
148
+ node.id
149
+ && node.id.type === 'Identifier'
150
+ && /cls|class/i.test(node.id.name) // only match variables with 'class' or 'cls'
151
+ && node.init
152
+ && node.init.type === 'Literal'
153
+ && typeof node.init.value === 'string'
154
+ ) {
155
+ reportIfMissing(node.init, node.init.value);
156
+ }
157
+ },
158
+ };
159
+ },
160
+ };
@@ -0,0 +1,63 @@
1
+ export default {
2
+ meta: {
3
+ type: 'layout',
4
+ docs: {
5
+ description: 'Enforce one export specifier per line with trailing comma',
6
+ },
7
+ fixable: 'code',
8
+ schema: [],
9
+ },
10
+ create(context) {
11
+ return {
12
+ ExportNamedDeclaration(node) {
13
+ const { specifiers } = node;
14
+ if (!specifiers || specifiers.length <= 1) return;
15
+
16
+ const sourceCode = context.getSourceCode();
17
+
18
+ // Check if each specifier is already on its own line and last has comma
19
+ let needsFix = false;
20
+ for (let i = 1; i < specifiers.length; i += 1) {
21
+ if (specifiers[i].loc.start.line === specifiers[i - 1].loc.start.line) {
22
+ needsFix = true;
23
+ break;
24
+ }
25
+ }
26
+
27
+ needsFix = needsFix || !!sourceCode
28
+ .getText(node, { comments: false })
29
+ .slice(specifiers[0].range[0]).match(/, [a-zA-Z0-9_]/);
30
+
31
+ if (!needsFix) {
32
+ return;
33
+ }
34
+
35
+ const last = specifiers[specifiers.length - 1];
36
+ const afterLastChar = sourceCode.getText().slice(last.range[1], last.range[1] + 1);
37
+
38
+ const needsComma = afterLastChar !== ',';
39
+
40
+ // Generate fixed text without adding extra braces
41
+ const exportText = specifiers
42
+ .map((s) => sourceCode.getText(s))
43
+ .join(',\n ');
44
+
45
+ const fixedText = needsComma ? `${exportText},` : exportText;
46
+
47
+ // Replace only the inner part of the braces
48
+ const first = specifiers[0].range[0];
49
+ const lastRange = specifiers[specifiers.length - 1].range[1];
50
+
51
+ context.report({
52
+ node,
53
+ message: 'Each export specifier must be on its own line with trailing comma.',
54
+ fix(fixer) {
55
+ const r = fixer.replaceTextRange([first, lastRange], fixedText);
56
+
57
+ return r;
58
+ },
59
+ });
60
+ },
61
+ };
62
+ },
63
+ };
@@ -0,0 +1,102 @@
1
+ export default {
2
+ meta: {
3
+ type: 'layout',
4
+ docs: {
5
+ description: 'Enforce one import specifier per line if more than 4 specifiers',
6
+ },
7
+ fixable: 'code',
8
+ schema: [
9
+ {
10
+ type: 'object',
11
+ properties: {
12
+ maxSpecifiers: { type: 'number', default: 4 },
13
+ maxLen: { type: 'number', default: 120 },
14
+ },
15
+ additionalProperties: false,
16
+ },
17
+ ],
18
+ },
19
+ create(context) {
20
+ const options = context.options[0] || {
21
+ maxSpecifiers: 4,
22
+ };
23
+
24
+ const { maxSpecifiers, maxLen } = options;
25
+
26
+ return {
27
+ ImportDeclaration(node) {
28
+ const sourceCode = context.getSourceCode();
29
+
30
+ let importText = sourceCode.getText(node);
31
+
32
+ const len = importText.length;
33
+
34
+ const specifiers = node.specifiers.filter((s) => s.type === 'ImportSpecifier');
35
+
36
+ // ignore <= 4 imports
37
+ let needsFix = specifiers.length >= maxSpecifiers;
38
+
39
+ if (!needsFix && !!maxLen && len > maxLen) {
40
+ needsFix = true;
41
+ }
42
+
43
+ if (!needsFix || !specifiers.length) {
44
+ return;
45
+ }
46
+
47
+ // Check if each specifier is already on its own line
48
+ for (let i = 1; i < specifiers.length; i += 1) {
49
+ if (specifiers[i].loc.start.line === specifiers[i - 1].loc.start.line) {
50
+ needsFix = true;
51
+ break;
52
+ }
53
+ }
54
+
55
+ if (!needsFix) {
56
+ return;
57
+ }
58
+
59
+ // Check if last specifier has a trailing comma
60
+ const last = specifiers[specifiers.length - 1];
61
+ const afterLastChar = sourceCode.getText().slice(last.range[1], last.range[1] + 1);
62
+
63
+ const addComma = afterLastChar !== ',';
64
+
65
+ const importLine = node.loc.start.line;
66
+ const lastBracketLine = node.loc.end.line;
67
+
68
+ const addFirstBreakLine = importLine === specifiers[0].loc.start.line;
69
+ const addLastBreakLine = lastBracketLine === specifiers[specifiers.length - 1].loc.start.line;
70
+
71
+ // Generate fixed text **without adding extra braces**
72
+ importText = specifiers
73
+ .map((s) => sourceCode.getText(s))
74
+ .join(',\n ');
75
+
76
+ const fixedText = (
77
+ `${addFirstBreakLine ? '\n ' : ''}`
78
+ + `${importText}`
79
+ + `${addComma ? ',' : ''}`
80
+ + `${addLastBreakLine ? '\n ' : ''}`
81
+ );
82
+
83
+ // Replace **only the inner part** of the braces
84
+ const first = specifiers[0].range[0];
85
+ const lastRange = specifiers[specifiers.length - 1].range[1];
86
+
87
+ if (sourceCode.getText().slice(first, lastRange) === fixedText) {
88
+ return;
89
+ }
90
+
91
+ context.report({
92
+ node,
93
+ message: 'Each import specifier must be on its own line when there are more than 4 imports, with trailing comma.',
94
+ fix(fixer) {
95
+ const r = fixer.replaceTextRange([first, lastRange], fixedText);
96
+ return r;
97
+ },
98
+ });
99
+ },
100
+ };
101
+ },
102
+ };
package/src/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import importSpecifiersPerLineRule from './import-specifiers-per-line.js';
2
+ import arrayElementsPerLineRule from './array-elements-per-line.js';
3
+ import exportSpecifiersPerLineRule from './export-specifiers-per-line.js';
4
+ import jsxExpressionStringLiterals from './jsx-expression-string-literals.js';
5
+ import enforceTailwindImportant from './enforce-tailwind-important.js';
6
+
7
+ export default {
8
+ rules: {
9
+ 'import-specifiers-per-line': importSpecifiersPerLineRule,
10
+ 'array-elements-per-line': arrayElementsPerLineRule,
11
+ 'export-specifiers-per-line': exportSpecifiersPerLineRule,
12
+ 'jsx-expression-string-literals': jsxExpressionStringLiterals,
13
+ 'enforce-tailwind-important': enforceTailwindImportant,
14
+ },
15
+ };
@@ -0,0 +1,41 @@
1
+ export default {
2
+ meta: {
3
+ type: 'layout',
4
+ docs: {
5
+ description:
6
+ "Convert JSX expression string literals like id={'test'} to id=\"test\"",
7
+ },
8
+ fixable: 'code',
9
+ schema: [],
10
+ },
11
+
12
+ create(context) {
13
+ return {
14
+ JSXAttribute(node) {
15
+ if (!node.value) return;
16
+
17
+ // Look for: id={ 'test' }
18
+ if (node.value.type !== 'JSXExpressionContainer') return;
19
+
20
+ const expr = node.value.expression;
21
+
22
+ // Must be a simple string literal
23
+ if (expr.type !== 'Literal' || typeof expr.value !== 'string') return;
24
+
25
+ const stringValue = expr.value;
26
+
27
+ context.report({
28
+ node: node.value,
29
+ message:
30
+ 'String literal attributes should not use expression containers. Use id="{{value}}".',
31
+ data: { value: stringValue },
32
+ fix(fixer) {
33
+ // Directly replace with a normal JSX string literal
34
+ const newText = `"${stringValue.replace(/"/g, '\\"')}"`;
35
+ return fixer.replaceText(node.value, newText);
36
+ },
37
+ });
38
+ },
39
+ };
40
+ },
41
+ };