-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-html-quotes.js
76 lines (65 loc) 路 1.9 KB
/
vue-html-quotes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module.exports = {
meta: {
type: "layout",
docs: {
description: "enforce quotes style of HTML attributes"
},
schema: [
{ enum: ["double", "single"] },
{
type: "object",
properties: {
avoidEscape: {
type: "boolean"
}
},
additionalProperties: false
}
],
messages: {
expected: "Expected to be enclosed by {{kind}}."
}
},
create(context) {
const sourceCode = context.getSourceCode()
const double = context.options[0] !== "single"
const avoidEscape =
context.options[1] && context.options[1].avoidEscape === true
const quoteChar = double ? '"' : "'"
const quoteName = double ? "double quotes" : "single quotes"
return context.parserServices.defineTemplateBodyVisitor(
{
"VAttribute[value!=null]"(node) {
const text = sourceCode.getText(node.value)
const firstChar = text[0]
if (firstChar !== quoteChar) {
const contentText = text.slice(1, -1);
const quoted = firstChar === "'" || firstChar === '"';
if (avoidEscape && quoted) {
if (contentText.includes(quoteChar)) {
return
}
}
// Attribute value is an object or an array
if (firstChar === "[" || firstChar === "{") {
// Matching quote in the object
if (contentText.includes(quoteChar)) {
return
}
// No quotes in the object or array
if (!contentText.includes("'") && !contentText.includes('"')) {
return
}
}
context.report({
node: node.value,
loc: node.value.loc,
messageId: "expected",
data: { kind: quoteName }
})
}
}
}
)
}
}