@plumeria/eslint-plugin 18.2.9 → 18.2.11

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 CHANGED
@@ -107,11 +107,38 @@ physical name, and two shorthands that cross without either containing the
107
107
  other. A shorthand and its longhand are ranked by specificity and never
108
108
  reported.
109
109
 
110
+ It also reports two conditions in one style where one provably matches a subset
111
+ of the other — `(min-width: 900px)` inside `(min-width: 600px)`, or two queries
112
+ naming the same container — and the narrower one is written first. The optimizer
113
+ places the narrower one last whatever the source says, so the declarations read
114
+ the wrong way round. The swap is offered as a suggestion rather than a fix.
115
+
110
116
  A pair of spellings is one property in a horizontal writing mode and two
111
117
  properties in a vertical one, which is per element and cannot be read from the
112
118
  source. The rule reads them as one property; disable the line where an element
113
119
  is known to be vertical.
114
120
 
121
+ ### expand-border-shorthands
122
+
123
+ Expands a border shorthand that bundles a width, a style and a color —
124
+ `border`, `borderBlock`, `borderInline`, and the eight edge forms — into the
125
+ three declarations it stands for. Those bundles are the only properties left
126
+ that cross an axis shorthand without either containing the other, so expanding
127
+ them turns the last unrankable pairs into ordinary shorthand-to-longhand ones.
128
+
129
+ Fixable. A value it cannot split, such as `var(--edge)` or `inherit`, is
130
+ reported without a fix: leaving it silent would let the expanded declarations
131
+ elsewhere outrank it. Not part of `recommended`.
132
+
133
+ ```js
134
+ borderTop: '1px solid red'
135
+ // becomes
136
+ borderTopWidth: '1px', borderTopStyle: 'solid', borderTopColor: 'red'
137
+ ```
138
+
139
+ A shorthand resets what it omits, so `borderBlock: 'solid'` expands with
140
+ `medium` and `currentcolor` written out.
141
+
115
142
  ### no-physical-properties / no-logical-properties
116
143
 
117
144
  Disallow one of the two names a property can carry, so a project writes edges
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  const props_require_import_1 = require("./rules/props-require-import");
3
+ const expand_border_shorthands_1 = require("./rules/expand-border-shorthands");
3
4
  const no_combinator_1 = require("./rules/no-combinator");
4
5
  const no_destructure_1 = require("./rules/no-destructure");
5
6
  const no_inline_object_1 = require("./rules/no-inline-object");
@@ -17,6 +18,7 @@ const validate_values_1 = require("./rules/validate-values");
17
18
  const validate_pseudos_1 = require("./rules/validate-pseudos");
