Skip to content

Commit 6589d4a

Browse files
committed
Chore: move create function into object literal
I found that `eslint-plugin-eslint-plugin` rules are using `create` function to detect ESLint rule files. If `create` is not a method (includes a variable reference), `eslint-plugin-eslint-plugin` rules don't work properly.
1 parent 085f1ca commit 6589d4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1494
-1789
lines changed

lib/rules/attribute-hyphenation.js

+43-45
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,6 @@ const casing = require('../utils/casing')
1111
// Rule Definition
1212
// ------------------------------------------------------------------------------
1313

14-
function create (context) {
15-
const sourceCode = context.getSourceCode()
16-
const options = context.options[0]
17-
const useHyphenated = options !== 'never'
18-
19-
const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase')
20-
21-
function reportIssue (node, name) {
22-
const text = sourceCode.getText(node.key)
23-
24-
context.report({
25-
node: node.key,
26-
loc: node.loc,
27-
message: useHyphenated ? "Attribute '{{text}}' must be hyphenated." : "Attribute '{{text}}' cann't be hyphenated.",
28-
data: {
29-
text
30-
},
31-
fix: fixer => fixer.replaceText(node.key, text.replace(name, caseConverter(name)))
32-
})
33-
}
34-
35-
function isIgnoredAttribute (value) {
36-
if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) {
37-
return true
38-
}
39-
return useHyphenated ? value.toLowerCase() === value : !/-/.test(value)
40-
}
41-
42-
// ----------------------------------------------------------------------
43-
// Public
44-
// ----------------------------------------------------------------------
45-
46-
return utils.defineTemplateBodyVisitor(context, {
47-
VAttribute (node) {
48-
if (!utils.isCustomComponent(node.parent.parent)) return
49-
50-
const name = !node.directive ? node.key.rawName : node.key.name === 'bind' ? node.key.raw.argument : false
51-
if (!name || isIgnoredAttribute(name)) return
52-
53-
reportIssue(node, name)
54-
}
55-
})
56-
}
57-
5814
module.exports = {
5915
meta: {
6016
docs: {
@@ -70,5 +26,47 @@ module.exports = {
7026
]
7127
},
7228

73-
create
29+
create (context) {
30+
const sourceCode = context.getSourceCode()
31+
const options = context.options[0]
32+
const useHyphenated = options !== 'never'
33+
34+
const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase')
35+
36+
function reportIssue (node, name) {
37+
const text = sourceCode.getText(node.key)
38+
39+
context.report({
40+
node: node.key,
41+
loc: node.loc,
42+
message: useHyphenated ? "Attribute '{{text}}' must be hyphenated." : "Attribute '{{text}}' cann't be hyphenated.",
43+
data: {
44+
text
45+
},
46+
fix: fixer => fixer.replaceText(node.key, text.replace(name, caseConverter(name)))
47+
})
48+
}
49+
50+
function isIgnoredAttribute (value) {
51+
if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) {
52+
return true
53+
}
54+
return useHyphenated ? value.toLowerCase() === value : !/-/.test(value)
55+
}
56+
57+
// ----------------------------------------------------------------------
58+
// Public
59+
// ----------------------------------------------------------------------
60+
61+
return utils.defineTemplateBodyVisitor(context, {
62+
VAttribute (node) {
63+
if (!utils.isCustomComponent(node.parent.parent)) return
64+
65+
const name = !node.directive ? node.key.rawName : node.key.name === 'bind' ? node.key.raw.argument : false
66+
if (!name || isIgnoredAttribute(name)) return
67+
68+
reportIssue(node, name)
69+
}
70+
})
71+
}
7472
}

lib/rules/comment-directive.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,6 @@ function processLine (context, comment) {
9999
}
100100
}
101101

102-
/**
103-
* The implementation of `vue/comment-directive` rule.
104-
* @param {Program} node The program node to parse.
105-
* @returns {Object} The visitor of this rule.
106-
*/
107-
function create (context) {
108-
return {
109-
Program (node) {
110-
const comments = (node.templateBody && node.templateBody.comments) || []
111-
for (const comment of comments) {
112-
processBlock(context, comment)
113-
processLine(context, comment)
114-
}
115-
}
116-
}
117-
}
118-
119102
// -----------------------------------------------------------------------------
120103
// Rule Definition
121104
// -----------------------------------------------------------------------------
@@ -129,5 +112,16 @@ module.exports = {
129112
},
130113
schema: []
131114
},
132-
create
115+
116+
create (context) {
117+
return {
118+
Program (node) {
119+
const comments = (node.templateBody && node.templateBody.comments) || []
120+
for (const comment of comments) {
121+
processBlock(context, comment)
122+
processLine(context, comment)
123+
}
124+
}
125+
}
126+
}
133127
}

lib/rules/html-closing-bracket-newline.js

+39-46
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,11 @@ function getPhrase (lineBreaks) {
2323
}
2424
}
2525

