@byscripts/eslint-plugin 0.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/dist/index.mjs +66 -0
- package/package.json +27 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//#region src/block-attribute-order.ts
|
|
2
|
+
const DEFAULT_ORDER = {
|
|
3
|
+
script: ["lang", "setup"],
|
|
4
|
+
style: ["lang", "scoped"]
|
|
5
|
+
};
|
|
6
|
+
const blockAttributeOrder = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: "layout",
|
|
9
|
+
fixable: "code",
|
|
10
|
+
schema: [{
|
|
11
|
+
type: "object",
|
|
12
|
+
additionalProperties: {
|
|
13
|
+
type: "array",
|
|
14
|
+
items: { type: "string" }
|
|
15
|
+
}
|
|
16
|
+
}],
|
|
17
|
+
messages: { wrongOrder: "Attributes on <{{tag}}> should follow order: {{expected}}." }
|
|
18
|
+
},
|
|
19
|
+
create(context) {
|
|
20
|
+
if (!context.filename.endsWith(".vue")) return {};
|
|
21
|
+
const attributeOrder = {
|
|
22
|
+
...DEFAULT_ORDER,
|
|
23
|
+
...context.options[0]
|
|
24
|
+
};
|
|
25
|
+
const { parserServices } = context.sourceCode;
|
|
26
|
+
const documentFragment = parserServices.getDocumentFragment?.();
|
|
27
|
+
if (!documentFragment) return {};
|
|
28
|
+
return { Program() {
|
|
29
|
+
for (const child of documentFragment.children) {
|
|
30
|
+
if (child.type !== "VElement") continue;
|
|
31
|
+
const expectedOrder = attributeOrder[child.name];
|
|
32
|
+
if (!expectedOrder) continue;
|
|
33
|
+
const attributes = child.startTag.attributes.filter((a) => a.type === "VAttribute" && !a.directive);
|
|
34
|
+
const relevantNames = attributes.map((a) => a.key.name).filter((n) => expectedOrder.includes(n));
|
|
35
|
+
const expectedNames = expectedOrder.filter((n) => relevantNames.includes(n));
|
|
36
|
+
if (relevantNames.every((name, i) => name === expectedNames[i])) continue;
|
|
37
|
+
const sourceCode = context.sourceCode;
|
|
38
|
+
context.report({
|
|
39
|
+
loc: child.startTag.loc,
|
|
40
|
+
messageId: "wrongOrder",
|
|
41
|
+
data: {
|
|
42
|
+
tag: child.name,
|
|
43
|
+
expected: expectedNames.join(", ")
|
|
44
|
+
},
|
|
45
|
+
fix(fixer) {
|
|
46
|
+
const newText = [...attributes].sort((attributeA, attributeB) => {
|
|
47
|
+
const attributeAIndex = expectedOrder.indexOf(attributeA.key.name);
|
|
48
|
+
const attributeBIndex = expectedOrder.indexOf(attributeB.key.name);
|
|
49
|
+
return (attributeAIndex === -1 ? 999 : attributeAIndex) - (attributeBIndex === -1 ? 999 : attributeBIndex);
|
|
50
|
+
}).map((a) => sourceCode.getText(a)).join(" ");
|
|
51
|
+
const firstAttribute = attributes[0];
|
|
52
|
+
const lastAttribute = attributes[attributes.length - 1];
|
|
53
|
+
return fixer.replaceTextRange([firstAttribute.range[0], lastAttribute.range[1]], newText);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
} };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/index.ts
|
|
63
|
+
var src_default = { blockAttributeOrder };
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
export { src_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@byscripts/eslint-plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsdown"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@eslint/js": "^10.0.1",
|
|
16
|
+
"eslint-plugin-vue": "^10.8.0",
|
|
17
|
+
"typescript-eslint": "^8.56.1",
|
|
18
|
+
"vue-eslint-parser": "^10.4.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@tsconfig/recommended": "^1.0.13",
|
|
22
|
+
"tsdown": "^0.20.3"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"eslint": "^10.0.1"
|
|
26
|
+
}
|
|
27
|
+
}
|