Skip to content

Commit ba29114

Browse files
authored
Meta: Install eslint-plugin-unicorn (#1857)
* Install `eslint-plugin-unicorn` * Fix new lint issues
1 parent 8900a96 commit ba29114

File tree

124 files changed

+2198
-2151
lines changed

Some content is hidden

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

124 files changed

+2198
-2151
lines changed

.eslintrc.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ module.exports = {
1414
'plugin:eslint-plugin/recommended',
1515
'prettier',
1616
'plugin:node-dependencies/recommended',
17-
'plugin:jsonc/recommended-with-jsonc'
17+
'plugin:jsonc/recommended-with-jsonc',
18+
'plugin:unicorn/recommended'
1819
],
19-
plugins: ['eslint-plugin', 'prettier'],
20+
plugins: ['eslint-plugin', 'prettier', 'unicorn'],
2021
rules: {
2122
'accessor-pairs': 2,
2223
camelcase: [2, { properties: 'never' }],
@@ -117,7 +118,19 @@ module.exports = {
117118
'prefer-arrow-callback': 'error',
118119
'prefer-spread': 'error',
119120

120-
'dot-notation': 'error'
121+
'dot-notation': 'error',
122+
123+
'unicorn/consistent-function-scoping': [
124+
'error',
125+
{ checkArrowFunctions: false }
126+
],
127+
'unicorn/filename-case': 'off',
128+
'unicorn/no-null': 'off',
129+
'unicorn/no-array-callback-reference': 'off', // doesn't work well with TypeScript's custom type guards
130+
'unicorn/no-useless-undefined': 'off',
131+
'unicorn/prefer-optional-catch-binding': 'off', // not supported by current ESLint parser version
132+
'unicorn/prefer-module': 'off',
133+
'unicorn/prevent-abbreviations': 'off'
121134
},
122135
overrides: [
123136
{

docs/.vuepress/enhanceApp.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ export default (
88
// siteData, // site metadata
99
}
1010
) => {
11-
if (typeof window !== 'undefined') {
12-
if (typeof window.process === 'undefined') {
13-
window.process = new Proxy(
14-
{
15-
env: {},
16-
cwd: () => undefined
17-
},
18-
{
19-
get(target, name) {
20-
// For debug
21-
// console.log(name)
22-
return target[name]
23-
}
11+
if (typeof window !== 'undefined' && typeof window.process === 'undefined') {
12+
window.process = new Proxy(
13+
{
14+
env: {},
15+
cwd: () => undefined
16+
},
17+
{
18+
get(target, name) {
19+
// For debug
20+
// console.log(name)
21+
return target[name]
2422
}
25-
)
26-
}
23+
}
24+
)
2725
}
2826
}

eslint-internal-rules/no-invalid-meta-docs-categories.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
/**
1313
* Gets the property of the Object node passed in that has the name specified.
1414
*
15-
* @param {string} property Name of the property to return.
15+
* @param {string} propertyName Name of the property to return.
1616
* @param {ASTNode} node The ObjectExpression node.
1717
* @returns {ASTNode} The Property node or null if not found.
1818
*/
19-
function getPropertyFromObject(property, node) {
19+
function getPropertyFromObject(propertyName, node) {
2020
if (node && node.type === 'ObjectExpression') {
2121
const properties = node.properties
2222

23-
for (let i = 0; i < properties.length; i++) {
24-
if (properties[i].key.name === property) {
25-
return properties[i]
23+
for (const property of properties) {
24+
if (property.key.name === propertyName) {
25+
return property
2626
}
2727
}
2828
}

eslint-internal-rules/no-invalid-meta.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
/**
1313
* Gets the property of the Object node passed in that has the name specified.
1414
*
15-
* @param {string} property Name of the property to return.
15+
* @param {string} propertyName Name of the property to return.
1616
* @param {ASTNode} node The ObjectExpression node.
1717
* @returns {ASTNode} The Property node or null if not found.
1818
*/
19-
function getPropertyFromObject(property, node) {
19+
function getPropertyFromObject(propertyName, node) {
2020
if (node && node.type === 'ObjectExpression') {
2121
const properties = node.properties
2222

23-
for (let i = 0; i < properties.length; i++) {
24-
if (properties[i].key.name === property) {
25-
return properties[i]
23+
for (const property of properties) {
24+
if (property.key.name === propertyName) {
25+
return property
2626
}
2727
}
2828
}

lib/processor.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ module.exports = {
8484
return false
8585
} else {
8686
const disableDirectiveKeys = []
87-
if (state.block.disableAllKeys.size) {
87+
if (state.block.disableAllKeys.size > 0) {
8888
disableDirectiveKeys.push(...state.block.disableAllKeys)
8989
}
90-
if (state.line.disableAllKeys.size) {
90+
if (state.line.disableAllKeys.size > 0) {
9191
disableDirectiveKeys.push(...state.line.disableAllKeys)
9292
}
9393
if (message.ruleId) {
@@ -101,7 +101,7 @@ module.exports = {
101101
}
102102
}
103103

104-
if (disableDirectiveKeys.length) {
104+
if (disableDirectiveKeys.length > 0) {
105105
// Store used eslint-disable comment key
106106
usedDisableDirectiveKeys.push(...disableDirectiveKeys)
107107
return false
@@ -111,7 +111,7 @@ module.exports = {
111111
}
112112
})
113113

114-
if (unusedDisableDirectiveReports.size) {
114+
if (unusedDisableDirectiveReports.size > 0) {
115115
for (const key of usedDisableDirectiveKeys) {
116116
// Remove used eslint-disable comments
117117
unusedDisableDirectiveReports.delete(key)

lib/rules/attribute-hyphenation.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ const svgAttributes = require('../utils/svg-attributes-weird-case.json')
1212
// Rule Definition
1313
// ------------------------------------------------------------------------------
1414

15+
/**
16+
* @param {VDirective | VAttribute} node
17+
* @returns {string | null}
18+
*/
19+
function getAttributeName(node) {
20+
if (!node.directive) {
21+
return node.key.rawName
22+
}
23+
24+
if (
25+
node.key.name.name === 'bind' &&
26+
node.key.argument &&
27+
node.key.argument.type === 'VIdentifier'
28+
) {
29+
return node.key.argument.rawName
30+
}
31+
32+
return null
33+
}
34+
1535
module.exports = {
1636
meta: {
1737
type: 'suggestion',
@@ -52,12 +72,10 @@ module.exports = {
5272
const option = context.options[0]
5373
const optionsPayload = context.options[1]
5474
const useHyphenated = option !== 'never'
55-
let ignoredAttributes = ['data-', 'aria-', 'slot-scope'].concat(
56-
svgAttributes
57-
)
75+
const ignoredAttributes = ['data-', 'aria-', 'slot-scope', ...svgAttributes]
5876

5977
if (optionsPayload && optionsPayload.ignore) {
60-
ignoredAttributes = ignoredAttributes.concat(optionsPayload.ignore)
78+
ignoredAttributes.push(...optionsPayload.ignore)
6179
}
6280

6381
const caseConverter = casing.getExactConverter(
@@ -112,14 +130,8 @@ module.exports = {
112130
)
113131
return
114132

115-
const name = !node.directive
116-
? node.key.rawName
117-
: node.key.name.name === 'bind'
118-
? node.key.argument &&
119-
node.key.argument.type === 'VIdentifier' &&
120-
node.key.argument.rawName
121-
: /* otherwise */ false
122-
if (!name || isIgnoredAttribute(name)) return
133+
const name = getAttributeName(node)
134+
if (name === null || isIgnoredAttribute(name)) return
123135

124136
reportIssue(node, name)
125137
}

lib/rules/attributes-order.js

+41-38
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,31 @@ function getAttributeType(attribute) {
111111
if (attribute.directive) {
112112
if (!isVBind(attribute)) {
113113
const name = attribute.key.name.name
114-
if (name === 'for') {
115-
return ATTRS.LIST_RENDERING
116-
} else if (
117-
name === 'if' ||
118-
name === 'else-if' ||
119-
name === 'else' ||
120-
name === 'show' ||
121-
name === 'cloak'
122-
) {
123-
return ATTRS.CONDITIONALS
124-
} else if (name === 'pre' || name === 'once') {
125-
return ATTRS.RENDER_MODIFIERS
126-
} else if (name === 'model') {
127-
return ATTRS.TWO_WAY_BINDING
128-
} else if (name === 'on') {
129-
return ATTRS.EVENTS
130-
} else if (name === 'html' || name === 'text') {
131-
return ATTRS.CONTENT
132-
} else if (name === 'slot') {
133-
return ATTRS.SLOT
134-
} else if (name === 'is') {
135-
return ATTRS.DEFINITION
136-
} else {
137-
return ATTRS.OTHER_DIRECTIVES
114+
switch (name) {
115+
case 'for':
116+
return ATTRS.LIST_RENDERING
117+
case 'if':
118+
case 'else-if':
119+
case 'else':
120+
case 'show':
121+
case 'cloak':
122+
return ATTRS.CONDITIONALS
123+
case 'pre':
124+
case 'once':
125+
return ATTRS.RENDER_MODIFIERS
126+
case 'model':
127+
return ATTRS.TWO_WAY_BINDING
128+
case 'on':
129+
return ATTRS.EVENTS
130+
case 'html':
131+
case 'text':
132+
return ATTRS.CONTENT
133+
case 'slot':
134+
return ATTRS.SLOT
135+
case 'is':
136+
return ATTRS.DEFINITION
137+
default:
138+
return ATTRS.OTHER_DIRECTIVES
138139
}
139140
}
140141
propName =
@@ -144,16 +145,19 @@ function getAttributeType(attribute) {
144145
} else {
145146
propName = attribute.key.name
146147
}
147-
if (propName === 'is') {
148-
return ATTRS.DEFINITION
149-
} else if (propName === 'id') {
150-
return ATTRS.GLOBAL
151-
} else if (propName === 'ref' || propName === 'key') {
152-
return ATTRS.UNIQUE
153-
} else if (propName === 'slot' || propName === 'slot-scope') {
154-
return ATTRS.SLOT
155-
} else {
156-
return ATTRS.OTHER_ATTR
148+
switch (propName) {
149+
case 'is':
150+
return ATTRS.DEFINITION
151+
case 'id':
152+
return ATTRS.GLOBAL
153+
case 'ref':
154+
case 'key':
155+
return ATTRS.UNIQUE
156+
case 'slot':
157+
case 'slot-scope':
158+
return ATTRS.SLOT
159+
default:
160+
return ATTRS.OTHER_ATTR
157161
}
158162
}
159163

@@ -213,13 +217,13 @@ function create(context) {
213217

214218
/** @type { { [key: string]: number } } */
215219
const attributePosition = {}
216-
attributeOrder.forEach((item, i) => {
220+
for (const [i, item] of attributeOrder.entries()) {
217221
if (Array.isArray(item)) {
218222
for (const attr of item) {
219223
attributePosition[attr] = i
220224
}
221225
} else attributePosition[item] = i
222-
})
226+
}
223227

224228
/**
225229
* @param {VAttribute | VDirective} node
@@ -319,8 +323,7 @@ function create(context) {
319323
})
320324

321325
const results = []
322-
for (let index = 0; index < attributes.length; index++) {
323-
const attr = attributes[index]
326+
for (const [index, attr] of attributes.entries()) {
324327
const position = getPositionFromAttrIndex(index)
325328
if (position == null) {
326329
// The omitted order is skipped.

lib/rules/block-lang.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,21 @@ function getAllowsLangPhrase(lang) {
5252
/**
5353
* Normalizes a given option.
5454
* @param {string} blockName The block name.
55-
* @param { UserBlockOptions } option An option to parse.
55+
* @param {UserBlockOptions} option An option to parse.
5656
* @returns {BlockOptions} Normalized option.
5757
*/
5858
function normalizeOption(blockName, option) {
59-
const lang = new Set(
60-
Array.isArray(option.lang) ? option.lang : option.lang ? [option.lang] : []
61-
)
59+
/** @type {Set<string>} */
60+
let lang
61+
62+
if (Array.isArray(option.lang)) {
63+
lang = new Set(option.lang)
64+
} else if (typeof option.lang === 'string') {
65+
lang = new Set([option.lang])
66+
} else {
67+
lang = new Set()
68+
}
69+
6270
let hasDefault = false
6371
for (const def of DEFAULT_LANGUAGES[blockName] || []) {
6472
if (lang.has(def)) {
@@ -165,8 +173,7 @@ module.exports = {
165173
style: { allowNoLang: true }
166174
}
167175
)
168-
if (!Object.keys(options).length) {
169-
// empty
176+
if (Object.keys(options).length === 0) {
170177
return {}
171178
}
172179

@@ -198,11 +205,9 @@ module.exports = {
198205
if (!option.allowNoLang) {
199206
messageId = 'expected'
200207
} else if (option.lang.size === 0) {
201-
if ((DEFAULT_LANGUAGES[tag] || []).includes(lang.value.value)) {
202-
messageId = 'unexpectedDefault'
203-
} else {
204-
messageId = 'unexpected'
205-
}
208+
messageId = (DEFAULT_LANGUAGES[tag] || []).includes(lang.value.value)
209+
? 'unexpectedDefault'
210+
: 'unexpected'
206211
} else {
207212
messageId = 'useOrNot'
208213
}

lib/rules/block-tag-newline.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,8 @@ module.exports = {
257257
}
258258

259259
const option =
260-
options.multiline === options.singleline
261-
? options.singleline
262-
: /[\n\r\u2028\u2029]/u.test(text.trim())
260+
options.multiline !== options.singleline &&
261+
/[\n\r\u2028\u2029]/u.test(text.trim())
263262
? options.multiline
264263
: options.singleline
265264
if (option === 'ignore') {

0 commit comments

Comments
 (0)