26-
/**
27-
* Creates AST event handlers for html-closing-bracket-newline.
28-
*
29-
* @param {RuleContext} context - The rule context.
30-
* @returns {object} AST event handlers.
31-
*/
32-
function create (context) {
33-
const options = context.options[0] || {}
34-
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
35-
36-
return utils.defineTemplateBodyVisitor(context, {
37-
'VStartTag, VEndTag' (node) {
38-
const closingBracketToken = template.getLastToken(node)
39-
if (closingBracketToken.type !== 'HTMLSelfClosingTagClose' && closingBracketToken.type !== 'HTMLTagClose') {
40-
return
41-
}
42-
43-
const prevToken = template.getTokenBefore(closingBracketToken)
44-
const type = (node.loc.start.line === prevToken.loc.end.line) ? 'singleline' : 'multiline'
45-
const expectedLineBreaks = (options[type] === 'always') ? 1 : 0
46-
const actualLineBreaks = (closingBracketToken.loc.start.line - prevToken.loc.end.line)
47-
48-
if (actualLineBreaks !== expectedLineBreaks) {
49-
context.report({
50-
node,
51-
loc: {
52-
start: prevToken.loc.end,
53-
end: closingBracketToken.loc.start
54-
},
55-
message: 'Expected {{expected}} before closing bracket, but {{actual}} found.',
56-
data: {
57-
expected: getPhrase(expectedLineBreaks),
58-
actual: getPhrase(actualLineBreaks)
59-
},
60-
fix (fixer) {
61-
const range = [prevToken.range[1], closingBracketToken.range[0]]
62-
const text = '\n'.repeat(expectedLineBreaks)
63-
return fixer.replaceTextRange(range, text)
64-
}
65-
})
66-
}
67-
}
68-
})
69-
}
70-
7126
// ------------------------------------------------------------------------------
7227
// Rule Definition
7328
// ------------------------------------------------------------------------------
7429

7530
module.exports = {
76-
create,
7731
meta: {
7832
docs: {
7933
description: "require or disallow a line break before tag's closing brackets",
@@ -89,5 +43,44 @@ module.exports = {
8943
},
9044
additionalProperties: false
9145
}]
46+
},
47+
48+
create (context) {
49+
const options = context.options[0] || {}
50+
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
51+
52+
return utils.defineTemplateBodyVisitor(context, {
53+
'VStartTag, VEndTag' (node) {
54+
const closingBracketToken = template.getLastToken(node)
55+
if (closingBracketToken.type !== 'HTMLSelfClosingTagClose' && closingBracketToken.type !== 'HTMLTagClose') {
56+
return
57+
}
58+
59+
const prevToken = template.getTokenBefore(closingBracketToken)
60+
const type = (node.loc.start.line === prevToken.loc.end.line) ? 'singleline' : 'multiline'
61+
const expectedLineBreaks = (options[type] === 'always') ? 1 : 0
62+
const actualLineBreaks = (closingBracketToken.loc.start.line - prevToken.loc.end.line)
63+
64+
if (actualLineBreaks !== expectedLineBreaks) {
65+
context.report({
66+
node,
67+
loc: {
68+
start: prevToken.loc.end,
69+
end: closingBracketToken.loc.start
70+
},
71+
message: 'Expected {{expected}} before closing bracket, but {{actual}} found.',
72+
data: {
73+
expected: getPhrase(expectedLineBreaks),
74+
actual: getPhrase(actualLineBreaks)
75+
},
76+
fix (fixer) {
77+
const range = [prevToken.range[1], closingBracketToken.range[0]]
78+
const text = '\n'.repeat(expectedLineBreaks)
79+
return fixer.replaceTextRange(range, text)
80+
}
81+
})
82+
}
83+
}
84+
})
9285
}
9386
}

lib/rules/html-end-tags.js

+31-42
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,11 @@
1111

1212
const utils = require('../utils')
1313

14-
// ------------------------------------------------------------------------------
15-
// Helpers
16-
// ------------------------------------------------------------------------------
17-
18-
/**
19-
* Creates AST event handlers for html-end-tags.
20-
*
21-
* @param {RuleContext} context - The rule context.
22-
* @returns {Object} AST event handlers.
23-
*/
24-
function create (context) {
25-
let hasInvalidEOF = false
26-
27-
return utils.defineTemplateBodyVisitor(context, {
28-
VElement (node) {
29-
if (hasInvalidEOF) {
30-
return
31-
}
32-
33-
const name = node.name
34-
const isVoid = utils.isHtmlVoidElementName(name)
35-
const isSelfClosing = node.startTag.selfClosing
36-
const hasEndTag = node.endTag != null
37-
38-
if (!isVoid && !hasEndTag && !isSelfClosing) {
39-
context.report({
40-
node: node.startTag,
41-
loc: node.startTag.loc,
42-
message: "'<{{name}}>' should have end tag.",
43-
data: { name },
44-
fix: (fixer) => fixer.insertTextAfter(node, `</${name}>`)
45-
})
46-
}
47-
}
48-
}, {
49-
Program (node) {
50-
hasInvalidEOF = utils.hasInvalidEOF(node)
51-
}
52-
})
53-
}
54-
5514
// ------------------------------------------------------------------------------
5615
// Rule Definition
5716
// ------------------------------------------------------------------------------
5817

5918
module.exports = {
60-
create,
6119
meta: {
6220
docs: {
6321
description: 'enforce end tag style',
@@ -66,5 +24,36 @@ module.exports = {
6624
},
6725
fixable: 'code',
6826
schema: []
27+
},
28+
29+
create (context) {
30+
let hasInvalidEOF = false
31+
32+
return utils.defineTemplateBodyVisitor(context, {
33+
VElement (node) {
34+
if (hasInvalidEOF) {
35+
return
36+
}
37+
38+
const name = node.name
39+
const isVoid = utils.isHtmlVoidElementName(name)
40+
const isSelfClosing = node.startTag.selfClosing
41+
const hasEndTag = node.endTag != null
42+
43+
if (!isVoid && !hasEndTag && !isSelfClosing) {
44+
context.report({
45+
node: node.startTag,
46+
loc: node.startTag.loc,
47+
message: "'<{{name}}>' should have end tag.",
48+
data: { name },
49+
fix: (fixer) => fixer.insertTextAfter(node, `</${name}>`)
50+
})
51+
}
52+
}
53+
}, {
54+
Program (node) {
55+
hasInvalidEOF = utils.hasInvalidEOF(node)
56+
}
57+
})
6958
}
7059
}

0 commit comments

Comments
 (0)