18
19
  const rules = {
19
20
  'props-require-import': props_require_import_1.propsRequireImport,
21
+ 'expand-border-shorthands': expand_border_shorthands_1.expandBorderShorthands,
20
22
  'no-combinator': no_combinator_1.noCombinator,
21
23
  'no-destructure': no_destructure_1.noDestructure,
22
24
  'no-inline-object': no_inline_object_1.noInlineObject,
@@ -0,0 +1,2 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const expandBorderShorthands: Rule.RuleModule;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expandBorderShorthands = void 0;
4
+ const borderShorthand_1 = require("../util/borderShorthand");
5
+ const logicalPhysical_1 = require("../util/logicalPhysical");
6
+ const BUNDLES = new Set(borderShorthand_1.BORDER_BUNDLES);
7
+ exports.expandBorderShorthands = {
8
+ meta: {
9
+ type: 'suggestion',
10
+ docs: {
11
+ description: 'Expand a border shorthand that bundles width, style and color into the three declarations it sets',
12
+ },
13
+ fixable: 'code',
14
+ messages: {
15
+ expand: "'{{ name }}' sets a width, a style and a color at once. It crosses the axis shorthands without either containing the other, so write the three declarations it stands for.",
16
+ opaque: "'{{ name }}' sets a width, a style and a color at once, and this value cannot be split. Write the three declarations yourself, or the ones this rule expands elsewhere will outrank it.",
17
+ },
18
+ schema: [],
19
+ },
20
+ create(context) {
21
+ const plumeriaAliases = {};
22
+ const sourceCode = context.sourceCode;
23
+ return {
24
+ ImportDeclaration(node) {
25
+ if (node.source.value === '@plumeria/core') {
26
+ node.specifiers.forEach((specifier) => {
27
+ if (specifier.type === 'ImportNamespaceSpecifier' ||
28
+ specifier.type === 'ImportDefaultSpecifier') {
29
+ plumeriaAliases[specifier.local.name] = 'NAMESPACE';
30
+ }
31
+ else {
32
+ const spec = specifier;
33
+ const importedName = spec.imported.type === 'Identifier'
34
+ ? spec.imported.name
35
+ : String(spec.imported.value);
36
+ plumeriaAliases[specifier.local.name] = importedName;
37
+ }
38
+ });
39
+ }
40
+ },
41
+ CallExpression(node) {
42
+ let isCssProperties = false;
43
+ if (node.callee.type === 'MemberExpression') {
44
+ if (node.callee.object.type === 'Identifier' &&
45
+ plumeriaAliases[node.callee.object.name] === 'NAMESPACE') {
46
+ const propertyName = node.callee.property.type === 'Identifier'
47
+ ? node.callee.property.name
48
+ : null;
49
+ if (propertyName === 'create' ||
50
+ propertyName === 'keyframes' ||
51
+ propertyName === 'viewTransition') {
52
+ isCssProperties = true;
53
+ }
54
+ }
55
+ }
56
+ else if (node.callee.type === 'Identifier') {
57
+ const aliasName = plumeriaAliases[node.callee.name];
58
+ if (aliasName === 'create' ||
59
+ aliasName === 'keyframes' ||
60
+ aliasName === 'viewTransition') {
61
+ isCssProperties = true;
62
+ }
63
+ }
64
+ if (isCssProperties) {
65
+ node.arguments.forEach((arg) => {
66
+ if (arg.type === 'ObjectExpression') {
67
+ arg.properties.forEach((prop) => {
68
+ if (prop.type === 'Property' &&
69
+ prop.value.type === 'ObjectExpression') {
70
+ checkStyleObject(prop.value);
71
+ }
72
+ });
73
+ }
74
+ });
75
+ }
76
+ },
77
+ };
78
+ function checkStyleObject(node) {
79
+ node.properties.forEach((prop) => {
80
+ if (prop.type !== 'Property')
81
+ return;
82
+ if (prop.value.type === 'ObjectExpression') {
83
+ checkStyleObject(prop.value);
84
+ return;
85
+ }
86
+ let name = '';
87
+ if (!prop.computed) {
88
+ name =
89
+ prop.key.type === 'Identifier'
90
+ ? prop.key.name
91
+ : String(prop.key.value);
92
+ }
93
+ else if (prop.key.type === 'Literal' &&
94
+ typeof prop.key.value === 'string') {
95
+ name = prop.key.value;
96
+ }
97
+ const kebab = (0, logicalPhysical_1.toKebabCase)(name);
98
+ if (!BUNDLES.has(kebab))
99
+ return;
100
+ const literal = prop.value.type === 'Literal' && typeof prop.value.value === 'string'
101
+ ? prop.value.value
102
+ : null;
103
+ const parts = literal === null ? null : (0, borderShorthand_1.splitBorderValue)(literal);
104
+ if (!parts) {
105
+ context.report({
106
+ node: prop.key,
107
+ messageId: 'opaque',
108
+ data: { name },
109
+ });
110
+ return;
111
+ }
112
+ const quote = sourceCode.getText(prop.value).trim().startsWith('"')
113
+ ? '"'
114
+ : "'";
115
+ const indent = ' '.repeat(prop.loc.start.column);
116
+ const declarations = ['width', 'style', 'color']
117
+ .map((part) => `${(0, logicalPhysical_1.toCamelCase)(`${kebab}-${part}`)}: ${quote}${parts[part]}${quote}`)
118
+ .join(`,\n${indent}`);
119
+ context.report({
120
+ node: prop.key,
121
+ messageId: 'expand',
122
+ data: { name },
123
+ fix: (fixer) => fixer.replaceText(prop, declarations),
124
+ });
125
+ });
126
+ }
127
+ },
128
+ };
@@ -58,7 +58,9 @@ exports.noOrderDependentOverlap = {
58
58
  messages: {
59
59
  alias: "'{{ first }}' and '{{ second }}' are the same property under two names, so neither outranks the other. The one written last wins.",
60
60
  crossing: "'{{ first }}' and '{{ second }}' overlap, but neither property outranks the other. The result depends on the order they are written.",
61
+ condition: "'{{ narrow }}' matches only where '{{ broad }}' also matches, so it is the more specific of the two, and both set '{{ property }}'. Written first, it reads as though the broader one wins, which is the opposite of what the stylesheet does.",
61
62
  keep: "Keep '{{ keep }}'",
63
+ swap: "Write '{{ narrow }}' after '{{ broad }}'",
62
64
  },
63
65
  schema: [],
64
66
  },
@@ -120,6 +122,33 @@ exports.noOrderDependentOverlap = {
120
122
  }
121
123
  },
