From 56bb69914b4127bd967514463b24d5c2675903a5 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 27 Jul 2023 15:45:14 +0200 Subject: [PATCH] Fix `unicorn/switch-case-braces` lint issues --- lib/processor.js | 30 ++++-- lib/rules/attributes-order.js | 42 ++++++--- lib/rules/block-lang.js | 6 +- lib/rules/block-tag-newline.js | 6 +- lib/rules/define-emits-declaration.js | 6 +- lib/rules/define-props-declaration.js | 6 +- lib/rules/html-closing-bracket-newline.js | 9 +- .../multiline-html-element-content-newline.js | 6 +- lib/rules/no-boolean-default.js | 6 +- lib/rules/require-prop-comment.js | 12 ++- lib/rules/v-on-handler-style.js | 18 ++-- lib/rules/v-slot-style.js | 12 ++- lib/utils/selector.js | 93 ++++++++++++------- lib/utils/ts-utils/ts-ast.js | 83 +++++++++++------ 14 files changed, 224 insertions(+), 111 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index f0a89fd8d..b7268fab9 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -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 { diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index 1f26b917d..44404f05c 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -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 = @@ -154,17 +163,21 @@ 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 } @@ -172,6 +185,7 @@ function getAttributeType(attribute) { return ATTRS.ATTR_SHORTHAND_BOOL } return ATTRS.ATTR_STATIC + } } } diff --git a/lib/rules/block-lang.js b/lib/rules/block-lang.js index 35898e938..55b85ce82 100644 --- a/lib/rules/block-lang.js +++ b/lib/rules/block-lang.js @@ -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]}` + } } } diff --git a/lib/rules/block-tag-newline.js b/lib/rules/block-tag-newline.js index 542098b16..bccecd834 100644 --- a/lib/rules/block-tag-newline.js +++ b/lib/rules/block-tag-newline.js @@ -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` + } } } diff --git a/lib/rules/define-emits-declaration.js b/lib/rules/define-emits-declaration.js index 3d64fed51..4daf4115c 100644 --- a/lib/rules/define-emits-declaration.js +++ b/lib/rules/define-emits-declaration.js @@ -36,7 +36,7 @@ module.exports = { return utils.defineScriptSetupVisitor(context, { onDefineEmitsEnter(node) { switch (defineType) { - case 'type-based': + case 'type-based': { if (node.arguments.length > 0) { context.report({ node, @@ -44,8 +44,9 @@ module.exports = { }) } break + } - case 'runtime': + case 'runtime': { if (node.typeParameters && node.typeParameters.params.length > 0) { context.report({ node, @@ -53,6 +54,7 @@ module.exports = { }) } break + } } } }) diff --git a/lib/rules/define-props-declaration.js b/lib/rules/define-props-declaration.js index 7a0be2259..c6fe4ffdc 100644 --- a/lib/rules/define-props-declaration.js +++ b/lib/rules/define-props-declaration.js @@ -36,7 +36,7 @@ module.exports = { return utils.defineScriptSetupVisitor(context, { onDefinePropsEnter(node) { switch (defineType) { - case 'type-based': + case 'type-based': { if (node.arguments.length > 0) { context.report({ node, @@ -44,8 +44,9 @@ module.exports = { }) } break + } - case 'runtime': + case 'runtime': { if (node.typeParameters && node.typeParameters.params.length > 0) { context.report({ node, @@ -53,6 +54,7 @@ module.exports = { }) } break + } } } }) diff --git a/lib/rules/html-closing-bracket-newline.js b/lib/rules/html-closing-bracket-newline.js index 0c787526a..e1a746e42 100644 --- a/lib/rules/html-closing-bracket-newline.js +++ b/lib/rules/html-closing-bracket-newline.js @@ -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` + } } } diff --git a/lib/rules/multiline-html-element-content-newline.js b/lib/rules/multiline-html-element-content-newline.js index a8ccc3676..dc4111864 100644 --- a/lib/rules/multiline-html-element-content-newline.js +++ b/lib/rules/multiline-html-element-content-newline.js @@ -34,10 +34,12 @@ function parseOptions(options) { */ function getPhrase(lineBreaks) { switch (lineBreaks) { - case 0: + case 0: { return 'no' - default: + } + default: { return `${lineBreaks}` + } } } /** diff --git a/lib/rules/no-boolean-default.js b/lib/rules/no-boolean-default.js index 5fd2560ad..66ca114bb 100644 --- a/lib/rules/no-boolean-default.js +++ b/lib/rules/no-boolean-default.js @@ -97,14 +97,15 @@ module.exports = { */ function verifyDefaultExpression(defaultNode) { switch (booleanType) { - case 'no-default': + case 'no-default': { context.report({ node: defaultNode, messageId: 'noBooleanDefault' }) break + } - case 'default-false': + case 'default-false': { if (defaultNode.type !== 'Literal' || defaultNode.value !== false) { context.report({ node: defaultNode, @@ -112,6 +113,7 @@ module.exports = { }) } break + } } } return utils.compositingVisitors( diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js index 845bba9df..41e1d57fb 100644 --- a/lib/rules/require-prop-comment.js +++ b/lib/rules/require-prop-comment.js @@ -76,18 +76,22 @@ module.exports = { let messageId switch (type) { - case 'block': + case 'block': { messageId = verifyBlock(lastPrecedingComment) break - case 'line': + } + case 'line': { messageId = verifyLine(lastPrecedingComment) break - case 'any': + } + case 'any': { messageId = verifyAny(lastPrecedingComment) break - default: + } + default: { messageId = verifyJSDoc(lastPrecedingComment) break + } } if (!messageId) { diff --git a/lib/rules/v-on-handler-style.js b/lib/rules/v-on-handler-style.js index aa1214e38..890965156 100644 --- a/lib/rules/v-on-handler-style.js +++ b/lib/rules/v-on-handler-style.js @@ -179,11 +179,13 @@ module.exports = { */ function verifyForInlineHandler(node, kind) { switch (kind) { - case 'method': + case 'method': { return verifyCanUseMethodHandlerForInlineHandler(node) - case 'inline-function': + } + case 'inline-function': { reportCanUseInlineFunctionForInlineHandler(node) return true + } } return false } @@ -196,7 +198,7 @@ module.exports = { function reportForMethodHandler(node, kind) { switch (kind) { case 'inline': - case 'inline-function': + case 'inline-function': { context.report({ node, messageId: @@ -205,6 +207,7 @@ module.exports = { : 'preferInlineFunctionOverMethod' }) return true + } } // This path is currently not taken. return false @@ -217,11 +220,13 @@ module.exports = { */ function verifyForInlineFunction(node, kind) { switch (kind) { - case 'method': + case 'method': { return verifyCanUseMethodHandlerForInlineFunction(node) - case 'inline': + } + case 'inline': { reportCanUseInlineHandlerForInlineFunction(node) return true + } } return false } @@ -529,8 +534,9 @@ module.exports = { } break } - default: + default: { return + } } }, ...(allows.includes('inline-function') diff --git a/lib/rules/v-slot-style.js b/lib/rules/v-slot-style.js index 1bb5f57f2..daa3d96da 100644 --- a/lib/rules/v-slot-style.js +++ b/lib/rules/v-slot-style.js @@ -141,14 +141,18 @@ module.exports = { fix(fixer) { switch (expected) { - case 'shorthand': + case 'shorthand': { return fixer.replaceTextRange(range, `#${argumentText}`) - case 'longform': + } + case 'longform': { return fixer.replaceTextRange(range, `v-slot:${argumentText}`) - case 'v-slot': + } + case 'v-slot': { return fixer.replaceTextRange(range, 'v-slot') - default: + } + default: { return null + } } } }) diff --git a/lib/utils/selector.js b/lib/utils/selector.js index 862f695fb..67960d59a 100644 --- a/lib/utils/selector.js +++ b/lib/utils/selector.js @@ -168,7 +168,7 @@ function selectorToVElementMatcher(selectorChildren) { */ function combination(left, combinator, right) { switch (combinator.trim()) { - case '': + case '': { // descendant return (element, subject) => { if (right(element, null)) { @@ -182,7 +182,8 @@ function combination(left, combinator, right) { } return false } - case '>': + } + case '>': { // child return (element, subject) => { if (right(element, null)) { @@ -193,7 +194,8 @@ function combination(left, combinator, right) { } return false } - case '+': + } + case '+': { // adjacent return (element, subject) => { if (right(element, null)) { @@ -204,7 +206,8 @@ function combination(left, combinator, right) { } return false } - case '~': + } + case '~': { // sibling return (element, subject) => { if (right(element, null)) { @@ -216,8 +219,10 @@ function combination(left, combinator, right) { } return false } - default: + } + default: { throw new SelectorError(`Unknown combinator: ${combinator}.`) + } } } @@ -228,26 +233,35 @@ function combination(left, combinator, right) { */ function nodeToVElementMatcher(selector) { switch (selector.type) { - case 'attribute': + case 'attribute': { return attributeNodeToVElementMatcher(selector) - case 'class': + } + case 'class': { return classNameNodeToVElementMatcher(selector) - case 'id': + } + case 'id': { return identifierNodeToVElementMatcher(selector) - case 'tag': + } + case 'tag': { return tagNodeToVElementMatcher(selector) - case 'universal': + } + case 'universal': { return universalNodeToVElementMatcher(selector) - case 'pseudo': + } + case 'pseudo': { return pseudoNodeToVElementMatcher(selector) - case 'nesting': + } + case 'nesting': { throw new SelectorError('Unsupported nesting selector.') - case 'string': + } + case 'string': { throw new SelectorError(`Unknown selector: ${selector.value}.`) - default: + } + default: { throw new SelectorError( `Unknown selector: ${/** @type {any}*/ (selector).value}.` ) + } } } @@ -264,30 +278,37 @@ function attributeNodeToVElementMatcher(selector) { const value = selector.value || '' switch (selector.operator) { - case '=': + case '=': { return buildVElementMatcher(value, (attr, val) => attr === val) - case '~=': + } + case '~=': { // words return buildVElementMatcher(value, (attr, val) => attr.split(/\s+/gu).includes(val) ) - case '|=': + } + case '|=': { // immediately followed by hyphen return buildVElementMatcher( value, (attr, val) => attr === val || attr.startsWith(`${val}-`) ) - case '^=': + } + case '^=': { // prefixed return buildVElementMatcher(value, (attr, val) => attr.startsWith(val)) - case '$=': + } + case '$=': { // suffixed return buildVElementMatcher(value, (attr, val) => attr.endsWith(val)) - case '*=': + } + case '*=': { // contains return buildVElementMatcher(value, (attr, val) => attr.includes(val)) - default: + } + default: { throw new SelectorError(`Unsupported operator: ${selector.operator}.`) + } } /** @@ -376,19 +397,22 @@ function pseudoNodeToVElementMatcher(selector) { return (element, subject) => !selectors(element, subject) } case ':is': - case ':where': + case ':where': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:is // https://developer.mozilla.org/en-US/docs/Web/CSS/:where return selectorsToVElementMatcher(selector.nodes) - case ':has': + } + case ':has': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:has return pseudoHasSelectorsToVElementMatcher(selector.nodes) - case ':empty': + } + case ':empty': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:empty return (element) => element.children.every( (child) => child.type === 'VText' && !child.value.trim() ) + } case ':nth-child': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child const nth = parseNth(selector) @@ -401,19 +425,22 @@ function pseudoNodeToVElementMatcher(selector) { nth(length - index - 1) ) } - case ':first-child': + case ':first-child': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child return buildPseudoNthVElementMatcher((index) => index === 0) - case ':last-child': + } + case ':last-child': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child return buildPseudoNthVElementMatcher( (index, length) => index === length - 1 ) - case ':only-child': + } + case ':only-child': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child return buildPseudoNthVElementMatcher( (index, length) => index === 0 && length === 1 ) + } case ':nth-of-type': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type const nth = parseNth(selector) @@ -426,21 +453,25 @@ function pseudoNodeToVElementMatcher(selector) { nth(length - index - 1) ) } - case ':first-of-type': + case ':first-of-type': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type return buildPseudoNthOfTypeVElementMatcher((index) => index === 0) - case ':last-of-type': + } + case ':last-of-type': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type return buildPseudoNthOfTypeVElementMatcher( (index, length) => index === length - 1 ) - case ':only-of-type': + } + case ':only-of-type': { // https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type return buildPseudoNthOfTypeVElementMatcher( (index, length) => index === 0 && length === 1 ) - default: + } + default: { throw new SelectorError(`Unsupported pseudo selector: ${pseudo}.`) + } } } diff --git a/lib/utils/ts-utils/ts-ast.js b/lib/utils/ts-utils/ts-ast.js index 8561bf565..d019b77cd 100644 --- a/lib/utils/ts-utils/ts-ast.js +++ b/lib/utils/ts-utils/ts-ast.js @@ -321,34 +321,45 @@ function flattenTypeNodes(context, node) { function inferRuntimeType(context, node, checked = new Set()) { switch (node.type) { case 'TSStringKeyword': - case 'TSTemplateLiteralType': + case 'TSTemplateLiteralType': { return ['String'] - case 'TSNumberKeyword': + } + case 'TSNumberKeyword': { return ['Number'] - case 'TSBooleanKeyword': + } + case 'TSBooleanKeyword': { return ['Boolean'] - case 'TSObjectKeyword': + } + case 'TSObjectKeyword': { return ['Object'] - case 'TSTypeLiteral': + } + case 'TSTypeLiteral': { return inferTypeLiteralType(node) - case 'TSFunctionType': + } + case 'TSFunctionType': { return ['Function'] + } case 'TSArrayType': - case 'TSTupleType': + case 'TSTupleType': { return ['Array'] - case 'TSSymbolKeyword': + } + case 'TSSymbolKeyword': { return ['Symbol'] + } - case 'TSLiteralType': + case 'TSLiteralType': { if (node.literal.type === 'Literal') { switch (typeof node.literal.value) { - case 'boolean': + case 'boolean': { return ['Boolean'] - case 'string': + } + case 'string': { return ['String'] + } case 'number': - case 'bigint': + case 'bigint': { return ['Number'] + } } if (node.literal.value instanceof RegExp) { return ['RegExp'] @@ -358,7 +369,8 @@ function inferRuntimeType(context, node, checked = new Set()) { context, /** @type {TypeNode} */ (node) ) - case 'TSTypeReference': + } + case 'TSTypeReference': { if (node.typeName.type === 'Identifier') { const variable = findVariable(context.getScope(), node.typeName.name) if (variable && variable.defs.length === 1) { @@ -391,8 +403,9 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Map': case 'WeakSet': case 'WeakMap': - case 'Date': + case 'Date': { return [name] + } } } @@ -403,17 +416,20 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Pick': case 'Omit': case 'Required': - case 'InstanceType': + case 'InstanceType': { return ['Object'] + } case 'Uppercase': case 'Lowercase': case 'Capitalize': - case 'Uncapitalize': + case 'Uncapitalize': { return ['String'] + } case 'Parameters': - case 'ConstructorParameters': + case 'ConstructorParameters': { return ['Array'] - case 'NonNullable': + } + case 'NonNullable': { if (node.typeParameters && node.typeParameters.params[0]) { return inferRuntimeType( context, @@ -422,7 +438,8 @@ function inferRuntimeType(context, node, checked = new Set()) { ).filter((t) => t !== 'null') } break - case 'Extract': + } + case 'Extract': { if (node.typeParameters && node.typeParameters.params[1]) { return inferRuntimeType( context, @@ -431,8 +448,9 @@ function inferRuntimeType(context, node, checked = new Set()) { ) } break + } case 'Exclude': - case 'OmitThisParameter': + case 'OmitThisParameter': { if (node.typeParameters && node.typeParameters.params[0]) { return inferRuntimeType( context, @@ -441,22 +459,26 @@ function inferRuntimeType(context, node, checked = new Set()) { ) } break + } } } return inferRuntimeTypeFromTypeNode( context, /** @type {TypeNode} */ (node) ) + } case 'TSUnionType': - case 'TSIntersectionType': + case 'TSIntersectionType': { return inferUnionType(node) + } - default: + default: { return inferRuntimeTypeFromTypeNode( context, /** @type {TypeNode} */ (node) ) + } } /** @@ -483,11 +505,13 @@ function inferTypeLiteralType(node) { for (const m of node.members) { switch (m.type) { case 'TSCallSignatureDeclaration': - case 'TSConstructSignatureDeclaration': + case 'TSConstructSignatureDeclaration': { types.add('Function') break - default: + } + default: { types.add('Object') + } } } return types.size > 0 ? [...types] : ['Object'] @@ -503,16 +527,21 @@ function inferEnumType(context, node) { if (m.initializer) { if (m.initializer.type === 'Literal') { switch (typeof m.initializer.value) { - case 'string': + case 'string': { types.add('String') break + } case 'number': - case 'bigint': // Now it's a syntax error. + case 'bigint': { + // Now it's a syntax error. types.add('Number') break - case 'boolean': // Now it's a syntax error. + } + case 'boolean': { + // Now it's a syntax error. types.add('Boolean') break + } } } else { for (const type of inferRuntimeTypeFromTypeNode(