@maltjoy/eslint-rules 0.6.1 → 6.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 maltcommunity / apps
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maltjoy/eslint-rules",
3
- "version": "0.6.1",
3
+ "version": "6.1.0",
4
4
  "description": "Custom ESLint rules for the @maltjoy/core-vue design system",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -18,10 +18,6 @@
18
18
  "rules",
19
19
  "README.md"
20
20
  ],
21
- "scripts": {
22
- "build": "pnpm test",
23
- "test": "node --test tests/*.test.js"
24
- },
25
21
  "peerDependencies": {
26
22
  "eslint": ">=8"
27
23
  },
@@ -29,5 +25,9 @@
29
25
  "eslint": "9.39.4",
30
26
  "vue-eslint-parser": "10.4.0"
31
27
  },
32
- "license": "MIT"
33
- }
28
+ "license": "MIT",
29
+ "scripts": {
30
+ "build": "pnpm test",
31
+ "test": "node --test tests/*.test.js"
32
+ }
33
+ }
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  containsElement,
3
3
  createTemplateBodyVisitor,
4
+ hasOnlyDirectCustomComponentChildren,
4
5
  isElementNamed,
5
6
  isNamedSlotTemplate,
6
7
  } from '../utils/vue-ast.js';
@@ -28,12 +29,12 @@ export default {
28
29
  description:
29
30
  'require VJoyTemplate template-title slots to contain a heading element',
30
31
  details:
31
- 'The template-title slot must contain an h1 to h6 element, preferably an h1, to preserve the page heading hierarchy.',
32
+ 'The template-title slot must contain an h1 to h6 element, preferably an h1, to preserve the page heading hierarchy. A direct custom component child is also allowed when it renders the heading internally.',
32
33
  examples: {
33
34
  incorrect:
34
35
  '<VJoyTemplate>\n <template #template-title>\n <div>Page title</div>\n </template>\n</VJoyTemplate>',
35
36
  correct:
36
- '<VJoyTemplate>\n <template #template-title>\n <h1>Page title</h1>\n </template>\n</VJoyTemplate>',
37
+ '<VJoyTemplate>\n <template #template-title>\n <h1>Page title</h1>\n </template>\n</VJoyTemplate>\n\n<VJoyTemplate>\n <template #template-title>\n <PageTitle />\n </template>\n</VJoyTemplate>',
37
38
  },
38
39
  recommended: true,
39
40
  category: 'accessibility',
@@ -49,7 +50,8 @@ export default {
49
50
  parentElement &&
50
51
  isVJoyTemplateElement(parentElement) &&
51
52
  isTemplateTitleSlot(node) &&
52
- !containsHeadingElement(node)
53
+ !containsHeadingElement(node) &&
54
+ !hasOnlyDirectCustomComponentChildren(node)
53
55
  ) {
54
56
  context.report({
55
57
  node,
@@ -19,6 +19,7 @@ export default createVueStyleInspectionRule({
19
19
  },
20
20
  messageId: 'arbitraryFontSize',
21
21
  message: ERROR_MESSAGE,
22
+ ruleName: 'no-arbitrary-font-size',
22
23
  inspectStyleText: inspectStyleTextForArbitraryFontSize,
23
24
  inspectStyleExpression: inspectStyleExpressionForFontSize,
24
25
  });
@@ -18,5 +18,6 @@ export default createVueStyleInspectionRule({
18
18
  },
19
19
  messageId: 'hardcodedColorName',
20
20
  message: ERROR_MESSAGE,
21
+ ruleName: 'no-hardcoded-color-names',
21
22
  inspectStyleText: inspectStyleTextForNamedColors,
22
23
  });
@@ -18,5 +18,6 @@ export default createVueStyleInspectionRule({
18
18
  },
19
19
  messageId: 'hardcodedHex',
20
20
  message: ERROR_MESSAGE,
21
+ ruleName: 'no-hardcoded-hex-colors-in-vue',
21
22
  inspectStyleText: inspectStyleTextForHexColors,
22
23
  });
@@ -18,5 +18,6 @@ export default createVueStyleInspectionRule({
18
18
  },
19
19
  messageId: 'hardcodedRgb',
20
20
  message: ERROR_MESSAGE,
21
+ ruleName: 'no-hardcoded-rgb-colors-in-vue',
21
22
  inspectStyleText: inspectStyleTextForRgbColors,
22
23
  });
