@unocss/eslint-plugin 0.49.3

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) 2021-PRESENT Anthony Fu <https://github.com/antfu>
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,9 @@
1
+ # @unocss/eslint-plugin
2
+
3
+ ESLint plugin for UnoCSS.
4
+
5
+ **Currently working in progress, breaking changes may NOT follow semver.**
6
+
7
+ ## Installation
8
+
9
+ Please refer to [@unocss/eslint-config](https://github.com/unocss/unocss/blob/main/packages/eslint-config) for installation.
package/dist/dirs.cjs ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const url = require('url');
6
+
7
+ const distDir = url.fileURLToPath(new URL("../dist", (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dirs.cjs', document.baseURI).href))));
8
+
9
+ exports.distDir = distDir;
package/dist/dirs.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare const distDir: string;
2
+
3
+ export { distDir };
package/dist/dirs.mjs ADDED
@@ -0,0 +1,5 @@
1
+ import { fileURLToPath } from 'url';
2
+
3
+ const distDir = fileURLToPath(new URL("../dist", import.meta.url));
4
+
5
+ export { distDir };
package/dist/index.cjs ADDED
@@ -0,0 +1,148 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const utils = require('@typescript-eslint/utils');
5
+ const synckit = require('synckit');
6
+ const dirs = require('./dirs.cjs');
7
+ const MagicString = require('magic-string');
8
+ require('url');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
11
+
12
+ const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
13
+
14
+ const CLASS_FIELDS = ["class", "classname"];
15
+
16
+ const sortClasses$1 = synckit.createSyncFn(path.join(dirs.distDir, "worker-sort.cjs"));
17
+ const order = utils.ESLintUtils.RuleCreator((name) => name)({
18
+ name: "order",
19
+ meta: {
20
+ type: "layout",
21
+ fixable: "code",
22
+ docs: {
23
+ description: "Order of UnoCSS utilities in class attribute",
24
+ recommended: "warn"
25
+ },
26
+ messages: {
27
+ "invalid-order": "UnoCSS utilities are not ordered"
28
+ },
29
+ schema: []
30
+ },
31
+ defaultOptions: [],
32
+ create(context) {
33
+ function checkLiteral(node) {
34
+ if (typeof node.value !== "string")
35
+ return;
36
+ const input = node.value;
37
+ const sorted = sortClasses$1(input);
38
+ if (sorted !== input) {
39
+ context.report({
40
+ node,
41
+ messageId: "invalid-order",
42
+ fix(fixer) {
43
+ return fixer.replaceText(node, `"${sorted.trim()}"`);
44
+ }
45
+ });
46
+ }
47
+ }
48
+ const scriptVisitor = {
49
+ JSXAttribute(node) {
50
+ if (typeof node.name.name === "string" && CLASS_FIELDS.includes(node.name.name.toLowerCase()) && node.value) {
51
+ if (node.value.type === "Literal")
52
+ checkLiteral(node.value);
53
+ }
54
+ }
55
+ };
56
+ const templateBodyVisitor = {
57
+ VAttribute(node) {
58
+ if (node.key.name === "class") {
59
+ if (node.value.type === "VLiteral")
60
+ checkLiteral(node.value);
61
+ }
62
+ }
63
+ };
64
+ if (context.parserServices == null || context.parserServices.defineTemplateBodyVisitor == null) {
65
+ return scriptVisitor;
66
+ } else {
67
+ return context.parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor);
68
+ }
69
+ }
70
+ });
71
+
72
+ const sortClasses = synckit.createSyncFn(path.join(dirs.distDir, "worker-sort.cjs"));
73
+ const INGORE_ATTRIBUTES = ["style", "class", "classname", "value"];
74
+ const orderAttributify = utils.ESLintUtils.RuleCreator((name) => name)({
75
+ name: "order-attributify",
76
+ meta: {
77
+ type: "layout",
78
+ fixable: "code",
79
+ docs: {
80
+ description: "Order of UnoCSS attributes",
81
+ recommended: false
82
+ },
83
+ messages: {
84
+ "invalid-order": "UnoCSS attributes are not ordered"
85
+ },
86
+ schema: []
87
+ },
88
+ defaultOptions: [],
89
+ create(context) {
90
+ const scriptVisitor = {};
91
+ const templateBodyVisitor = {
92
+ VStartTag(node) {
93
+ const valueless = node.attributes.filter((i) => !INGORE_ATTRIBUTES.includes(i.key?.name?.toLowerCase()) && i.value == null);
94
+ if (!valueless.length)
95
+ return;
96
+ const input = valueless.map((i) => i.key.name).join(" ").trim();
97
+ const sorted = sortClasses(input);
98
+ if (sorted !== input) {
99
+ context.report({
100
+ node,
101
+ messageId: "invalid-order",
102
+ fix(fixer) {
103
+ const codeFull = context.getSourceCode();
104
+ const offset = node.range[0];
105
+ const code = codeFull.getText().slice(node.range[0], node.range[1]);
106
+ const s = new MagicString__default(code);
107
+ const sortedNodes = valueless.map((i) => [i.range[0] - offset, i.range[1] - offset]).sort((a, b) => a[0] - b[0]);
108
+ for (let [start, end] of sortedNodes.slice(1)) {
109
+ if (code[start - 1] === " ")
110
+ start--;
111
+ if (code[end] === " ")
112
+ end++;
113
+ s.remove(start, end);
114
+ }
115
+ s.overwrite(sortedNodes[0][0], sortedNodes[0][1], ` ${sorted.trim()} `);
116
+ return fixer.replaceText(node, s.toString());
117
+ }
118
+ });
119
+ }
120
+ }
121
+ };
122
+ if (context.parserServices == null || context.parserServices.defineTemplateBodyVisitor == null) {
123
+ return scriptVisitor;
124
+ } else {
125
+ return context.parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor);
126
+ }
127
+ }
128
+ });
129
+
130
+ const configsRecommended = {
131
+ plugins: ["@unocss"],
132
+ rules: {
133
+ "@unocss/order": "warn",
134
+ "@unocss/order-attributify": "warn"
135
+ }
136
+ };
137
+
138
+ const index = {
139
+ rules: {
140
+ order,
141
+ "order-attributify": orderAttributify
142
+ },
143
+ configs: {
144
+ recommended: configsRecommended
145
+ }
146
+ };
147
+
148
+ module.exports = index;
@@ -0,0 +1,19 @@
1
+ import * as _typescript_eslint_utils_dist_ts_eslint_Rule from '@typescript-eslint/utils/dist/ts-eslint/Rule';
2
+
3
+ declare const _default: {
4
+ rules: {
5
+ order: _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<"invalid-order", never[], any>;
6
+ 'order-attributify': _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<"invalid-order", never[], any>;
7
+ };
8
+ configs: {
9
+ recommended: {
10
+ plugins: string[];
11
+ rules: {
12
+ '@unocss/order': string;
13
+ '@unocss/order-attributify': string;
14
+ };
15
+ };
16
+ };
17
+ };
18
+
19
+ export { _default as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,142 @@
1
+ import { join } from 'path';
2
+ import { ESLintUtils } from '@typescript-eslint/utils';
3
+ import { createSyncFn } from 'synckit';
4
+ import { distDir } from './dirs.mjs';
5
+ import MagicString from 'magic-string';
6
+ import 'url';
7
+
8
+ const CLASS_FIELDS = ["class", "classname"];
9
+
10
+ const sortClasses$1 = createSyncFn(join(distDir, "worker-sort.cjs"));
11
+ const order = ESLintUtils.RuleCreator((name) => name)({
12
+ name: "order",
13
+ meta: {
14
+ type: "layout",
15
+ fixable: "code",
16
+ docs: {
17
+ description: "Order of UnoCSS utilities in class attribute",
18
+ recommended: "warn"
19
+ },
20
+ messages: {
21
+ "invalid-order": "UnoCSS utilities are not ordered"
22
+ },
23
+ schema: []
24
+ },
25
+ defaultOptions: [],
26
+ create(context) {
27
+ function checkLiteral(node) {
28
+ if (typeof node.value !== "string")
29
+ return;
30
+ const input = node.value;
31
+ const sorted = sortClasses$1(input);
32
+ if (sorted !== input) {
33
+ context.report({
34
+ node,
35
+ messageId: "invalid-order",
36
+ fix(fixer) {
37
+ return fixer.replaceText(node, `"${sorted.trim()}"`);
38
+ }
39
+ });
40
+ }
41
+ }
42
+ const scriptVisitor = {
43
+ JSXAttribute(node) {
44
+ if (typeof node.name.name === "string" && CLASS_FIELDS.includes(node.name.name.toLowerCase()) && node.value) {
45
+ if (node.value.type === "Literal")
46
+ checkLiteral(node.value);
47
+ }
48
+ }
49
+ };
50
+ const templateBodyVisitor = {
51
+ VAttribute(node) {
52
+ if (node.key.name === "class") {
53
+ if (node.value.type === "VLiteral")
54
+ checkLiteral(node.value);
55
+ }
56
+ }
57
+ };
58
+ if (context.parserServices == null || context.parserServices.defineTemplateBodyVisitor == null) {
59
+ return scriptVisitor;
60
+ } else {
61
+ return context.parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor);
62
+ }
63
+ }
64
+ });
65
+
66
+ const sortClasses = createSyncFn(join(distDir, "worker-sort.cjs"));
67
+ const INGORE_ATTRIBUTES = ["style", "class", "classname", "value"];
68
+ const orderAttributify = ESLintUtils.RuleCreator((name) => name)({
69
+ name: "order-attributify",
70
+ meta: {
71
+ type: "layout",
72
+ fixable: "code",
73
+ docs: {
74
+ description: "Order of UnoCSS attributes",
75
+ recommended: false
76
+ },
77
+ messages: {
78
+ "invalid-order": "UnoCSS attributes are not ordered"
79
+ },
80
+ schema: []
81
+ },
82
+ defaultOptions: [],
83
+ create(context) {
84
+ const scriptVisitor = {};
85
+ const templateBodyVisitor = {
86
+ VStartTag(node) {
87
+ const valueless = node.attributes.filter((i) => !INGORE_ATTRIBUTES.includes(i.key?.name?.toLowerCase()) && i.value == null);
88
+ if (!valueless.length)
89
+ return;
90
+ const input = valueless.map((i) => i.key.name).join(" ").trim();
91
+ const sorted = sortClasses(input);
92
+ if (sorted !== input) {
93
+ context.report({
94
+ node,
95
+ messageId: "invalid-order",
96
+ fix(fixer) {
97
+ const codeFull = context.getSourceCode();
98
+ const offset = node.range[0];
99
+ const code = codeFull.getText().slice(node.range[0], node.range[1]);
100
+ const s = new MagicString(code);
101
+ const sortedNodes = valueless.map((i) => [i.range[0] - offset, i.range[1] - offset]).sort((a, b) => a[0] - b[0]);
102
+ for (let [start, end] of sortedNodes.slice(1)) {
103
+ if (code[start - 1] === " ")
104
+ start--;
105
+ if (code[end] === " ")
106
+ end++;
107
+ s.remove(start, end);
108
+ }
109
+ s.overwrite(sortedNodes[0][0], sortedNodes[0][1], ` ${sorted.trim()} `);
110
+ return fixer.replaceText(node, s.toString());
111
+ }
112
+ });
113
+ }
114
+ }
115
+ };
116
+ if (context.parserServices == null || context.parserServices.defineTemplateBodyVisitor == null) {
117
+ return scriptVisitor;
118
+ } else {
119
+ return context.parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor);
120
+ }
121
+ }
122
+ });
123
+
124
+ const configsRecommended = {
125
+ plugins: ["@unocss"],
126
+ rules: {
127
+ "@unocss/order": "warn",
128
+ "@unocss/order-attributify": "warn"
129
+ }
130
+ };
131
+
132
+ const index = {
133
+ rules: {
134
+ order,
135
+ "order-attributify": orderAttributify
136
+ },
137
+ configs: {
138
+ recommended: configsRecommended
139
+ }
140
+ };
141
+
142
+ export { index as default };
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const config = require('@unocss/config');
4
+ const core = require('@unocss/core');
5
+ const synckit = require('synckit');
6
+
7
+ async function sortRules(rules, uno) {
8
+ const unknown = [];
9
+ if (!uno.config.details)
10
+ uno.config.details = true;
11
+ const expandedResult = core.parseVariantGroup(rules);
12
+ rules = expandedResult.expanded;
13
+ const result = await Promise.all(rules.split(/\s+/g).map(async (i) => {
14
+ const token = await uno.parseToken(i);
15
+ if (token == null) {
16
+ unknown.push(i);
17
+ return void 0;
18
+ }
19
+ const variantRank = (token[0][5]?.variantHandlers?.length || 0) * 1e3;
20
+ const order = token[0][0] + variantRank;
21
+ return [order, i];
22
+ }));
23
+ let sorted = result.filter(core.notNull).sort((a, b) => {
24
+ let result2 = a[0] - b[0];
25
+ if (result2 === 0)
26
+ result2 = a[1].localeCompare(b[1]);
27
+ return result2;
28
+ }).map((i) => i[1]).join(" ");
29
+ if (expandedResult?.prefixes.length)
30
+ sorted = core.collapseVariantGroup(sorted, expandedResult.prefixes);
31
+ return [...unknown, sorted].join(" ").trim();
32
+ }
33
+
34
+ async function getGenerator() {
35
+ const { config: config$1 } = await config.loadConfig();
36
+ return core.createGenerator(config$1);
37
+ }
38
+ let promise;
39
+ synckit.runAsWorker(async (classes) => {
40
+ promise = promise || getGenerator();
41
+ const uno = await promise;
42
+ return await sortRules(classes, uno);
43
+ });
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,41 @@
1
+ import { loadConfig } from '@unocss/config';
2
+ import { parseVariantGroup, notNull, collapseVariantGroup, createGenerator } from '@unocss/core';
3
+ import { runAsWorker } from 'synckit';
4
+
5
+ async function sortRules(rules, uno) {
6
+ const unknown = [];
7
+ if (!uno.config.details)
8
+ uno.config.details = true;
9
+ const expandedResult = parseVariantGroup(rules);
10
+ rules = expandedResult.expanded;
11
+ const result = await Promise.all(rules.split(/\s+/g).map(async (i) => {
12
+ const token = await uno.parseToken(i);
13
+ if (token == null) {
14
+ unknown.push(i);
15
+ return void 0;
16
+ }
17
+ const variantRank = (token[0][5]?.variantHandlers?.length || 0) * 1e3;
18
+ const order = token[0][0] + variantRank;
19
+ return [order, i];
20
+ }));
21
+ let sorted = result.filter(notNull).sort((a, b) => {
22
+ let result2 = a[0] - b[0];
23
+ if (result2 === 0)
24
+ result2 = a[1].localeCompare(b[1]);
25
+ return result2;
26
+ }).map((i) => i[1]).join(" ");
27
+ if (expandedResult?.prefixes.length)
28
+ sorted = collapseVariantGroup(sorted, expandedResult.prefixes);
29
+ return [...unknown, sorted].join(" ").trim();
30
+ }
31
+
32
+ async function getGenerator() {
33
+ const { config } = await loadConfig();
34
+ return createGenerator(config);
35
+ }
36
+ let promise;
37
+ runAsWorker(async (classes) => {
38
+ promise = promise || getGenerator();
39
+ const uno = await promise;
40
+ return await sortRules(classes, uno);
41
+ });
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@unocss/eslint-plugin",
3
+ "version": "0.49.3",
4
+ "description": "ESLint plugin for UnoCSS",
5
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
+ "license": "MIT",
7
+ "funding": "https://github.com/sponsors/antfu",
8
+ "homepage": "https://github.com/unocss/unocss/tree/main/packages/esling-plugin#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/unocss/unocss.git",
12
+ "directory": "packages/esling-plugin"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/unocss/unocss/issues"
16
+ },
17
+ "keywords": [
18
+ "eslint-plugin",
19
+ "eslint"
20
+ ],
21
+ "sideEffects": false,
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "require": "./dist/index.cjs",
26
+ "import": "./dist/index.mjs"
27
+ }
28
+ },
29
+ "main": "dist/index.cjs",
30
+ "module": "dist/index.mjs",
31
+ "types": "dist/index.d.ts",
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "engines": {
36
+ "node": ">=14"
37
+ },
38
+ "dependencies": {
39
+ "@typescript-eslint/utils": "^5.50.0",
40
+ "@unocss/config": "^0.49.2",
41
+ "@unocss/core": "0.49.3",
42
+ "magic-string": "^0.27.0",
43
+ "synckit": "^0.8.5"
44
+ },
45
+ "devDependencies": {
46
+ "@unocss/eslint-plugin": "0.49.3"
47
+ },
48
+ "scripts": {
49
+ "build": "unbuild",
50
+ "lint": "nr build && cd ./fixtures && eslint ./src"
51
+ }
52
+ }