-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathhtml-quotes.js
63 lines (56 loc) · 1.76 KB
/
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
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Creates AST event handlers for html-quotes.
*
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
function create (context) {
const sourceCode = context.getSourceCode()
const double = context.options[0] !== 'single'
const quoteChar = double ? '"' : "'"
const quoteName = double ? 'double quotes' : 'single quotes'
return utils.defineTemplateBodyVisitor(context, {
'VAttribute[value!=null]' (node) {
const text = sourceCode.getText(node.value)
const firstChar = text[0]
if (firstChar !== quoteChar) {
context.report({
node: node.value,
loc: node.value.loc,
message: 'Expected to be enclosed by {{kind}}.',
data: { kind: quoteName }
})
}
}
})
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
create,
meta: {
docs: {
description: 'enforce quotes style of HTML attributes.',
category: 'Stylistic Issues',
recommended: false
},
fixable: false,
schema: [
{ enum: ['double', 'single'] }
]
}
}