122
124
  };
125
+ function keyNameOf(prop) {
126
+ if (!prop.computed) {
127
+ return prop.key.type === 'Identifier'
128
+ ? prop.key.name
129
+ : String(prop.key.value);
130
+ }
131
+ return prop.key.type === 'Literal' && typeof prop.key.value === 'string'
132
+ ? prop.key.value
133
+ : '';
134
+ }
135
+ function declaredNames(node) {
136
+ const names = new Set();
137
+ node.properties.forEach((prop) => {
138
+ if (prop.type !== 'Property' || prop.value.type === 'ObjectExpression')
139
+ return;
140
+ const name = keyNameOf(prop);
141
+ if (name && !name.startsWith('--'))
142
+ names.add(name);
143
+ });
144
+ return names;
145
+ }
146
+ function swapProperties(fixer, first, second) {
147
+ return [
148
+ fixer.replaceText(first, sourceCode.getText(second)),
149
+ fixer.replaceText(second, sourceCode.getText(first)),
150
+ ];
151
+ }
123
152
  function removeProperty(fixer, prop) {
124
153
  const after = sourceCode.getTokenAfter(prop);
125
154
  if (after && after.value === ',') {
@@ -132,24 +161,24 @@ exports.noOrderDependentOverlap = {
132
161
  }
133
162
  function checkStyleObject(node) {
134
163
  const declarations = [];
164
+ const conditions = [];
135
165
  node.properties.forEach((prop) => {
136
166
  if (prop.type !== 'Property')
137
167
  return;
138
168
  if (prop.value.type === 'ObjectExpression') {
169
+ const condition = keyNameOf(prop);
170
+ if (condition.startsWith('@media') ||
171
+ condition.startsWith('@container')) {
172
+ conditions.push({
173
+ prop,
174
+ name: condition,
175
+ sets: declaredNames(prop.value),
176
+ });
177
+ }
139
178
  checkStyleObject(prop.value);
140
179
  return;
141
180
  }
142
- let name = '';
143
- if (!prop.computed) {
144
- name =
145
- prop.key.type === 'Identifier'
146
- ? prop.key.name
147
- : String(prop.key.value);
148
- }
149
- else if (prop.key.type === 'Literal' &&
150
- typeof prop.key.value === 'string') {
151
- name = prop.key.value;
152
- }
181
+ const name = keyNameOf(prop);
153
182
  if (!name ||
154
183
  name.startsWith(':') ||
155
184
  name.startsWith('[') ||
@@ -159,6 +188,33 @@ exports.noOrderDependentOverlap = {
159
188
  }
160
189
  declarations.push({ prop, name, kebab: (0, logicalPhysical_1.toKebabCase)(name) });
161
190
  });
191
+ for (let i = 0; i < conditions.length; i++) {
192
+ for (let j = i + 1; j < conditions.length; j++) {
193
+ const first = conditions[i];
194
+ const second = conditions[j];
195
+ if (!(0, zss_engine_1.impliesCondition)(first.name, second.name))
196
+ continue;
197
+ const shared = [...first.sets].filter((name) => second.sets.has(name));
198
+ if (shared.length === 0)
199
+ continue;
200
+ context.report({
201
+ node: first.prop.key,
202
+ messageId: 'condition',
203
+ data: {
204
+ narrow: first.name,
205
+ broad: second.name,
206
+ property: shared[0],
207
+ },
208
+ suggest: [
209
+ {
210
+ messageId: 'swap',
211
+ data: { narrow: first.name, broad: second.name },
212
+ fix: (fixer) => swapProperties(fixer, first.prop, second.prop),
213
+ },
214
+ ],
215
+ });
216
+ }
217
+ }
162
218
  for (let i = 0; i < declarations.length; i++) {
163
219
  for (let j = i + 1; j < declarations.length; j++) {
164
220
  const first = declarations[i];
@@ -0,0 +1,6 @@
1
+ export declare const BORDER_BUNDLES: string[];
2
+ export declare const splitBorderValue: (value: string) => {
3
+ width: string;
4
+ style: string;
5
+ color: string;
6
+ } | null;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitBorderValue = exports.BORDER_BUNDLES = void 0;
4
+ exports.BORDER_BUNDLES = [
5
+ 'border',
6
+ 'border-block',
7
+ 'border-inline',
8
+ 'border-top',
9
+ 'border-bottom',
10
+ 'border-left',
11
+ 'border-right',
12
+ 'border-block-start',
13
+ 'border-block-end',
14
+ 'border-inline-start',
15
+ 'border-inline-end',
16
+ ];
17
+ const STYLES = new Set([
18
+ 'none',
19
+ 'hidden',
20
+ 'dotted',
21
+ 'dashed',
22
+ 'solid',
23
+ 'double',
24
+ 'groove',
25
+ 'ridge',
26
+ 'inset',
27
+ 'outset',
28
+ ]);
29
+ const WIDTH_KEYWORDS = new Set(['thin', 'medium', 'thick']);
30
+ const LENGTH = /^[+-]?(\d+\.?\d*|\.\d+)([a-z%]+)?$/i;
31
+ const INITIAL = { width: 'medium', style: 'none', color: 'currentcolor' };
32
+ const tokenize = (value) => {
33
+ const tokens = [];
34
+ let depth = 0;
35
+ let current = '';
36
+ for (const character of value.trim()) {
37
+ if (character === '(')
38
+ depth++;
39
+ if (character === ')')
40
+ depth--;
41
+ if (depth === 0 && /\s/.test(character)) {
42
+ if (current)
43
+ tokens.push(current);
44
+ current = '';
45
+ continue;
46
+ }
47
+ current += character;
48
+ }
49
+ if (current)
50
+ tokens.push(current);
51
+ return depth === 0 && tokens.length > 0 && tokens.length <= 3 ? tokens : null;
52
+ };
53
+ const isWidth = (token) => WIDTH_KEYWORDS.has(token) || LENGTH.test(token) || /^calc\(/i.test(token);
54
+ const isColor = (token) => /^(#|rgb|hsl|hwb|lab|lch|oklab|oklch|color\(|light-dark\()/i.test(token) ||
55
+ /^[a-z-]+$/i.test(token);
56
+ const splitBorderValue = (value) => {
57
+ const tokens = tokenize(value);
58
+ if (!tokens)
59
+ return null;
60
+ if (tokens.some((token) => /\bvar\(|^inherit$|^initial$|^unset$|^revert/i.test(token)))
61
+ return null;
62
+ const parts = {};
63
+ for (const token of tokens) {
64
+ const lower = token.toLowerCase();
65
+ if (!parts.style && STYLES.has(lower)) {
66
+ parts.style = token;
67
+ continue;
68
+ }
69
+ if (!parts.width && isWidth(lower)) {
70
+ parts.width = token;
71
+ continue;
72
+ }
73
+ if (!parts.color && isColor(token)) {
74
+ parts.color = token;
75
+ continue;
76
+ }
77
+ return null;
78
+ }
79
+ return {
80
+ width: parts.width ?? INITIAL.width,
81
+ style: parts.style ?? INITIAL.style,
82
+ color: parts.color ?? INITIAL.color,
83
+ };
84
+ };
85
+ exports.splitBorderValue = splitBorderValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/eslint-plugin",
3
- "version": "18.2.9",
3
+ "version": "18.2.11",
4
4
  "description": "Plumeria ESLint plugin",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -54,7 +54,7 @@
54
54
  "dependencies": {
55
55
  "@typescript-eslint/utils": "^8.60.0",
56
56
  "known-css-properties": "^0.37.0",
57
- "zss-engine": "2.4.3"
57
+ "zss-engine": "2.5.0"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "rimraf dist && pnpm cjs",