@d-zero/stylelint-rules 5.0.0-alpha.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/const.js +2 -0
- package/dist/index.js +2 -0
- package/dist/rules/declaration-value-type-disallowed-list/index.js +76 -0
- package/dist/rules/declaration-value-type-disallowed-list/index.spec.js +50 -0
- package/dist/rules/length/index.js +40 -0
- package/dist/rules/length/index.spec.js +1 -0
- package/dist/rules/length-pattern/index.js +59 -0
- package/dist/rules/length-pattern/index.spec.js +36 -0
- package/dist/rules/value-pattern-on-type/index.js +59 -0
- package/dist/rules/value-pattern-on-type/index.spec.js +36 -0
- package/dist/utils/create-rule.js +20 -0
- package/dist/utils/get-value-type.js +46 -0
- package/dist/utils/get-value-type.spec.js +28 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 D-ZERO Co., Ltd.
|
|
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/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# `@d-zero/stylelint-rules`
|
|
2
|
+
|
|
3
|
+
Rules plugin for [stylelint](https://stylelint.io/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @d-zero/stylelint-rules --save-dev
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
Add the following to your `.stylelintrc` file:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"plugins": ["@d-zero/stylelint-rules"],
|
|
18
|
+
"rules": {
|
|
19
|
+
"@d-zero/declaration-value-type-disallowed-list": {
|
|
20
|
+
"length": ["/10px/"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Rules
|
|
27
|
+
|
|
28
|
+
- [`@d-zero/declaration-value-type-disallowed-list`](./src/rules/declaration-value-type-disallowed-list/)
|
package/dist/const.js
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import declarationValueIndex from 'stylelint/lib/utils/declarationValueIndex.mjs';
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
import matchesStringOrRegExp from 'stylelint/lib/utils/matchesStringOrRegExp.mjs';
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import validateObjectWithArrayProps from 'stylelint/lib/utils/validateObjectWithArrayProps.mjs';
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import validateObjectWithProps from 'stylelint/lib/utils/validateObjectWithProps.mjs';
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
import validateOptions from 'stylelint/lib/utils/validateOptions.mjs';
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
import { isString } from 'stylelint/lib/utils/validateTypes.mjs';
|
|
14
|
+
import { createRule } from '../../utils/create-rule.js';
|
|
15
|
+
import { getValueType } from '../../utils/get-value-type.js';
|
|
16
|
+
export default createRule({
|
|
17
|
+
name: 'declaration-value-type-disallowed-list',
|
|
18
|
+
rejected: (value, type) => `Unexpected value "${value}" for type "${type}"`,
|
|
19
|
+
rule: (ruleName, messages) => (primary) => {
|
|
20
|
+
return (root, result) => {
|
|
21
|
+
const validOptions = validateOptions(result, ruleName, {
|
|
22
|
+
actual: primary,
|
|
23
|
+
possible: [
|
|
24
|
+
validateObjectWithArrayProps(isString),
|
|
25
|
+
validateObjectWithProps(validateObjectWithArrayProps(isString)),
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
if (!validOptions) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const options = Object.entries(primary);
|
|
32
|
+
if (options.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
root.walkDecls((decl) => {
|
|
36
|
+
const nodes = getValueType(decl);
|
|
37
|
+
for (const node of nodes) {
|
|
38
|
+
if (!node.valueType) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const checkList = options.flatMap(([key, value]) => {
|
|
42
|
+
const matched = matchesStringOrRegExp(node.valueType, key);
|
|
43
|
+
if (!matched) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
if (value.ignoreProperties &&
|
|
50
|
+
matchesStringOrRegExp(decl.prop, value.ignoreProperties)) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
return value.patterns;
|
|
54
|
+
});
|
|
55
|
+
if (checkList.length === 0) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const index = declarationValueIndex(decl) + node.value.sourceIndex;
|
|
59
|
+
const endIndex = index + node.value.sourceEndIndex;
|
|
60
|
+
const raw = decl.value.slice(node.value.sourceIndex, node.value.sourceIndex + node.value.sourceEndIndex);
|
|
61
|
+
if (matchesStringOrRegExp(raw, checkList)) {
|
|
62
|
+
stylelint.utils.report({
|
|
63
|
+
result,
|
|
64
|
+
ruleName,
|
|
65
|
+
message: messages.rejected(raw, node.valueType),
|
|
66
|
+
node: decl,
|
|
67
|
+
index,
|
|
68
|
+
endIndex,
|
|
69
|
+
word: raw,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import { describe, test, expect } from 'vitest';
|
|
3
|
+
import rule from './index.js';
|
|
4
|
+
const { lint } = stylelint;
|
|
5
|
+
const config = (settings = true) => ({
|
|
6
|
+
plugins: [rule],
|
|
7
|
+
rules: {
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
[rule.ruleName]: settings,
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
describe('length-pattern', () => {
|
|
13
|
+
test('length in flex', async () => {
|
|
14
|
+
const {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
results: [{ warnings, parseErrors }], } = await lint({
|
|
17
|
+
code: '* { flex: 1 1 10px }',
|
|
18
|
+
config: config({
|
|
19
|
+
length: ['/[0-9]{2,}px/'],
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
expect(parseErrors).toHaveLength(0);
|
|
23
|
+
expect(warnings).toStrictEqual([
|
|
24
|
+
{
|
|
25
|
+
rule: '@d-zero/declaration-value-type-disallowed-list',
|
|
26
|
+
severity: 'error',
|
|
27
|
+
line: 1,
|
|
28
|
+
endLine: 1,
|
|
29
|
+
column: 15,
|
|
30
|
+
endColumn: 19,
|
|
31
|
+
text: 'Unexpected value "10px" for type "length" (@d-zero/declaration-value-type-disallowed-list)',
|
|
32
|
+
},
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
test('ignoreProperties options', async () => {
|
|
36
|
+
const {
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
results: [{ warnings, parseErrors }], } = await lint({
|
|
39
|
+
code: '* { flex: 1 1 10px }',
|
|
40
|
+
config: config({
|
|
41
|
+
length: {
|
|
42
|
+
ignoreProperties: ['flex'],
|
|
43
|
+
patterns: ['/[0-9]{2,}px/'],
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
expect(parseErrors).toHaveLength(0);
|
|
48
|
+
expect(warnings).toHaveLength(0);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import declarationValueIndex from 'stylelint/lib/utils/declarationValueIndex.mjs';
|
|
3
|
+
|
|
4
|
+
import { getValueType } from '../../utils/get-value-type.js';
|
|
5
|
+
const { createPlugin, utils: { report, ruleMessages }, } = stylelint;
|
|
6
|
+
const ruleName = 'd-zero/length';
|
|
7
|
+
const messages = ruleMessages(ruleName, {
|
|
8
|
+
rejected: (value) => `Unexpected length value "${value}"`,
|
|
9
|
+
});
|
|
10
|
+
const meta = {
|
|
11
|
+
url: 'https://github.com/d-zero-dev/linters/tree/main/packages/%40d-zero/stylelint-rules',
|
|
12
|
+
};
|
|
13
|
+
const ruleFunction = () => {
|
|
14
|
+
return (root, result) => {
|
|
15
|
+
root.walkDecls((decl) => {
|
|
16
|
+
const nodes = getValueType(decl);
|
|
17
|
+
for (const node of nodes) {
|
|
18
|
+
if (node.valueType === 'length') {
|
|
19
|
+
console.log(node.valueType, node);
|
|
20
|
+
const index = declarationValueIndex(decl) + node.value.sourceIndex;
|
|
21
|
+
const endIndex = index + node.value.sourceEndIndex;
|
|
22
|
+
const raw = decl.value.slice(node.value.sourceIndex, node.value.sourceIndex + node.value.sourceEndIndex);
|
|
23
|
+
report({
|
|
24
|
+
result,
|
|
25
|
+
ruleName,
|
|
26
|
+
message: messages.rejected(raw),
|
|
27
|
+
node: decl,
|
|
28
|
+
index,
|
|
29
|
+
endIndex,
|
|
30
|
+
word: raw,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
ruleFunction.ruleName = ruleName;
|
|
38
|
+
ruleFunction.messages = messages;
|
|
39
|
+
ruleFunction.meta = meta;
|
|
40
|
+
export default createPlugin(ruleName, ruleFunction);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import declarationValueIndex from 'stylelint/lib/utils/declarationValueIndex.mjs';
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
import matchesStringOrRegExp from 'stylelint/lib/utils/matchesStringOrRegExp.mjs';
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import validateObjectWithArrayProps from 'stylelint/lib/utils/validateObjectWithArrayProps.mjs';
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import validateOptions from 'stylelint/lib/utils/validateOptions.mjs';
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
import { isString } from 'stylelint/lib/utils/validateTypes.mjs';
|
|
12
|
+
|
|
13
|
+
import { createRule } from '../../utils/create-rule.js';
|
|
14
|
+
import { getValueType } from '../../utils/get-value-type.js';
|
|
15
|
+
export default createRule({
|
|
16
|
+
name: 'length-pattern',
|
|
17
|
+
rejected: (value) => `Unexpected value "${value}"`,
|
|
18
|
+
rule: (ruleName, messages) => (primary) => {
|
|
19
|
+
return (root, result) => {
|
|
20
|
+
const validOptions = validateOptions(result, ruleName, {
|
|
21
|
+
actual: primary,
|
|
22
|
+
possible: [validateObjectWithArrayProps(isString)],
|
|
23
|
+
});
|
|
24
|
+
if (!validOptions) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const options = Object.entries(primary);
|
|
28
|
+
if (options.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
root.walkDecls((decl) => {
|
|
32
|
+
const nodes = getValueType(decl);
|
|
33
|
+
for (const node of nodes) {
|
|
34
|
+
const checkList = options
|
|
35
|
+
.filter(([key]) => matchesStringOrRegExp(node.valueType, key))
|
|
36
|
+
.flatMap(([, value]) => value);
|
|
37
|
+
if (checkList.length === 0) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const index = declarationValueIndex(decl) + node.value.sourceIndex;
|
|
41
|
+
const endIndex = index + node.value.sourceEndIndex;
|
|
42
|
+
const raw = decl.value.slice(node.value.sourceIndex, node.value.sourceIndex + node.value.sourceEndIndex);
|
|
43
|
+
if (matchesStringOrRegExp(raw, checkList)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
stylelint.utils.report({
|
|
47
|
+
result,
|
|
48
|
+
ruleName,
|
|
49
|
+
message: messages.rejected(raw),
|
|
50
|
+
node: decl,
|
|
51
|
+
index,
|
|
52
|
+
endIndex,
|
|
53
|
+
word: raw,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import { describe, test, expect } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import rule from './index.js';
|
|
5
|
+
const { lint } = stylelint;
|
|
6
|
+
const config = (settings = true) => ({
|
|
7
|
+
plugins: [rule],
|
|
8
|
+
rules: {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
[rule.ruleName]: settings,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
describe('length-pattern', () => {
|
|
14
|
+
test('length in flex', async () => {
|
|
15
|
+
const {
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
results: [{ warnings, parseErrors }], } = await lint({
|
|
18
|
+
code: '* { flex: 1 1 10px }',
|
|
19
|
+
config: config({
|
|
20
|
+
length: ['30px'],
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
expect(parseErrors).toHaveLength(0);
|
|
24
|
+
expect(warnings).toStrictEqual([
|
|
25
|
+
{
|
|
26
|
+
rule: '@d-zero/length-pattern',
|
|
27
|
+
severity: 'error',
|
|
28
|
+
line: 1,
|
|
29
|
+
endLine: 1,
|
|
30
|
+
column: 15,
|
|
31
|
+
endColumn: 19,
|
|
32
|
+
text: 'Unexpected value "10px" (@d-zero/length-pattern)',
|
|
33
|
+
},
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import declarationValueIndex from 'stylelint/lib/utils/declarationValueIndex.mjs';
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
import matchesStringOrRegExp from 'stylelint/lib/utils/matchesStringOrRegExp.mjs';
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import validateObjectWithArrayProps from 'stylelint/lib/utils/validateObjectWithArrayProps.mjs';
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import validateOptions from 'stylelint/lib/utils/validateOptions.mjs';
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
import { isString } from 'stylelint/lib/utils/validateTypes.mjs';
|
|
12
|
+
|
|
13
|
+
import { createRule } from '../../utils/create-rule.js';
|
|
14
|
+
import { getValueType } from '../../utils/get-value-type.js';
|
|
15
|
+
export default createRule({
|
|
16
|
+
name: 'value-pattern-on-type',
|
|
17
|
+
rejected: (value) => `Unexpected value "${value}"`,
|
|
18
|
+
rule: (ruleName, messages) => (primary) => {
|
|
19
|
+
return (root, result) => {
|
|
20
|
+
const validOptions = validateOptions(result, ruleName, {
|
|
21
|
+
actual: primary,
|
|
22
|
+
possible: [validateObjectWithArrayProps(isString)],
|
|
23
|
+
});
|
|
24
|
+
if (!validOptions) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const options = Object.entries(primary);
|
|
28
|
+
if (options.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
root.walkDecls((decl) => {
|
|
32
|
+
const nodes = getValueType(decl);
|
|
33
|
+
for (const node of nodes) {
|
|
34
|
+
const checkList = options
|
|
35
|
+
.filter(([key]) => matchesStringOrRegExp(node.valueType, key))
|
|
36
|
+
.flatMap(([, value]) => value);
|
|
37
|
+
if (checkList.length === 0) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const index = declarationValueIndex(decl) + node.value.sourceIndex;
|
|
41
|
+
const endIndex = index + node.value.sourceEndIndex;
|
|
42
|
+
const raw = decl.value.slice(node.value.sourceIndex, node.value.sourceIndex + node.value.sourceEndIndex);
|
|
43
|
+
if (matchesStringOrRegExp(raw, checkList)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
stylelint.utils.report({
|
|
47
|
+
result,
|
|
48
|
+
ruleName,
|
|
49
|
+
message: messages.rejected(raw),
|
|
50
|
+
node: decl,
|
|
51
|
+
index,
|
|
52
|
+
endIndex,
|
|
53
|
+
word: raw,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import { describe, test, expect } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import rule from './index.js';
|
|
5
|
+
const { lint } = stylelint;
|
|
6
|
+
const config = (settings = true) => ({
|
|
7
|
+
plugins: [rule],
|
|
8
|
+
rules: {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
[rule.ruleName]: settings,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
describe('length-pattern', () => {
|
|
14
|
+
test('length in flex', async () => {
|
|
15
|
+
const {
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
results: [{ warnings, parseErrors }], } = await lint({
|
|
18
|
+
code: '* { flex: 1 1 10px }',
|
|
19
|
+
config: config({
|
|
20
|
+
length: ['30px'],
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
expect(parseErrors).toHaveLength(0);
|
|
24
|
+
expect(warnings).toStrictEqual([
|
|
25
|
+
{
|
|
26
|
+
rule: '@d-zero/value-pattern-on-type',
|
|
27
|
+
severity: 'error',
|
|
28
|
+
line: 1,
|
|
29
|
+
endLine: 1,
|
|
30
|
+
column: 15,
|
|
31
|
+
endColumn: 19,
|
|
32
|
+
text: 'Unexpected value "10px" (@d-zero/value-pattern-on-type)',
|
|
33
|
+
},
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import stylelint from 'stylelint';
|
|
2
|
+
import { NAMESPACE, REPOSITORY_URL } from '../const.js';
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
export function createRule(setting) {
|
|
5
|
+
const name = setting.name;
|
|
6
|
+
const ruleName = `${NAMESPACE}/${name}`;
|
|
7
|
+
const meta = {
|
|
8
|
+
url: `${REPOSITORY_URL}/${name}`,
|
|
9
|
+
};
|
|
10
|
+
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
11
|
+
rejected: setting.rejected,
|
|
12
|
+
});
|
|
13
|
+
const ruleBase = setting.rule(ruleName, messages);
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const ruleFunction = ruleBase;
|
|
16
|
+
ruleFunction.ruleName = ruleName;
|
|
17
|
+
ruleFunction.meta = meta;
|
|
18
|
+
ruleFunction.messages = messages;
|
|
19
|
+
return stylelint.createPlugin(ruleName, ruleFunction);
|
|
20
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import CSSTree from '@d-zero/csstree-scss-syntax';
|
|
2
|
+
import postcssValueParser from 'postcss-value-parser';
|
|
3
|
+
export function getValueType(decl) {
|
|
4
|
+
return _getValueType(decl.prop, decl.value);
|
|
5
|
+
}
|
|
6
|
+
function _getValueType(prop, value) {
|
|
7
|
+
const valueAst = postcssValueParser(value);
|
|
8
|
+
const valueAstFromCssTree = CSSTree.parse(value, { context: 'value' });
|
|
9
|
+
let cssTreeDecl = CSSTree.lexer.matchProperty(prop, valueAstFromCssTree);
|
|
10
|
+
if (cssTreeDecl.error?.message === 'Matching for a tree with var() is not supported') {
|
|
11
|
+
value = value.replaceAll(/(var\([^)]+\))/g, (_, $1) => ' '.repeat($1.length - 1) + '1');
|
|
12
|
+
cssTreeDecl = CSSTree.lexer.matchProperty(prop, value);
|
|
13
|
+
}
|
|
14
|
+
const values = valueAst.nodes.filter((node) => node.type === 'string' || node.type === 'function' || node.type === 'word');
|
|
15
|
+
const props = cssTreeDecl.matched;
|
|
16
|
+
if (props === null) {
|
|
17
|
+
throw cssTreeDecl.error;
|
|
18
|
+
}
|
|
19
|
+
const valueTypes = props.match.map((node) => getValueNode(node).syntax.name);
|
|
20
|
+
return values.map((value, i) => {
|
|
21
|
+
const valueType = valueTypes[i] ?? null;
|
|
22
|
+
if (valueType === null) {
|
|
23
|
+
if (value.type === 'word' && value.value.startsWith('$')) {
|
|
24
|
+
return {
|
|
25
|
+
value: value,
|
|
26
|
+
valueType: '$SASS_VARIABLE',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
throw new Error('Value type not found');
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
value,
|
|
33
|
+
valueType,
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function getValueNode(node) {
|
|
38
|
+
if (isProperty(node)) {
|
|
39
|
+
const firstChild = node.match.at(0);
|
|
40
|
+
return getValueNode(firstChild);
|
|
41
|
+
}
|
|
42
|
+
return node;
|
|
43
|
+
}
|
|
44
|
+
function isProperty(node) {
|
|
45
|
+
return node.syntax.type === 'Property';
|
|
46
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
import { describe, test, expect } from 'vitest';
|
|
3
|
+
import { getValueType } from './get-value-type.js';
|
|
4
|
+
function p(css) {
|
|
5
|
+
const root = postcss.parse(css);
|
|
6
|
+
const rule = root.first;
|
|
7
|
+
const decl = rule.first;
|
|
8
|
+
const nodeWithType = getValueType(decl);
|
|
9
|
+
return nodeWithType.map((node) => node.valueType);
|
|
10
|
+
}
|
|
11
|
+
describe('getValueType', () => {
|
|
12
|
+
test('flex', () => {
|
|
13
|
+
expect(p('a { flex: 1 1 calc(var(--foo) * 10vw) }')).toEqual([
|
|
14
|
+
'number',
|
|
15
|
+
'number',
|
|
16
|
+
'length',
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
test('max-width', () => {
|
|
20
|
+
expect(p('a { max-width: 10% }')).toEqual(['length-percentage']);
|
|
21
|
+
});
|
|
22
|
+
test('height vw', () => {
|
|
23
|
+
expect(p('a { height: 100vw }')).toEqual(['length']);
|
|
24
|
+
});
|
|
25
|
+
test('SASS Variable', () => {
|
|
26
|
+
expect(p('a { flex: 1 2 $basis }')).toEqual(['number', 'number', '$SASS_VARIABLE']);
|
|
27
|
+
});
|
|
28
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d-zero/stylelint-rules",
|
|
3
|
+
"version": "5.0.0-alpha.11",
|
|
4
|
+
"description": "Rules of Stylelint for D-ZERO",
|
|
5
|
+
"repository": "https://github.com/d-zero-dev/linters.git",
|
|
6
|
+
"author": "D-ZERO Co., Ltd.",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": false,
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@d-zero/csstree-scss-syntax": "5.0.0-alpha.11",
|
|
24
|
+
"css-tree": "2.3.1",
|
|
25
|
+
"postcss-value-parser": "4.2.0",
|
|
26
|
+
"stylelint": "16.2.1"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "1c2ab0432103986eebd319cbea166e951d8ff524"
|
|
29
|
+
}
|