@@ -17,6 +17,7 @@ export default createVueStyleInspectionRule({
17
17
  },
18
18
  messageId: 'inlineStyleColor',
19
19
  message: ERROR_MESSAGE,
20
+ ruleName: 'no-inline-style-colors',
20
21
  includeStyleBlocks: false,
21
22
  inspectStyleText: inspectStyleTextForInlineColors,
22
23
  inspectStyleExpression: inspectStyleExpressionForInlineColors,
@@ -1,3 +1,4 @@
1
+ import { createStyleBlockSuppressionChecker } from '../utils/style-block-directives.js';
1
2
  import { createTemplateBodyVisitor } from '../utils/vue-ast.js';
2
3
  import {
3
4
  collectStringLiteralsFromExpression,
@@ -13,6 +14,8 @@ import {
13
14
  styleStringHasColorDeclaration,
14
15
  } from '../utils/style-inspection.js';
15
16
 
17
+ const PLUGIN_NAME = '@maltjoy/eslint-rules';
18
+
16
19
  function visitStyleAttribute(context, attribute, handlers) {
17
20
  if (isStaticStyleAttribute(attribute)) {
18
21
  handlers.onStyleText?.(attribute.value.value, attribute.value.range[0]);
@@ -39,6 +42,7 @@ export function createVueStyleInspectionRule({
39
42
  examples,
40
43
  messageId,
41
44
  message,
45
+ ruleName,
42
46
  inspectStyleText,
43
47
  inspectStyleExpression,
44
48
  includeStyleBlocks = true,
@@ -59,6 +63,8 @@ export function createVueStyleInspectionRule({
59
63
  },
60
64
  },
61
65
  create(context) {
66
+ const ruleId = `${PLUGIN_NAME}/${ruleName}`;
67
+
62
68
  const handlers = {
63
69
  onStyleText(text, startIndex) {
64
70
  inspectStyleText(context, text, startIndex, messageId);
@@ -82,7 +88,14 @@ export function createVueStyleInspectionRule({
82
88
  Program(node) {
83
89
  if (includeStyleBlocks) {
84
90
  for (const block of iterateStyleBlockContents(context.sourceCode)) {
85
- inspectStyleText(context, block.content, block.startIndex, messageId);
91
+ const suppression = createStyleBlockSuppressionChecker(
92
+ block.content,
93
+ block.startIndex,
94
+ context.sourceCode,
95
+ ruleId,
96
+ );
97
+
98
+ inspectStyleText(context, block.content, block.startIndex, messageId, suppression);
86
99
  }
87
100
  }
88
101
 
@@ -93,32 +106,38 @@ export function createVueStyleInspectionRule({
93
106
  };
94
107
  }
95
108
 
96
- export function inspectStyleTextForHexColors(context, text, startIndex, messageId) {
97
- reportHexColorsInText(context, text, startIndex, messageId);
109
+ export function inspectStyleTextForHexColors(context, text, startIndex, messageId, suppression) {
110
+ reportHexColorsInText(context, text, startIndex, messageId, suppression);
98
111
  }
99
112
 
100
- export function inspectStyleTextForRgbColors(context, text, startIndex, messageId) {
101
- reportRgbColorsInText(context, text, startIndex, messageId);
113
+ export function inspectStyleTextForRgbColors(context, text, startIndex, messageId, suppression) {
114
+ reportRgbColorsInText(context, text, startIndex, messageId, suppression);
102
115
  }
103
116
 
104
- export function inspectStyleTextForNamedColors(context, text, startIndex, messageId) {
105
- reportNamedColorsInText(context, text, startIndex, messageId);
117
+ export function inspectStyleTextForNamedColors(context, text, startIndex, messageId, suppression) {
118
+ reportNamedColorsInText(context, text, startIndex, messageId, suppression);
106
119
  }
107
120
 
108
- export function inspectStyleTextForInlineColors(context, text, startIndex, messageId) {
109
- if (styleStringHasColorDeclaration(text)) {
110
- context.report({
111
- loc: {
112
- start: context.sourceCode.getLocFromIndex(startIndex),
113
- end: context.sourceCode.getLocFromIndex(startIndex + text.length),
114
- },
115
- messageId,
116
- });
121
+ export function inspectStyleTextForInlineColors(context, text, startIndex, messageId, suppression) {
122
+ if (!styleStringHasColorDeclaration(text)) {
123
+ return;
117
124
  }
125
+
126
+ if (suppression?.isSuppressed(startIndex)) {
127
+ return;
128
+ }
129
+
130
+ context.report({
131
+ loc: {
132
+ start: context.sourceCode.getLocFromIndex(startIndex),
133
+ end: context.sourceCode.getLocFromIndex(startIndex + text.length),
134
+ },
135
+ messageId,
136
+ });
118
137
  }
119
138
 
120
- export function inspectStyleTextForArbitraryFontSize(context, text, startIndex, messageId) {
121
- reportArbitraryFontSizesInText(context, text, startIndex, messageId);
139
+ export function inspectStyleTextForArbitraryFontSize(context, text, startIndex, messageId, suppression) {
140
+ reportArbitraryFontSizesInText(context, text, startIndex, messageId, suppression);
122
141
  }
123
142
 
124
143
  export function inspectStyleExpressionForInlineColors(context, expression, attribute, messageId) {
@@ -0,0 +1,166 @@
1
+ const BLOCK_COMMENT_DIRECTIVE_PATTERN =
2
+ /\/\*[\s*]*eslint-(disable-next-line|disable-line|disable|enable)(?:\s+([^*]*?))?(?:\s*--[^*]*)?\*\//gi;
3
+
4
+ const LINE_COMMENT_DIRECTIVE_PATTERN =
5
+ /^\s*\/\/\s*eslint-(disable-next-line|disable-line|disable|enable)(?:\s+(.*))?/;
6
+
7
+ function parseRuleList(description) {
8
+ if (description === undefined || description.trim() === '') {
9
+ return null;
10
+ }
11
+
12
+ return description
13
+ .split(',')
14
+ .map((rule) => rule.trim())
15
+ .filter(Boolean);
16
+ }
17
+
18
+ function ruleMatchesDisabledList(disabledRules, ruleId) {
19
+ if (disabledRules === null) {
20
+ return true;
21
+ }
22
+
23
+ const shortName = ruleId.includes('/') ? ruleId.slice(ruleId.lastIndexOf('/') + 1) : ruleId;
24
+
25
+ return disabledRules.some((disabledRule) => {
26
+ if (disabledRule === ruleId || disabledRule === shortName) {
27
+ return true;
28
+ }
29
+
30
+ return disabledRule.endsWith(`/${shortName}`);
31
+ });
32
+ }
33
+
34
+ function extractDirectivesFromLine(lineText) {
35
+ const directives = [];
36
+ const blockPattern = new RegExp(BLOCK_COMMENT_DIRECTIVE_PATTERN.source, 'gi');
37
+ let match = blockPattern.exec(lineText);
38
+
39
+ while (match) {
40
+ directives.push({
41
+ type: match[1],
42
+ rules: parseRuleList(match[2]),
43
+ });
44
+ match = blockPattern.exec(lineText);
45
+ }
46
+
47
+ const lineMatch = lineText.match(LINE_COMMENT_DIRECTIVE_PATTERN);
48
+ if (lineMatch) {
49
+ directives.push({
50
+ type: lineMatch[1],
51
+ rules: parseRuleList(lineMatch[2]),
52
+ });
53
+ }
54
+
55
+ return directives;
56
+ }
57
+
58
+ function buildDirectiveState(content, blockStartIndex, sourceCode) {
59
+ const blockStartLine = sourceCode.getLocFromIndex(blockStartIndex).line;
60
+ const lines = content.split('\n');
61
+ const disableLine = new Map();
62
+ const disableNextLine = new Map();
63
+ const disableStarts = [];
64
+ const enableLines = new Map();
65
+
66
+ lines.forEach((lineText, index) => {
67
+ const lineNumber = blockStartLine + index;
68
+
69
+ extractDirectivesFromLine(lineText).forEach(({ type, rules }) => {
70
+ switch (type) {
71
+ case 'disable-next-line':
72
+ disableNextLine.set(lineNumber, rules);
73
+ break;
74
+ case 'disable-line':
75
+ disableLine.set(lineNumber, rules);
76
+ break;
77
+ case 'disable':
78
+ disableStarts.push({ line: lineNumber, rules });
79
+ break;
80
+ case 'enable':
81
+ enableLines.set(lineNumber, rules);
82
+ break;
83
+ default:
84
+ break;
85
+ }
86
+ });
87
+ });
88
+
89
+ return {
90
+ disableLine,
91
+ disableNextLine,
92
+ disableStarts,
93
+ enableLines,
94
+ };
95
+ }
96
+
97
+ function isLineDisabledByBlockDirective(line, ruleId, state) {
98
+ for (const disableStart of state.disableStarts) {
99
+ if (line < disableStart.line) {
100
+ continue;
101
+ }
102
+
103
+ let enableLine = Number.POSITIVE_INFINITY;
104
+
105
+ for (const [candidateEnableLine, enableRules] of state.enableLines) {
106
+ if (candidateEnableLine < disableStart.line) {
107
+ continue;
108
+ }
109
+
110
+ if (
111
+ enableRules === null ||
112
+ disableStart.rules === null ||
113
+ ruleMatchesDisabledList(enableRules, ruleId)
114
+ ) {
115
+ enableLine = Math.min(enableLine, candidateEnableLine);
116
+ }
117
+ }
118
+
119
+ if (
120
+ line >= disableStart.line &&
121
+ line < enableLine &&
122
+ ruleMatchesDisabledList(disableStart.rules, ruleId)
123
+ ) {
124
+ return true;
125
+ }
126
+ }
127
+
128
+ return false;
129
+ }
130
+
131
+ function isLineSuppressed(line, ruleId, state) {
132
+ if (
133
+ state.disableLine.has(line) &&
134
+ ruleMatchesDisabledList(state.disableLine.get(line), ruleId)
135
+ ) {
136
+ return true;
137
+ }
138
+
139
+ if (
140
+ state.disableNextLine.has(line - 1) &&
141
+ ruleMatchesDisabledList(state.disableNextLine.get(line - 1), ruleId)
142
+ ) {
143
+ return true;
144
+ }
145
+
146
+ return isLineDisabledByBlockDirective(line, ruleId, state);
147
+ }
148
+
149
+ export function createStyleBlockSuppressionChecker(content, blockStartIndex, sourceCode, ruleId) {
150
+ const state = buildDirectiveState(content, blockStartIndex, sourceCode);
151
+
152
+ return {
153
+ isSuppressed(absoluteIndex) {
154
+ const line = sourceCode.getLocFromIndex(absoluteIndex).line;
155
+ return isLineSuppressed(line, ruleId, state);
156
+ },
157
+ };
158
+ }
159
+
160
+ export {
161
+ buildDirectiveState,
162
+ extractDirectivesFromLine,
163
+ isLineSuppressed,
164
+ parseRuleList,
165
+ ruleMatchesDisabledList,
166
+ };
@@ -319,12 +319,25 @@ export function isBoundStyleAttribute(attribute) {
319
319
  );
320
320
  }
321
321
 
322
- export function reportMatchesInText(context, text, startIndex, messageId, matches) {
322
+ export function reportMatchesInText(
323
+ context,
324
+ text,
325
+ startIndex,
326
+ messageId,
327
+ matches,
328
+ suppression,
329
+ ) {
323
330
  for (const match of matches) {
331
+ const absoluteIndex = startIndex + match.index;
332
+
333
+ if (suppression?.isSuppressed(absoluteIndex)) {
334
+ continue;
335
+ }
336
+
324
337
  context.report({
325
338
  loc: {
326
- start: context.sourceCode.getLocFromIndex(startIndex + match.index),
327
- end: context.sourceCode.getLocFromIndex(startIndex + match.index + match.length),
339
+ start: context.sourceCode.getLocFromIndex(absoluteIndex),
340
+ end: context.sourceCode.getLocFromIndex(absoluteIndex + match.length),
328
341
  },
329
342
  messageId,
330
343
  data: match.data,
@@ -332,7 +345,7 @@ export function reportMatchesInText(context, text, startIndex, messageId, matche
332
345
  }
333
346
  }
334
347
 
335
- export function reportRgbColorsInText(context, text, startIndex, messageId) {
348
+ export function reportRgbColorsInText(context, text, startIndex, messageId, suppression) {
336
349
  reportMatchesInText(
337
350
  context,
338
351
  text,
@@ -343,14 +356,22 @@ export function reportRgbColorsInText(context, text, startIndex, messageId) {
343
356
  length: match.value.length,
344
357
  data: { value: match.value },
345
358
  })),
359
+ suppression,
346
360
  );
347
361
  }
348
362
 
349
- export function reportNamedColorsInText(context, text, startIndex, messageId) {
350
- reportMatchesInText(context, text, startIndex, messageId, [...iterateNamedColorDeclarations(text)]);
363
+ export function reportNamedColorsInText(context, text, startIndex, messageId, suppression) {
364
+ reportMatchesInText(
365
+ context,
366
+ text,
367
+ startIndex,
368
+ messageId,
369
+ [...iterateNamedColorDeclarations(text)],
370
+ suppression,
371
+ );
351
372
  }
352
373
 
353
- export function reportHexColorsInText(context, text, startIndex, messageId) {
374
+ export function reportHexColorsInText(context, text, startIndex, messageId, suppression) {
354
375
  reportMatchesInText(
355
376
  context,
356
377
  text,
@@ -361,10 +382,11 @@ export function reportHexColorsInText(context, text, startIndex, messageId) {
361
382
  length: match.value.length,
362
383
  data: { value: match.value },
363
384
  })),
385
+ suppression,
364
386
  );
365
387
  }
366
388
 
367
- export function reportArbitraryFontSizesInText(context, text, startIndex, messageId) {
389
+ export function reportArbitraryFontSizesInText(context, text, startIndex, messageId, suppression) {
368
390
  reportMatchesInText(
369
391
  context,
370
392
  text,
@@ -374,6 +396,7 @@ export function reportArbitraryFontSizesInText(context, text, startIndex, messag
374
396
  ...match,
375
397
  data: { value: match.value },
376
398
  })),
399
+ suppression,
377
400
  );
378
401
  }
379
402
 
@@ -14,6 +14,40 @@ export function getStaticName(node) {
14
14
  return undefined;
15
15
  }
16
16
 
17
+ const HTML_ELEMENT_NAMES = new Set([
18
+ 'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo',
19
+ 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup',
20
+ 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed',
21
+ 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
22
+ 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd',
23
+ 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'meta', 'meter', 'nav',
24
+ 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre',
25
+ 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'slot',
26
+ 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody',
27
+ 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u',
28
+ 'ul', 'var', 'video', 'wbr',
29
+ ]);
30
+
31
+ export function isHtmlElementName(name) {
32
+ return typeof name === 'string' && HTML_ELEMENT_NAMES.has(name.toLowerCase());
33
+ }
34
+
35
+ export function isCustomComponentElement(node) {
36
+ return (
37
+ node?.type === 'VElement' &&
38
+ typeof node.name === 'string' &&
39
+ !isHtmlElementName(node.name)
40
+ );
41
+ }
42
+
43
+ export function hasOnlyDirectCustomComponentChildren(node) {
44
+ const directElements = Array.isArray(node?.children)
45
+ ? node.children.filter((child) => child.type === 'VElement')
46
+ : [];
47
+
48
+ return directElements.length > 0 && directElements.every(isCustomComponentElement);
49
+ }
50
+
17
51
  export function isElementNamed(node, names) {
18
52
  return (
19
53
  node?.type === 'VElement' &&