Skip to content

Enable more eslint-plugin-unicorn rules #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions eslint-internal-rules/no-invalid-meta-docs-categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ function checkMetaValidity(context, exportsNode) {
) {
// fixes.push(fixer.insertTextBefore(category.value, '['), fixer.insertTextAfter(category.value, ']'))

// for vue3 migration
if (category.value.value !== 'base') {
if (category.value.value === 'base') {
fixes.push(fixer.insertTextBefore(category.value, '['))
} else {
// for vue3 migration
fixes.push(
fixer.insertTextBefore(
category.value,
`['vue3-${category.value.value}', `
)
)
} else {
fixes.push(fixer.insertTextBefore(category.value, '['))
}
fixes.push(fixer.insertTextAfter(category.value, ']'))
}
Expand Down
3 changes: 0 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,14 @@ module.exports = [
'unicorn/filename-case': 'off',
'unicorn/no-null': 'off',
'unicorn/no-array-callback-reference': 'off', // doesn't work well with TypeScript's custom type guards
'unicorn/no-negated-condition': 'off', // remove when there are few pull requests (ref: #2146)
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-optional-catch-binding': 'off', // not supported by current ESLint parser version
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/prefer-at': 'off', // turn off to prevent make breaking changes (ref: #2146)
'unicorn/prefer-node-protocol': 'off', // turn off to prevent make breaking changes (ref: #2146)
'unicorn/prefer-string-replace-all': 'off', // turn off to prevent make breaking changes (ref: #2146)
'unicorn/prefer-ternary': 'off', // remove when there are few pull requests (ref: #2146)
'unicorn/prefer-top-level-await': 'off', // turn off to prevent make breaking changes (ref: #2146)
'unicorn/switch-case-braces': 'off', // remove when there are few pull requests (ref: #2146)

'internal/require-eslint-community': ['error']
}
Expand Down
30 changes: 20 additions & 10 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,50 @@ module.exports = {
const directiveType = message.messageId
const data = message.message.split(' ')
switch (directiveType) {
case 'disableBlock':
case 'disableBlock': {
state.block.disableAllKeys.add(data[1])
break
case 'disableLine':
}
case 'disableLine': {
state.line.disableAllKeys.add(data[1])
break
case 'enableBlock':
}
case 'enableBlock': {
state.block.disableAllKeys.clear()
break
case 'enableLine':
}
case 'enableLine': {
state.line.disableAllKeys.clear()
break
case 'disableBlockRule':
}
case 'disableBlockRule': {
addDisableRule(state.block.disableRuleKeys, data[1], data[2])
break
case 'disableLineRule':
}
case 'disableLineRule': {
addDisableRule(state.line.disableRuleKeys, data[1], data[2])
break
case 'enableBlockRule':
}
case 'enableBlockRule': {
state.block.disableRuleKeys.delete(data[1])
break
case 'enableLineRule':
}
case 'enableLineRule': {
state.line.disableRuleKeys.delete(data[1])
break
case 'clear':
}
case 'clear': {
state.block.disableAllKeys.clear()
state.block.disableRuleKeys.clear()
state.line.disableAllKeys.clear()
state.line.disableRuleKeys.clear()
break
default:
}
default: {
// unused eslint-disable comments report
unusedDisableDirectiveReports.set(messageToKey(message), message)
break
}
}
return false
} else {
Expand Down
48 changes: 31 additions & 17 deletions lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,39 @@ function getAttributeType(attribute) {
if (!isVBind(attribute)) {
const name = attribute.key.name.name
switch (name) {
case 'for':
case 'for': {
return ATTRS.LIST_RENDERING
}
case 'if':
case 'else-if':
case 'else':
case 'show':
case 'cloak':
case 'cloak': {
return ATTRS.CONDITIONALS
}
case 'pre':
case 'once':
case 'once': {
return ATTRS.RENDER_MODIFIERS
case 'model':
}
case 'model': {
return ATTRS.TWO_WAY_BINDING
case 'on':
}
case 'on': {
return ATTRS.EVENTS
}
case 'html':
case 'text':
case 'text': {
return ATTRS.CONTENT
case 'slot':
}
case 'slot': {
return ATTRS.SLOT
case 'is':
}
case 'is': {
return ATTRS.DEFINITION
default:
}
default: {
return ATTRS.OTHER_DIRECTIVES
}
}
}
propName =
Expand All @@ -154,24 +163,29 @@ function getAttributeType(attribute) {
propName = attribute.key.name
}
switch (propName) {
case 'is':
case 'is': {
return ATTRS.DEFINITION
case 'id':
}
case 'id': {
return ATTRS.GLOBAL
}
case 'ref':
case 'key':
case 'key': {
return ATTRS.UNIQUE
}
case 'slot':
case 'slot-scope':
case 'slot-scope': {
return ATTRS.SLOT
default:
}
default: {
if (isVBind(attribute)) {
return ATTRS.ATTR_DYNAMIC
}
if (isVShorthandBoolean(attribute)) {
return ATTRS.ATTR_SHORTHAND_BOOL
}
return ATTRS.ATTR_STATIC
}
}
}

Expand All @@ -182,9 +196,9 @@ function getAttributeType(attribute) {
*/
function getPosition(attribute, attributePosition) {
const attributeType = getAttributeType(attribute)
return attributePosition[attributeType] != null
? attributePosition[attributeType]
: null
return attributePosition[attributeType] == null
? null
: attributePosition[attributeType]
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/block-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ const DEFAULT_LANGUAGES = {
function getAllowsLangPhrase(lang) {
const langs = [...lang].map((s) => `"${s}"`)
switch (langs.length) {
case 1:
case 1: {
return langs[0]
default:
}
default: {
return `${langs.slice(0, -1).join(', ')}, and ${langs[langs.length - 1]}`
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions lib/rules/block-tag-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ function getLinebreakCount(text) {
*/
function getPhrase(lineBreaks) {
switch (lineBreaks) {
case 1:
case 1: {
return '1 line break'
default:
}
default: {
return `${lineBreaks} line breaks`
}
}
}

Expand Down Expand Up @@ -322,17 +324,17 @@ module.exports = {
return (element) => {
const { name } = element
const elementsOptions = blocks[name]
if (!elementsOptions) {
verifyElement(element, options)
} else {
if (elementsOptions) {
normalizeOptionValue({
singleline: elementsOptions.singleline || options.singleline,
multiline: elementsOptions.multiline || options.multiline,
maxEmptyLines:
elementsOptions.maxEmptyLines != null
? elementsOptions.maxEmptyLines
: options.maxEmptyLines
elementsOptions.maxEmptyLines == null
? options.maxEmptyLines
: elementsOptions.maxEmptyLines
})(element)
} else {
verifyElement(element, options)
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/rules/comment-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ function parse(pattern, comment) {
* @returns {void}
*/
function enable(context, loc, group, rule) {
if (!rule) {
if (rule) {
context.report({
loc,
messageId: group === 'block' ? 'enableBlock' : 'enableLine'
messageId: group === 'block' ? 'enableBlockRule' : 'enableLineRule',
data: { rule }
})
} else {
context.report({
loc,
messageId: group === 'block' ? 'enableBlockRule' : 'enableLineRule',
data: { rule }
messageId: group === 'block' ? 'enableBlock' : 'enableLine'
})
}
}
Expand All @@ -94,17 +94,17 @@ function enable(context, loc, group, rule) {
* @returns {void}
*/
function disable(context, loc, group, rule, key) {
if (!rule) {
if (rule) {
context.report({
loc,
messageId: group === 'block' ? 'disableBlock' : 'disableLine',
data: { key }
messageId: group === 'block' ? 'disableBlockRule' : 'disableLineRule',
data: { rule, key }
})
} else {
context.report({
loc,
messageId: group === 'block' ? 'disableBlockRule' : 'disableLineRule',
data: { rule, key }
messageId: group === 'block' ? 'disableBlock' : 'disableLine',
data: { key }
})
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/define-emits-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,25 @@ module.exports = {
return utils.defineScriptSetupVisitor(context, {
onDefineEmitsEnter(node) {
switch (defineType) {
case 'type-based':
case 'type-based': {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
}
break
}

case 'runtime':
case 'runtime': {
if (node.typeParameters && node.typeParameters.params.length > 0) {
context.report({
node,
messageId: 'hasTypeArg'
})
}
break
}
}
}
})
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,25 @@ module.exports = {
return utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node) {
switch (defineType) {
case 'type-based':
case 'type-based': {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
}
break
}

case 'runtime':
case 'runtime': {
if (node.typeParameters && node.typeParameters.params.length > 0) {
context.report({
node,
messageId: 'hasTypeArg'
})
}
break
}
}
}
})
Expand Down
9 changes: 6 additions & 3 deletions lib/rules/html-closing-bracket-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ const utils = require('../utils')
*/
function getPhrase(lineBreaks) {
switch (lineBreaks) {
case 0:
case 0: {
return 'no line breaks'
case 1:
}
case 1: {
return '1 line break'
default:
}
default: {
return `${lineBreaks} line breaks`
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getElementType(node) {
*/
function isEmpty(node, sourceCode) {
const start = node.startTag.range[1]
const end = node.endTag != null ? node.endTag.range[0] : node.range[1]
const end = node.endTag == null ? node.range[1] : node.endTag.range[0]

return sourceCode.text.slice(start, end).trim() === ''
}
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/multiline-html-element-content-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ function parseOptions(options) {
*/
function getPhrase(lineBreaks) {
switch (lineBreaks) {
case 0:
case 0: {
return 'no'
default:
}
default: {
return `${lineBreaks}`
}
}
}
/**
Expand Down
Loading