From 03b45436abfccb66636aeece4499d862a086123a Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sat, 6 May 2023 18:55:31 +0900 Subject: [PATCH 1/9] chore: update eslint-plugin-unicorn --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3449d4740..1848a5f4d 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "eslint-plugin-jsonc": "^2.2.1", "eslint-plugin-node-dependencies": ">=0.5.0 <1.0.0", "eslint-plugin-prettier": "^4.0.0", - "eslint-plugin-unicorn": "^42.0.0", + "eslint-plugin-unicorn": "^47.0.0", "eslint-plugin-vue": "file:.", "espree": "^9.3.2", "events": "^3.3.0", From 27e8adad416d9f5dff09ea284d4cbe7eca583e86 Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sat, 6 May 2023 18:58:32 +0900 Subject: [PATCH 2/9] chore: run 'npm run lint:fix' --- .../no-invalid-meta-docs-categories.js | 6 +- lib/processor.js | 30 ++++-- lib/rules/attributes-order.js | 48 ++++++---- lib/rules/block-lang.js | 8 +- lib/rules/block-tag-newline.js | 18 ++-- lib/rules/comment-directive.js | 18 ++-- lib/rules/component-api-style.js | 2 +- lib/rules/define-emits-declaration.js | 6 +- lib/rules/define-props-declaration.js | 6 +- lib/rules/first-attribute-linebreak.js | 2 +- lib/rules/html-closing-bracket-newline.js | 9 +- lib/rules/html-self-closing.js | 2 +- lib/rules/match-component-file-name.js | 2 +- lib/rules/max-attributes-per-line.js | 2 +- lib/rules/max-len.js | 16 +--- lib/rules/multi-word-component-names.js | 2 +- .../multiline-html-element-content-newline.js | 6 +- lib/rules/no-boolean-default.js | 6 +- lib/rules/no-extra-parens.js | 10 +- lib/rules/no-multi-spaces.js | 2 +- lib/rules/no-multiple-template-root.js | 14 +-- lib/rules/no-restricted-call-after-await.js | 4 +- lib/rules/no-restricted-custom-event.js | 2 +- lib/rules/no-undef-components.js | 4 +- lib/rules/no-unused-components.js | 6 +- lib/rules/no-useless-mustaches.js | 4 +- lib/rules/no-useless-v-bind.js | 8 +- lib/rules/quote-props.js | 2 +- lib/rules/require-default-prop.js | 6 +- lib/rules/require-explicit-emits.js | 10 +- lib/rules/require-name-property.js | 2 +- lib/rules/require-prop-comment.js | 16 ++-- lib/rules/require-prop-types.js | 6 +- lib/rules/require-valid-default-prop.js | 39 ++++---- lib/rules/script-setup-uses-vars.js | 2 +- lib/rules/this-in-template.js | 2 +- lib/rules/v-for-delimiter-style.js | 2 +- lib/rules/v-on-function-call.js | 2 +- lib/rules/v-on-handler-style.js | 20 ++-- lib/rules/v-slot-style.js | 12 ++- lib/rules/valid-v-cloak.js | 2 +- lib/rules/valid-v-else-if.js | 2 +- lib/rules/valid-v-else.js | 2 +- lib/rules/valid-v-for.js | 2 +- lib/rules/valid-v-html.js | 2 +- lib/rules/valid-v-if.js | 2 +- lib/rules/valid-v-is.js | 2 +- lib/rules/valid-v-memo.js | 6 +- lib/rules/valid-v-on.js | 2 +- lib/rules/valid-v-once.js | 2 +- lib/rules/valid-v-pre.js | 2 +- lib/rules/valid-v-show.js | 2 +- lib/rules/valid-v-slot.js | 2 +- lib/rules/valid-v-text.js | 2 +- lib/utils/casing.js | 10 +- lib/utils/indent-common.js | 59 ++++++------ lib/utils/indent-ts.js | 14 ++- lib/utils/indent-utils.js | 2 +- lib/utils/index.js | 21 ++--- lib/utils/property-references.js | 10 +- lib/utils/ref-object-references.js | 2 +- lib/utils/regexp.js | 2 +- lib/utils/selector.js | 93 ++++++++++++------- lib/utils/ts-ast-utils.js | 53 +++++++---- tests/integrations/eslint-plugin-import.js | 4 +- tests/lib/autofix.js | 2 +- tests/lib/rules-without-vue-eslint-parser.js | 2 +- tests/lib/rules/comment-directive.js | 2 +- tests/lib/rules/component-tags-order.js | 2 +- tests/lib/rules/html-indent.js | 4 +- .../rules/no-restricted-call-after-await.js | 2 +- tests/lib/rules/no-unused-properties.js | 2 +- tests/lib/rules/script-indent.js | 4 +- tests/lib/utils/casing.js | 2 +- tests/lib/utils/comments.js | 2 +- tests/lib/utils/html-comments.js | 6 +- tests/lib/utils/index.js | 2 +- tests/lib/utils/ref-object-references.js | 6 +- tests/lib/utils/regexp.js | 2 +- tests/lib/utils/selector.js | 6 +- tools/lib/configs.js | 4 +- tools/lib/rules.js | 4 +- tools/new-rule.js | 6 +- tools/update-docs-rules-index.js | 4 +- tools/update-docs.js | 12 +-- tools/update-lib-configs.js | 4 +- tools/update-lib-index.js | 4 +- tools/update-no-layout-rules-config.js | 4 +- tools/update-vue3-export-names.js | 8 +- 89 files changed, 421 insertions(+), 341 deletions(-) diff --git a/eslint-internal-rules/no-invalid-meta-docs-categories.js b/eslint-internal-rules/no-invalid-meta-docs-categories.js index 24350c8f6..67f9789f3 100644 --- a/eslint-internal-rules/no-invalid-meta-docs-categories.js +++ b/eslint-internal-rules/no-invalid-meta-docs-categories.js @@ -71,15 +71,15 @@ 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 { fixes.push( fixer.insertTextBefore( category.value, `['vue3-${category.value.value}', ` ) ) - } else { - fixes.push(fixer.insertTextBefore(category.value, '[')) } fixes.push(fixer.insertTextAfter(category.value, ']')) } diff --git a/lib/processor.js b/lib/processor.js index e0c10ef72..4952cca74 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 e6915aebf..226eb6df7 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 + } } } @@ -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] } /** diff --git a/lib/rules/block-lang.js b/lib/rules/block-lang.js index 35898e938..bc6d657fb 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: - return `${langs.slice(0, -1).join(', ')}, and ${langs[langs.length - 1]}` + } + default: { + return `${langs.slice(0, -1).join(', ')}, and ${langs.at(-1)}` + } } } diff --git a/lib/rules/block-tag-newline.js b/lib/rules/block-tag-newline.js index 7096b2daf..2f7c6db29 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` + } } } @@ -324,17 +326,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) } } } diff --git a/lib/rules/comment-directive.js b/lib/rules/comment-directive.js index 8ed085602..aeca98aef 100644 --- a/lib/rules/comment-directive.js +++ b/lib/rules/comment-directive.js @@ -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' }) } } @@ -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 } }) } } diff --git a/lib/rules/component-api-style.js b/lib/rules/component-api-style.js index 550eebfed..6fdcf32be 100644 --- a/lib/rules/component-api-style.js +++ b/lib/rules/component-api-style.js @@ -162,7 +162,7 @@ function buildAllowedPhrase(allowsOpt) { phrases.push('Options API') } return phrases.length > 2 - ? `${phrases.slice(0, -1).join(', ')} or ${phrases.slice(-1)[0]}` + ? `${phrases.slice(0, -1).join(', ')} or ${phrases.at(-1)}` : phrases.join(' or ') } diff --git a/lib/rules/define-emits-declaration.js b/lib/rules/define-emits-declaration.js index 9ef9bbc03..47e7e187d 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 f86f3ad98..6b78ac78e 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/first-attribute-linebreak.js b/lib/rules/first-attribute-linebreak.js index 83f018e02..11fefa44f 100644 --- a/lib/rules/first-attribute-linebreak.js +++ b/lib/rules/first-attribute-linebreak.js @@ -69,7 +69,7 @@ module.exports = { const firstAttribute = node.attributes[0] if (!firstAttribute) return - const lastAttribute = node.attributes[node.attributes.length - 1] + const lastAttribute = node.attributes.at(-1) const location = firstAttribute.loc.start.line === lastAttribute.loc.end.line diff --git a/lib/rules/html-closing-bracket-newline.js b/lib/rules/html-closing-bracket-newline.js index 73afb9d69..e92d45165 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/html-self-closing.js b/lib/rules/html-self-closing.js index 19fc4b587..00f2c1147 100644 --- a/lib/rules/html-self-closing.js +++ b/lib/rules/html-self-closing.js @@ -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() === '' } diff --git a/lib/rules/match-component-file-name.js b/lib/rules/match-component-file-name.js index 70c34009b..5d92be0d1 100644 --- a/lib/rules/match-component-file-name.js +++ b/lib/rules/match-component-file-name.js @@ -6,7 +6,7 @@ const utils = require('../utils') const casing = require('../utils/casing') -const path = require('path') +const path = require('node:path') /** * @param {Expression | SpreadElement} node diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 0f204d500..7997e9bf0 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -51,7 +51,7 @@ function groupAttrsByLine(attributes) { const current = attributes[index] if (previous.loc.end.line === current.loc.start.line) { - propsPerLine[propsPerLine.length - 1].push(current) + propsPerLine.at(-1).push(current) } else { propsPerLine.push([current]) } diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index 588f2de75..2e619a154 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -212,10 +212,7 @@ module.exports = { const htmlAttributeValues = [] // The options object must be the last option specified… - const options = Object.assign( - {}, - context.options[context.options.length - 1] - ) + const options = Object.assign({}, context.options.at(-1)) // …but max code length… if (typeof context.options[0] === 'number') { @@ -347,18 +344,15 @@ module.exports = { scriptComments[0].loc.start.line ), Math.max( - scriptTokens[scriptTokens.length - 1].loc.end.line, - scriptComments[scriptComments.length - 1].loc.end.line + scriptTokens.at(-1).loc.end.line, + scriptComments.at(-1).loc.end.line ) ] - : [ - scriptTokens[0].loc.start.line, - scriptTokens[scriptTokens.length - 1].loc.end.line - ] + : [scriptTokens[0].loc.start.line, scriptTokens.at(-1).loc.end.line] } else if (scriptComments.length > 0) { scriptLinesRange = [ scriptComments[0].loc.start.line, - scriptComments[scriptComments.length - 1].loc.end.line + scriptComments.at(-1).loc.end.line ] } const templateLinesRange = templateBody && [ diff --git a/lib/rules/multi-word-component-names.js b/lib/rules/multi-word-component-names.js index 38426bfb0..be3388bb4 100644 --- a/lib/rules/multi-word-component-names.js +++ b/lib/rules/multi-word-component-names.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('path') +const path = require('node:path') const casing = require('../utils/casing') const utils = require('../utils') 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 7dbeeceaf..e3cbebf2b 100644 --- a/lib/rules/no-boolean-default.js +++ b/lib/rules/no-boolean-default.js @@ -92,15 +92,16 @@ module.exports = { */ function verifyDefaultExpression(defaultNode) { switch (booleanType) { - case 'no-default': + case 'no-default': { context.report({ node: defaultNode, message: 'Boolean prop should not set a default (Vue defaults it to false).' }) break + } - case 'default-false': + case 'default-false': { if (defaultNode.type !== 'Literal' || defaultNode.value !== false) { context.report({ node: defaultNode, @@ -108,6 +109,7 @@ module.exports = { }) } break + } } } return utils.compositingVisitors( diff --git a/lib/rules/no-extra-parens.js b/lib/rules/no-extra-parens.js index bf1ff1e46..aaabb35f2 100644 --- a/lib/rules/no-extra-parens.js +++ b/lib/rules/no-extra-parens.js @@ -97,15 +97,15 @@ function createForVueSyntax(context) { function isUnwrapChangeToFilter(expression) { let parenStack = null for (const token of tokenStore.getTokens(expression)) { - if (!parenStack) { - if (token.value === '|') { - return true - } - } else { + if (parenStack) { if (parenStack.isUpToken(token)) { parenStack = parenStack.upper continue } + } else { + if (token.value === '|') { + return true + } } if (isLeftParen(token)) { parenStack = { isUpToken: isRightParen, upper: parenStack } diff --git a/lib/rules/no-multi-spaces.js b/lib/rules/no-multi-spaces.js index e13cdff74..f9871c3ed 100644 --- a/lib/rules/no-multi-spaces.js +++ b/lib/rules/no-multi-spaces.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('path') +const path = require('node:path') /** * @param {RuleContext} context diff --git a/lib/rules/no-multiple-template-root.js b/lib/rules/no-multiple-template-root.js index d5ac49b5a..e09b1e316 100644 --- a/lib/rules/no-multiple-template-root.js +++ b/lib/rules/no-multiple-template-root.js @@ -59,13 +59,7 @@ module.exports = { loc: extraText.loc, message: 'The template root requires an element rather than texts.' }) - } else if (extraElement != null) { - context.report({ - node: extraElement, - loc: extraElement.loc, - message: 'The template root requires exactly one element.' - }) - } else { + } else if (extraElement == null) { for (const element of rootElements) { const tag = element.startTag const name = element.name @@ -86,6 +80,12 @@ module.exports = { }) } } + } else { + context.report({ + node: extraElement, + loc: extraElement.loc, + message: 'The template root requires exactly one element.' + }) } } } diff --git a/lib/rules/no-restricted-call-after-await.js b/lib/rules/no-restricted-call-after-await.js index d2b2a1414..e0bdc0eaf 100644 --- a/lib/rules/no-restricted-call-after-await.js +++ b/lib/rules/no-restricted-call-after-await.js @@ -3,8 +3,8 @@ * See LICENSE file in root directory for full license. */ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const { ReferenceTracker } = require('@eslint-community/eslint-utils') const utils = require('../utils') diff --git a/lib/rules/no-restricted-custom-event.js b/lib/rules/no-restricted-custom-event.js index abe104b97..64a63d1a0 100644 --- a/lib/rules/no-restricted-custom-event.js +++ b/lib/rules/no-restricted-custom-event.js @@ -155,7 +155,7 @@ module.exports = { sourceCode.text[nameWithLoc.range[0]] }${JSON.stringify(option.suggest) .slice(1, -1) - .replace(/'/gu, "\\'")}${ + .replaceAll("'", "\\'")}${ sourceCode.text[nameWithLoc.range[1] - 1] }` ) diff --git a/lib/rules/no-undef-components.js b/lib/rules/no-undef-components.js index 9eb74ad05..fd9f9a07c 100644 --- a/lib/rules/no-undef-components.js +++ b/lib/rules/no-undef-components.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('path') +const path = require('node:path') const utils = require('../utils') const casing = require('../utils/casing') @@ -15,7 +15,7 @@ const casing = require('../utils/casing') * @param {string} str */ function camelize(str) { - return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) + return str.replaceAll(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) } class DefinedInSetupComponents { diff --git a/lib/rules/no-unused-components.js b/lib/rules/no-unused-components.js index ffab616c1..0fa7d10b0 100644 --- a/lib/rules/no-unused-components.js +++ b/lib/rules/no-unused-components.js @@ -33,9 +33,9 @@ module.exports = { create(context) { const options = context.options[0] || {} const ignoreWhenBindingPresent = - options.ignoreWhenBindingPresent !== undefined - ? options.ignoreWhenBindingPresent - : true + options.ignoreWhenBindingPresent === undefined + ? true + : options.ignoreWhenBindingPresent const usedComponents = new Set() /** @type { { node: Property, name: string }[] } */ let registeredComponents = [] diff --git a/lib/rules/no-useless-mustaches.js b/lib/rules/no-useless-mustaches.js index 51e3853ba..624283051 100644 --- a/lib/rules/no-useless-mustaches.js +++ b/lib/rules/no-useless-mustaches.js @@ -14,7 +14,7 @@ const utils = require('../utils') function stripQuotesForHTML(text) { if ( (text[0] === '"' || text[0] === "'" || text[0] === '`') && - text[0] === text[text.length - 1] + text[0] === text.at(-1) ) { return text.slice(1, -1) } @@ -144,7 +144,7 @@ module.exports = { return null } - return fixer.replaceText(node, text.replace(/\\([\S\s])/g, '$1')) + return fixer.replaceText(node, text.replaceAll(/\\([\S\s])/g, '$1')) } }) } diff --git a/lib/rules/no-useless-v-bind.js b/lib/rules/no-useless-v-bind.js index 9f9da6e27..bbbea7c4f 100644 --- a/lib/rules/no-useless-v-bind.js +++ b/lib/rules/no-useless-v-bind.js @@ -131,13 +131,13 @@ module.exports = { let attrValue if (quoteChar === '"') { - attrValue = strValue.replace(DOUBLE_QUOTES_RE, '"') + attrValue = strValue.replaceAll(DOUBLE_QUOTES_RE, '"') } else if (quoteChar === "'") { - attrValue = strValue.replace(SINGLE_QUOTES_RE, ''') + attrValue = strValue.replaceAll(SINGLE_QUOTES_RE, ''') } else { attrValue = strValue - .replace(DOUBLE_QUOTES_RE, '"') - .replace(SINGLE_QUOTES_RE, ''') + .replaceAll(DOUBLE_QUOTES_RE, '"') + .replaceAll(SINGLE_QUOTES_RE, ''') } yield fixer.replaceText(expression, attrValue) } diff --git a/lib/rules/quote-props.js b/lib/rules/quote-props.js index aabfc2bf5..518f78a04 100644 --- a/lib/rules/quote-props.js +++ b/lib/rules/quote-props.js @@ -40,7 +40,7 @@ module.exports = wrapCoreRule('quote-props', { )) { yield fixer.replaceTextRange( fix.range, - fix.text.replace(/["']/gu, expectedQuote) + fix.text.replaceAll(/["']/gu, expectedQuote) ) } } diff --git a/lib/rules/require-default-prop.js b/lib/rules/require-default-prop.js index 21ee6825e..acd532ce0 100644 --- a/lib/rules/require-default-prop.js +++ b/lib/rules/require-default-prop.js @@ -150,9 +150,9 @@ module.exports = { continue } const propName = - prop.propName != null - ? prop.propName - : `[${context.getSourceCode().getText(prop.node.key)}]` + prop.propName == null + ? `[${context.getSourceCode().getText(prop.node.key)}]` + : prop.propName context.report({ node: prop.node, diff --git a/lib/rules/require-explicit-emits.js b/lib/rules/require-explicit-emits.js index 167613804..524ec8b8c 100644 --- a/lib/rules/require-explicit-emits.js +++ b/lib/rules/require-explicit-emits.js @@ -431,7 +431,7 @@ function buildSuggest(define, emits, nameWithLoc, context) { define.type === 'ObjectExpression' ? '`emits` option' : '`defineEmits`' const certainEmits = emits.filter((e) => e.key) if (certainEmits.length > 0) { - const last = certainEmits[certainEmits.length - 1] + const last = certainEmits.at(-1) return [ { messageId: 'addOneOption', @@ -529,9 +529,7 @@ function buildSuggest(define, emits, nameWithLoc, context) { `\nemits: ['${nameWithLoc.name}'],` ) } else if (object.properties.length > 0) { - const before = - propertyNodes[propertyNodes.length - 1] || - object.properties[object.properties.length - 1] + const before = propertyNodes.at(-1) || object.properties.at(-1) return fixer.insertTextAfter( before, `,\nemits: ['${nameWithLoc.name}']` @@ -564,9 +562,7 @@ function buildSuggest(define, emits, nameWithLoc, context) { `\nemits: {'${nameWithLoc.name}': null},` ) } else if (object.properties.length > 0) { - const before = - propertyNodes[propertyNodes.length - 1] || - object.properties[object.properties.length - 1] + const before = propertyNodes.at(-1) || object.properties.at(-1) return fixer.insertTextAfter( before, `,\nemits: {'${nameWithLoc.name}': null}` diff --git a/lib/rules/require-name-property.js b/lib/rules/require-name-property.js index 62eb0cfd5..84a203a01 100644 --- a/lib/rules/require-name-property.js +++ b/lib/rules/require-name-property.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('path') +const path = require('node:path') const utils = require('../utils') const { getVueComponentDefinitionType } = require('../utils') /** diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js index 8dd852cbc..5a9f6f744 100644 --- a/lib/rules/require-prop-comment.js +++ b/lib/rules/require-prop-comment.js @@ -68,26 +68,28 @@ module.exports = { const precedingComments = sourceCode.getCommentsBefore(prop.node) const lastPrecedingComment = - precedingComments.length > 0 - ? precedingComments[precedingComments.length - 1] - : undefined + precedingComments.length > 0 ? precedingComments.at(-1) : undefined /** @type {string|undefined} */ 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/require-prop-types.js b/lib/rules/require-prop-types.js index e4d9fdcc1..e4f5f890d 100644 --- a/lib/rules/require-prop-types.js +++ b/lib/rules/require-prop-types.js @@ -54,9 +54,7 @@ module.exports = { const { value, node, propName } = prop let hasType = true - if (!value) { - hasType = false - } else { + if (value) { switch (value.type) { case 'ObjectExpression': { // foo: { @@ -74,6 +72,8 @@ module.exports = { break } } + } else { + hasType = false } if (!hasType) { diff --git a/lib/rules/require-valid-default-prop.js b/lib/rules/require-valid-default-prop.js index 50a3d191b..ba44b823c 100644 --- a/lib/rules/require-valid-default-prop.js +++ b/lib/rules/require-valid-default-prop.js @@ -230,9 +230,9 @@ module.exports = { */ function report(node, prop, expectedTypeNames) { const propName = - prop.propName != null - ? prop.propName - : `[${context.getSourceCode().getText(prop.node.key)}]` + prop.propName == null + ? `[${context.getSourceCode().getText(prop.node.key)}]` + : prop.propName context.report({ node, message: @@ -280,21 +280,7 @@ module.exports = { if (!defType) continue - if (!defType.function) { - if ( - typeNames.has(defType.type) && - !FUNCTION_VALUE_TYPES.has(defType.type) - ) { - continue - } - report( - defExpr, - prop, - [...typeNames].map((type) => - FUNCTION_VALUE_TYPES.has(type) ? 'Function' : type - ) - ) - } else { + if (defType.function) { if (typeNames.has('Function')) { continue } @@ -310,6 +296,20 @@ module.exports = { default: defType }) } + } else { + if ( + typeNames.has(defType.type) && + !FUNCTION_VALUE_TYPES.has(defType.type) + ) { + continue + } + report( + defExpr, + prop, + [...typeNames].map((type) => + FUNCTION_VALUE_TYPES.has(type) ? 'Function' : type + ) + ) } } return propContexts @@ -415,8 +415,7 @@ module.exports = { * @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node */ ':function'(node) { - const data = - scriptSetupPropsContexts[scriptSetupPropsContexts.length - 1] + const data = scriptSetupPropsContexts.at(-1) if (!data || !scopeStack) { return } diff --git a/lib/rules/script-setup-uses-vars.js b/lib/rules/script-setup-uses-vars.js index 6af828177..f76c530a0 100644 --- a/lib/rules/script-setup-uses-vars.js +++ b/lib/rules/script-setup-uses-vars.js @@ -15,7 +15,7 @@ const casing = require('../utils/casing') * @param {string} str */ function camelize(str) { - return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) + return str.replaceAll(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) } module.exports = { diff --git a/lib/rules/this-in-template.js b/lib/rules/this-in-template.js index 381871c42..f31ad114f 100644 --- a/lib/rules/this-in-template.js +++ b/lib/rules/this-in-template.js @@ -30,7 +30,7 @@ module.exports = { * @returns {Object} AST event handlers. */ create(context) { - const options = context.options[0] !== 'always' ? 'never' : 'always' + const options = context.options[0] === 'always' ? 'always' : 'never' /** * @typedef {object} ScopeStack * @property {ScopeStack | null} parent diff --git a/lib/rules/v-for-delimiter-style.js b/lib/rules/v-for-delimiter-style.js index f4f262100..861ef5570 100644 --- a/lib/rules/v-for-delimiter-style.js +++ b/lib/rules/v-for-delimiter-style.js @@ -35,7 +35,7 @@ module.exports = { const delimiterToken = /** @type {Token} */ ( tokenStore.getTokenAfter( node.left.length > 0 - ? node.left[node.left.length - 1] + ? node.left.at(-1) : tokenStore.getFirstToken(node), (token) => token.type !== 'Punctuator' || token.value !== ')' ) diff --git a/lib/rules/v-on-function-call.js b/lib/rules/v-on-function-call.js index e2409794b..81b8a4b8e 100644 --- a/lib/rules/v-on-function-call.js +++ b/lib/rules/v-on-function-call.js @@ -162,7 +162,7 @@ module.exports = { const range = leftQuote && rightQuote ? [leftQuote.range[1], rightQuote.range[0]] - : [tokens[0].range[0], tokens[tokens.length - 1].range[1]] + : [tokens[0].range[0], tokens.at(-1).range[1]] return fixer.replaceTextRange( range, diff --git a/lib/rules/v-on-handler-style.js b/lib/rules/v-on-handler-style.js index aa1214e38..a17ffe666 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 } @@ -236,7 +241,7 @@ module.exports = { includeComments: true }) const firstToken = tokens[0] - const lastToken = tokens[tokens.length - 1] + const lastToken = tokens.at(-1) const hasQuote = isQuote(firstToken) /** @type {Range} */ @@ -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/rules/valid-v-cloak.js b/lib/rules/valid-v-cloak.js index 8fe19504f..2af5c2cf3 100644 --- a/lib/rules/valid-v-cloak.js +++ b/lib/rules/valid-v-cloak.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-else-if.js b/lib/rules/valid-v-else-if.js index d8888534d..62893fe81 100644 --- a/lib/rules/valid-v-else-if.js +++ b/lib/rules/valid-v-else-if.js @@ -65,7 +65,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-else.js b/lib/rules/valid-v-else.js index 8c7f8e49a..13d538541 100644 --- a/lib/rules/valid-v-else.js +++ b/lib/rules/valid-v-else.js @@ -65,7 +65,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-for.js b/lib/rules/valid-v-for.js index 9dcf76350..77b2c4b3e 100644 --- a/lib/rules/valid-v-for.js +++ b/lib/rules/valid-v-for.js @@ -133,7 +133,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-html.js b/lib/rules/valid-v-html.js index af4636a50..ed222d692 100644 --- a/lib/rules/valid-v-html.js +++ b/lib/rules/valid-v-html.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-if.js b/lib/rules/valid-v-if.js index b728a829b..5ff9e8228 100644 --- a/lib/rules/valid-v-if.js +++ b/lib/rules/valid-v-if.js @@ -57,7 +57,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-is.js b/lib/rules/valid-v-is.js index 2f97d1fd9..ef6cdd5dd 100644 --- a/lib/rules/valid-v-is.js +++ b/lib/rules/valid-v-is.js @@ -55,7 +55,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-memo.js b/lib/rules/valid-v-memo.js index 66ba9a75d..1a038deea 100644 --- a/lib/rules/valid-v-memo.js +++ b/lib/rules/valid-v-memo.js @@ -59,7 +59,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) @@ -102,9 +102,7 @@ module.exports = { break } case 'SequenceExpression': { - expressions.push( - expression.expressions[expression.expressions.length - 1] - ) + expressions.push(expression.expressions.at(-1)) break } case 'ConditionalExpression': { diff --git a/lib/rules/valid-v-on.js b/lib/rules/valid-v-on.js index 7c5471f28..93303e7bd 100644 --- a/lib/rules/valid-v-on.js +++ b/lib/rules/valid-v-on.js @@ -118,7 +118,7 @@ module.exports = { let innerText = valueText if ( (valueText[0] === '"' || valueText[0] === "'") && - valueText[0] === valueText[valueText.length - 1] + valueText[0] === valueText.at(-1) ) { // quoted innerText = valueText.slice(1, -1) diff --git a/lib/rules/valid-v-once.js b/lib/rules/valid-v-once.js index 987218585..9756cda39 100644 --- a/lib/rules/valid-v-once.js +++ b/lib/rules/valid-v-once.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-pre.js b/lib/rules/valid-v-pre.js index 7cd9e30ec..f54a35e50 100644 --- a/lib/rules/valid-v-pre.js +++ b/lib/rules/valid-v-pre.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-show.js b/lib/rules/valid-v-show.js index c547b8c31..2ad7d6027 100644 --- a/lib/rules/valid-v-show.js +++ b/lib/rules/valid-v-show.js @@ -41,7 +41,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-slot.js b/lib/rules/valid-v-slot.js index c33107966..730c17de6 100644 --- a/lib/rules/valid-v-slot.js +++ b/lib/rules/valid-v-slot.js @@ -377,7 +377,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'disallowAnyModifier' }) diff --git a/lib/rules/valid-v-text.js b/lib/rules/valid-v-text.js index c6ef676d1..b0115a094 100644 --- a/lib/rules/valid-v-text.js +++ b/lib/rules/valid-v-text.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers[node.key.modifiers.length - 1].loc.end + end: node.key.modifiers.at(-1).loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/utils/casing.js b/lib/utils/casing.js index a47e978cd..eddef6cd7 100644 --- a/lib/utils/casing.js +++ b/lib/utils/casing.js @@ -27,8 +27,8 @@ function hasUpper(str) { */ function kebabCase(str) { return str - .replace(/_/gu, '-') - .replace(/\B([A-Z])/gu, '-$1') + .replaceAll('_', '-') + .replaceAll(/\B([A-Z])/gu, '-$1') .toLowerCase() } @@ -52,8 +52,8 @@ function isKebabCase(str) { */ function snakeCase(str) { return str - .replace(/\B([A-Z])/gu, '_$1') - .replace(/-/gu, '_') + .replaceAll(/\B([A-Z])/gu, '_$1') + .replaceAll('-', '_') .toLowerCase() } @@ -74,7 +74,7 @@ function camelCase(str) { if (isPascalCase(str)) { return str.charAt(0).toLowerCase() + str.slice(1) } - return str.replace(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : '')) + return str.replaceAll(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : '')) } /** diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index 4e3edb229..983b12069 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -372,7 +372,7 @@ module.exports.defineVisitor = function create( } const elementTokens = getFirstAndLastTokens( node, - lastToken != null ? lastToken.range[1] : 0 + lastToken == null ? 0 : lastToken.range[1] ) // Collect comma/comment tokens between the last token of the previous node and the first token of this node. @@ -578,15 +578,15 @@ module.exports.defineVisitor = function create( function processTopLevelNode(node, expectedIndent) { const token = tokenStore.getFirstToken(node) const offsetInfo = offsets.get(token) - if (offsetInfo != null) { - offsetInfo.expectedIndent = expectedIndent - } else { + if (offsetInfo == null) { offsets.set(token, { baseToken: null, offset: 0, baseline: false, expectedIndent }) + } else { + offsetInfo.expectedIndent = expectedIndent } } @@ -639,9 +639,7 @@ module.exports.defineVisitor = function create( const offsetInfo = offsets.get(token) if (offsetInfo != null) { - if (offsetInfo.expectedIndent != null) { - expectedIndents.push(offsetInfo.expectedIndent) - } else { + if (offsetInfo.expectedIndent == null) { const baseOffsetInfo = offsets.get(offsetInfo.baseToken) if ( baseOffsetInfo != null && @@ -656,6 +654,8 @@ module.exports.defineVisitor = function create( break } } + } else { + expectedIndents.push(offsetInfo.expectedIndent) } } } @@ -943,7 +943,12 @@ module.exports.defineVisitor = function create( }, /** @param {VElement} node */ VElement(node) { - if (!PREFORMATTED_ELEMENT_NAMES.has(node.name)) { + if (PREFORMATTED_ELEMENT_NAMES.has(node.name)) { + const startTagToken = tokenStore.getFirstToken(node) + const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag) + setOffset(endTagToken, 0, startTagToken) + setPreformattedTokens(node) + } else { const isTopLevel = node.parent.type !== 'VElement' const offset = isTopLevel ? options.baseIndent : 1 processNodeList( @@ -953,11 +958,6 @@ module.exports.defineVisitor = function create( offset, false ) - } else { - const startTagToken = tokenStore.getFirstToken(node) - const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag) - setOffset(endTagToken, 0, startTagToken) - setPreformattedTokens(node) } }, /** @param {VEndTag} node */ @@ -1050,9 +1050,9 @@ module.exports.defineVisitor = function create( ) if (closeToken != null && closeToken.type.endsWith('TagClose')) { const offset = - closeToken.type !== 'HTMLSelfClosingTagClose' - ? options.closeBracket.startTag - : options.closeBracket.selfClosingTag + closeToken.type === 'HTMLSelfClosingTagClose' + ? options.closeBracket.selfClosingTag + : options.closeBracket.startTag setOffset(closeToken, offset, openToken) } }, @@ -1082,7 +1082,7 @@ module.exports.defineVisitor = function create( 'ArrayExpression, ArrayPattern'(node) { const firstToken = tokenStore.getFirstToken(node) const rightToken = tokenStore.getTokenAfter( - node.elements[node.elements.length - 1] || firstToken, + node.elements.at(-1) || firstToken, isClosingBracketToken ) processNodeList(node.elements, firstToken, rightToken, 1) @@ -1290,9 +1290,7 @@ module.exports.defineVisitor = function create( ...tokenStore.getTokensBetween(exportToken, node.source), tokenStore.getFirstToken(node.source) ] - if (!node.exported) { - setOffset(tokens, 1, exportToken) - } else { + if (node.exported) { // export * as foo from "mod" const starToken = /** @type {Token} */ (tokens.find(isWildcard)) const asToken = tokenStore.getTokenAfter(starToken) @@ -1303,6 +1301,8 @@ module.exports.defineVisitor = function create( setOffset(asToken, 1, starToken) setOffset(exportedToken, 1, starToken) setOffset(afterTokens, 1, exportToken) + } else { + setOffset(tokens, 1, exportToken) } // assertions @@ -1492,7 +1492,7 @@ module.exports.defineVisitor = function create( ) } const rightParenToken = tokenStore.getTokenAfter( - node.params[node.params.length - 1] || leftParenToken, + node.params.at(-1) || leftParenToken, isClosingParenToken ) setOffset(leftParenToken, 1, bodyBaseToken) @@ -1563,10 +1563,7 @@ module.exports.defineVisitor = function create( if (namedSpecifiers.length > 0) { const leftBrace = tokenStore.getTokenBefore(namedSpecifiers[0]) const rightBrace = /** @type {Token} */ ( - tokenStore.getTokenAfter( - namedSpecifiers[namedSpecifiers.length - 1], - isClosingBraceToken - ) + tokenStore.getTokenAfter(namedSpecifiers.at(-1), isClosingBraceToken) ) processNodeList(namedSpecifiers, leftBrace, rightBrace, 1) for (const token of [ @@ -1719,7 +1716,7 @@ module.exports.defineVisitor = function create( 'ObjectExpression, ObjectPattern'(node) { const firstToken = tokenStore.getFirstToken(node) const rightToken = tokenStore.getTokenAfter( - node.properties[node.properties.length - 1] || firstToken, + node.properties.at(-1) || firstToken, isClosingBraceToken ) processNodeList(node.properties, firstToken, rightToken, 1) @@ -1732,7 +1729,11 @@ module.exports.defineVisitor = function create( SwitchCase(node) { const caseToken = tokenStore.getFirstToken(node) - if (node.test != null) { + if (node.test == null) { + const colonToken = tokenStore.getTokenAfter(caseToken) + + setOffset(colonToken, 1, caseToken) + } else { const testToken = tokenStore.getTokenAfter(caseToken) const colonToken = tokenStore.getTokenAfter( node.test, @@ -1740,10 +1741,6 @@ module.exports.defineVisitor = function create( ) setOffset([testToken, colonToken], 1, caseToken) - } else { - const colonToken = tokenStore.getTokenAfter(caseToken) - - setOffset(colonToken, 1, caseToken) } if ( diff --git a/lib/utils/indent-ts.js b/lib/utils/indent-ts.js index c7ad7ad63..901a9c566 100644 --- a/lib/utils/indent-ts.js +++ b/lib/utils/indent-ts.js @@ -418,7 +418,7 @@ function defineVisitor({ const leftParenToken = currToken const rightParenToken = /**@type {Token} */ ( tokenStore.getTokenAfter( - node.params[node.params.length - 1] || leftParenToken, + node.params.at(-1) || leftParenToken, isClosingParenToken ) ) @@ -493,7 +493,7 @@ function defineVisitor({ const leftBracketToken = tokenStore.getFirstToken(node) const rightBracketToken = /**@type {Token} */ ( tokenStore.getTokenAfter( - node.parameters[node.parameters.length - 1] || leftBracketToken, + node.parameters.at(-1) || leftBracketToken, isClosingBracketToken ) ) @@ -864,7 +864,7 @@ function defineVisitor({ firstToken ) const rightParenToken = tokenStore.getTokenAfter( - node.params[node.params.length - 1] || leftParenToken, + node.params.at(-1) || leftParenToken, isClosingParenToken ) processNodeList(node.params, leftParenToken, rightParenToken, 1) @@ -923,7 +923,7 @@ function defineVisitor({ const leftParenToken = currToken const rightParenToken = /** @type {Token} */ ( tokenStore.getTokenAfter( - node.params[node.params.length - 1] || leftParenToken, + node.params.at(-1) || leftParenToken, isClosingParenToken ) ) @@ -994,7 +994,7 @@ function defineVisitor({ leftParenToken = tokenStore.getTokenAfter(node.typeParameters) } const rightParenToken = tokenStore.getTokenAfter( - node.params[node.params.length - 1] || leftParenToken, + node.params.at(-1) || leftParenToken, isClosingParenToken ) setOffset(leftParenToken, 1, bodyBaseToken) @@ -1306,9 +1306,7 @@ function defineVisitor({ } if (decorators[0] === node) { if (range[0] === node.range[0]) { - const startParentToken = tokenStore.getTokenAfter( - decorators[decorators.length - 1] - ) + const startParentToken = tokenStore.getTokenAfter(decorators.at(-1)) setOffset(startParentToken, 0, atToken) } else { const startParentToken = tokenStore.getFirstToken( diff --git a/lib/utils/indent-utils.js b/lib/utils/indent-utils.js index 5e40ba996..e5189d638 100644 --- a/lib/utils/indent-utils.js +++ b/lib/utils/indent-utils.js @@ -101,7 +101,7 @@ function isPipeOperator(token) { * @returns {T | undefined} The last element or undefined. */ function last(xs) { - return xs.length === 0 ? undefined : xs[xs.length - 1] + return xs.length === 0 ? undefined : xs.at(-1) } module.exports = { diff --git a/lib/utils/index.js b/lib/utils/index.js index 249097418..7f05dced4 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -52,7 +52,7 @@ const VUE2_BUILTIN_COMPONENT_NAMES = new Set( const VUE3_BUILTIN_COMPONENT_NAMES = new Set( require('./vue3-builtin-components') ) -const path = require('path') +const path = require('node:path') const vueEslintParser = require('vue-eslint-parser') const { traverseNodes, getFallbackKeys, NS } = vueEslintParser.AST const { findVariable } = require('@eslint-community/eslint-utils') @@ -567,7 +567,7 @@ module.exports = { let valueText = context.getSourceCode().getText(node.value) if ( (valueText[0] === '"' || valueText[0] === "'") && - valueText[0] === valueText[valueText.length - 1] + valueText[0] === valueText.at(-1) ) { // quoted valueText = valueText.slice(1, -1) @@ -1670,11 +1670,10 @@ module.exports = { } for (let i = 1; i <= alen; i++) { for (let j = 1; j <= blen; j++) { - if (a[i - 1] === b[j - 1]) { - dp[i][j] = dp[i - 1][j - 1] - } else { - dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 - } + dp[i][j] = + a[i - 1] === b[j - 1] + ? dp[i - 1][j - 1] + : Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 } } return dp[alen][blen] @@ -1819,7 +1818,7 @@ module.exports = { } case 'CallExpression': { if (pathNodes.length > 0 && target.callee === node) { - const mem = pathNodes[pathNodes.length - 1] + const mem = pathNodes.at(-1) const callName = getStaticPropertyName(mem) if ( callName && @@ -1944,7 +1943,7 @@ function isIterable(v) { * @param {String} filename */ function createRequire(filename) { - const Module = require('module') + const Module = require('node:module') const moduleCreateRequire = // Added in v12.2.0 Module.createRequire || @@ -2499,7 +2498,7 @@ function getVueComponentDefinitionType(node) { function isObjectArgument(node) { return ( node.arguments.length > 0 && - skipTSAsExpression(node.arguments.slice(-1)[0]).type === 'ObjectExpression' + skipTSAsExpression(node.arguments.at(-1)).type === 'ObjectExpression' ) } @@ -2557,7 +2556,7 @@ function getVueObjectType(context, node) { // Vue.component('xxx', {}) || component('xxx', {}) if ( getVueComponentDefinitionType(node) != null && - skipTSAsExpression(parent.arguments.slice(-1)[0]) === node + skipTSAsExpression(parent.arguments.at(-1)) === node ) { return 'definition' } diff --git a/lib/utils/property-references.js b/lib/utils/property-references.js index 4be7b17c7..4d4e39805 100644 --- a/lib/utils/property-references.js +++ b/lib/utils/property-references.js @@ -650,14 +650,14 @@ function definePropertyReferenceExtractor( if (ignoreRef(id.name)) { continue } - if (!isFunctionalTemplate) { - references.push( - extractFromName(id.name, id, () => extractFromExpression(id, true)) - ) - } else { + if (isFunctionalTemplate) { if (id.name === 'props') { references.push(extractFromExpression(id, true)) } + } else { + references.push( + extractFromName(id.name, id, () => extractFromExpression(id, true)) + ) } } return mergePropertyReferences(references) diff --git a/lib/utils/ref-object-references.js b/lib/utils/ref-object-references.js index ceecd89e3..b9c47585a 100644 --- a/lib/utils/ref-object-references.js +++ b/lib/utils/ref-object-references.js @@ -107,7 +107,7 @@ function* iterateDefineRefs(globalScope) { const expr = /** @type {CallExpression} */ (node) yield { node: expr, - name: path[path.length - 1] + name: path.at(-1) } } } diff --git a/lib/utils/regexp.js b/lib/utils/regexp.js index f19d55a6e..d6e210930 100644 --- a/lib/utils/regexp.js +++ b/lib/utils/regexp.js @@ -12,7 +12,7 @@ const RE_REGEXP_STR = /^\/(.+)\/(.*)$/u */ function escape(string) { return string && RE_HAS_REGEXP_CHAR.test(string) - ? string.replace(RE_REGEXP_CHAR, '\\$&') + ? string.replaceAll(RE_REGEXP_CHAR, '\\$&') : string } 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-ast-utils.js b/lib/utils/ts-ast-utils.js index e7cc52420..bef2a05a8 100644 --- a/lib/utils/ts-ast-utils.js +++ b/lib/utils/ts-ast-utils.js @@ -243,39 +243,50 @@ function resolveQualifiedType(context, node, qualifier) { */ function inferRuntimeType(context, node, checked = new Set()) { switch (node.type) { - case 'TSStringKeyword': + case 'TSStringKeyword': { 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 ['Object'] - case 'TSFunctionType': + } + case 'TSFunctionType': { return ['Function'] + } case 'TSArrayType': - case 'TSTupleType': + case 'TSTupleType': { return ['Array'] + } - 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'] } } return [`null`] - case 'TSTypeReference': + } + case 'TSTypeReference': { if (node.typeName.type === 'Identifier') { const variable = findVariable(context.getScope(), node.typeName.name) if (variable && variable.defs.length === 1) { @@ -299,8 +310,9 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Map': case 'WeakSet': case 'WeakMap': - case 'Date': + case 'Date': { return [node.typeName.name] + } case 'Record': case 'Partial': case 'Readonly': @@ -309,13 +321,15 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Exclude': case 'Extract': case 'Required': - case 'InstanceType': + case 'InstanceType': { return ['Object'] + } } } return [`null`] + } - case 'TSUnionType': + case 'TSUnionType': { const set = new Set() for (const t of node.types) { for (const tt of inferRuntimeType(context, t, checked)) { @@ -323,11 +337,14 @@ function inferRuntimeType(context, node, checked = new Set()) { } } return [...set] + } - case 'TSIntersectionType': + case 'TSIntersectionType': { return ['Object'] + } - default: - return [`null`] // no runtime check + default: { + return [`null`] + } // no runtime check } } diff --git a/tests/integrations/eslint-plugin-import.js b/tests/integrations/eslint-plugin-import.js index a408fdcdd..c4a0c1c15 100644 --- a/tests/integrations/eslint-plugin-import.js +++ b/tests/integrations/eslint-plugin-import.js @@ -5,8 +5,8 @@ */ 'use strict' -const cp = require('child_process') -const path = require('path') +const cp = require('node:child_process') +const path = require('node:path') const semver = require('semver') const ESLINT = `.${path.sep}node_modules${path.sep}.bin${path.sep}eslint` diff --git a/tests/lib/autofix.js b/tests/lib/autofix.js index bb08d0f36..a0506ccc7 100644 --- a/tests/lib/autofix.js +++ b/tests/lib/autofix.js @@ -6,7 +6,7 @@ const Linter = require('eslint').Linter const parser = require('vue-eslint-parser') -const assert = require('assert') +const assert = require('node:assert') const rules = require('../..').rules diff --git a/tests/lib/rules-without-vue-eslint-parser.js b/tests/lib/rules-without-vue-eslint-parser.js index a0c8a80a0..6a70c2e17 100644 --- a/tests/lib/rules-without-vue-eslint-parser.js +++ b/tests/lib/rules-without-vue-eslint-parser.js @@ -6,7 +6,7 @@ const Linter = require('eslint').Linter const rules = require('../..').rules -const assert = require('assert') +const assert = require('node:assert') describe("Don't crash even if without vue-eslint-parser.", () => { const code = '' diff --git a/tests/lib/rules/comment-directive.js b/tests/lib/rules/comment-directive.js index 4a8e158c3..78905aaea 100644 --- a/tests/lib/rules/comment-directive.js +++ b/tests/lib/rules/comment-directive.js @@ -5,7 +5,7 @@ 'use strict' -const assert = require('assert') +const assert = require('node:assert') const { ESLint } = require('../../eslint-compat') // Initialize linter. diff --git a/tests/lib/rules/component-tags-order.js b/tests/lib/rules/component-tags-order.js index 51ed7140b..e9d5736bf 100644 --- a/tests/lib/rules/component-tags-order.js +++ b/tests/lib/rules/component-tags-order.js @@ -5,7 +5,7 @@ const rule = require('../../../lib/rules/component-tags-order') const RuleTester = require('eslint').RuleTester -const assert = require('assert') +const assert = require('node:assert') const { ESLint } = require('../../eslint-compat') // Initialize linter. diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index 688f44f6b..e98ba1e5a 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -5,8 +5,8 @@ */ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/html-indent') diff --git a/tests/lib/rules/no-restricted-call-after-await.js b/tests/lib/rules/no-restricted-call-after-await.js index 69eaf3a42..81d1d02f4 100644 --- a/tests/lib/rules/no-restricted-call-after-await.js +++ b/tests/lib/rules/no-restricted-call-after-await.js @@ -3,7 +3,7 @@ */ 'use strict' -const path = require('path') +const path = require('node:path') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/no-restricted-call-after-await') diff --git a/tests/lib/rules/no-unused-properties.js b/tests/lib/rules/no-unused-properties.js index 8d4b2b215..676fd623f 100644 --- a/tests/lib/rules/no-unused-properties.js +++ b/tests/lib/rules/no-unused-properties.js @@ -5,7 +5,7 @@ 'use strict' const { RuleTester, Linter } = require('eslint') -const assert = require('assert') +const assert = require('node:assert') const rule = require('../../../lib/rules/no-unused-properties') const tester = new RuleTester({ diff --git a/tests/lib/rules/script-indent.js b/tests/lib/rules/script-indent.js index 3013b13e6..7e56fd9fe 100644 --- a/tests/lib/rules/script-indent.js +++ b/tests/lib/rules/script-indent.js @@ -5,8 +5,8 @@ */ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const semver = require('semver') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/script-indent') diff --git a/tests/lib/utils/casing.js b/tests/lib/utils/casing.js index dfcc70e0b..7d03fe4f4 100644 --- a/tests/lib/utils/casing.js +++ b/tests/lib/utils/casing.js @@ -1,7 +1,7 @@ 'use strict' const casing = require('../../../lib/utils/casing') -const assert = require('assert') +const assert = require('node:assert') describe('getConverter()', () => { it('should convert string to camelCase', () => { diff --git a/tests/lib/utils/comments.js b/tests/lib/utils/comments.js index 9e567f6b1..59e550a5d 100644 --- a/tests/lib/utils/comments.js +++ b/tests/lib/utils/comments.js @@ -1,6 +1,6 @@ 'use strict' -const assert = require('assert') +const assert = require('node:assert') const { isBlockComment, isJSDocComment diff --git a/tests/lib/utils/html-comments.js b/tests/lib/utils/html-comments.js index 066c73eb1..949756ec6 100644 --- a/tests/lib/utils/html-comments.js +++ b/tests/lib/utils/html-comments.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('fs') -const path = require('path') -const assert = require('assert') +const fs = require('node:fs') +const path = require('node:path') +const assert = require('node:assert') const Linter = require('eslint').Linter diff --git a/tests/lib/utils/index.js b/tests/lib/utils/index.js index 9b02ba46b..770cb2325 100644 --- a/tests/lib/utils/index.js +++ b/tests/lib/utils/index.js @@ -2,7 +2,7 @@ const espree = require('espree') const utils = require('../../../lib/utils/index') -const assert = require('assert') +const assert = require('node:assert') function parse(code) { return espree.parse(code, { ecmaVersion: 2020 }).body[0].declarations[0].init diff --git a/tests/lib/utils/ref-object-references.js b/tests/lib/utils/ref-object-references.js index 66ee89d0c..13ac39a89 100644 --- a/tests/lib/utils/ref-object-references.js +++ b/tests/lib/utils/ref-object-references.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('fs') -const path = require('path') -const assert = require('assert') +const fs = require('node:fs') +const path = require('node:path') +const assert = require('node:assert') const Linter = require('eslint').Linter diff --git a/tests/lib/utils/regexp.js b/tests/lib/utils/regexp.js index 830fa2a11..4ae39b3d1 100644 --- a/tests/lib/utils/regexp.js +++ b/tests/lib/utils/regexp.js @@ -1,7 +1,7 @@ 'use strict' const { escape, toRegExp } = require('../../../lib/utils/regexp') -const assert = require('assert') +const assert = require('node:assert') const ESCAPED = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\' const UNESCAPED = '^$.*+?()[]{}|\\' diff --git a/tests/lib/utils/selector.js b/tests/lib/utils/selector.js index 6677a9fa4..69f49c617 100644 --- a/tests/lib/utils/selector.js +++ b/tests/lib/utils/selector.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('fs') -const path = require('path') -const assert = require('assert') +const fs = require('node:fs') +const path = require('node:path') +const assert = require('node:assert') const Linter = require('eslint').Linter diff --git a/tools/lib/configs.js b/tools/lib/configs.js index 031f41e67..78b98e87c 100644 --- a/tools/lib/configs.js +++ b/tools/lib/configs.js @@ -5,8 +5,8 @@ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const ROOT = path.resolve(__dirname, '../../lib/configs') module.exports = fs diff --git a/tools/lib/rules.js b/tools/lib/rules.js index 745c63595..00a1f7b57 100644 --- a/tools/lib/rules.js +++ b/tools/lib/rules.js @@ -5,8 +5,8 @@ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const ROOT = path.resolve(__dirname, '../../lib/rules') module.exports = fs diff --git a/tools/new-rule.js b/tools/new-rule.js index 3e4e5b9ff..da98332bd 100644 --- a/tools/new-rule.js +++ b/tools/new-rule.js @@ -1,6 +1,6 @@ -const path = require('path') -const fs = require('fs') -const cp = require('child_process') +const path = require('node:path') +const fs = require('node:fs') +const cp = require('node:child_process') const logger = console // main diff --git a/tools/update-docs-rules-index.js b/tools/update-docs-rules-index.js index 3a5eb600c..970c28d8f 100644 --- a/tools/update-docs-rules-index.js +++ b/tools/update-docs-rules-index.js @@ -4,8 +4,8 @@ */ 'use strict' -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const rules = require('./lib/rules') const { getPresetIds, formatItems } = require('./lib/utils') const removedRules = require('../lib/removed-rules') diff --git a/tools/update-docs.js b/tools/update-docs.js index 75f6dab6d..6a471325c 100644 --- a/tools/update-docs.js +++ b/tools/update-docs.js @@ -18,8 +18,8 @@ For example: ``` */ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const rules = require('./lib/rules') const removedRules = require('../lib/removed-rules') const { getPresetIds, formatItems } = require('./lib/utils') @@ -75,7 +75,7 @@ class DocFile { const fileIntroPattern = /^---\n(.*\n)+---\n*/g this.content = fileIntroPattern.test(this.content) - ? this.content.replace(fileIntroPattern, computed) + ? this.content.replaceAll(fileIntroPattern, computed) : `${computed}${this.content.trim()}\n` return this @@ -158,7 +158,7 @@ class DocFile { updateCodeBlocks() { const { meta } = this.rule - this.content = this.content.replace( + this.content = this.content.replaceAll( /)\n+```/gm, '$1\n\n```' ) - this.content = this.content.replace( + this.content = this.content.replaceAll( /```\n+<\/eslint-code-block>/gm, '```\n\n' ) diff --git a/tools/update-lib-configs.js b/tools/update-lib-configs.js index f75aff4fa..726e845e8 100644 --- a/tools/update-lib-configs.js +++ b/tools/update-lib-configs.js @@ -9,8 +9,8 @@ This script updates `lib/configs/*.js` files from rule's meta data. */ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const eslint = require('eslint') const categories = require('./lib/categories') diff --git a/tools/update-lib-index.js b/tools/update-lib-index.js index f57ae1e58..c6356859d 100644 --- a/tools/update-lib-index.js +++ b/tools/update-lib-index.js @@ -9,8 +9,8 @@ This script updates `lib/index.js` file from rule's meta data. */ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const eslint = require('eslint') const rules = require('./lib/rules') const configs = require('./lib/configs') diff --git a/tools/update-no-layout-rules-config.js b/tools/update-no-layout-rules-config.js index ee4690e41..932a903bb 100644 --- a/tools/update-no-layout-rules-config.js +++ b/tools/update-no-layout-rules-config.js @@ -10,8 +10,8 @@ * and disables all layout rules */ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const rules = require('./lib/rules') const rulesToDisable = rules.filter(({ meta }) => meta.type === 'layout') diff --git a/tools/update-vue3-export-names.js b/tools/update-vue3-export-names.js index 8a3de00ea..5f96150d5 100644 --- a/tools/update-vue3-export-names.js +++ b/tools/update-vue3-export-names.js @@ -4,10 +4,10 @@ This script updates `lib/utils/vue3-export-names.json` file from vue type. */ -const fs = require('fs') -const path = require('path') -const https = require('https') -const { URL } = require('url') +const fs = require('node:fs') +const path = require('node:path') +const https = require('node:https') +const { URL } = require('node:url') const tsParser = require('@typescript-eslint/parser') main() From e4f621800d5ef837ec0666dc34490d2b969c614d Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sat, 6 May 2023 19:11:46 +0900 Subject: [PATCH 3/9] chore: set top-level await --- tools/update-lib-configs.js | 2 +- tools/update-lib-index.js | 2 +- tools/update-vue3-export-names.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/update-lib-configs.js b/tools/update-lib-configs.js index 726e845e8..0b2df3143 100644 --- a/tools/update-lib-configs.js +++ b/tools/update-lib-configs.js @@ -100,4 +100,4 @@ async function format() { eslint.ESLint.outputFixes(report) } -format() +await format() diff --git a/tools/update-lib-index.js b/tools/update-lib-index.js index c6356859d..0d5b1a56c 100644 --- a/tools/update-lib-index.js +++ b/tools/update-lib-index.js @@ -61,4 +61,4 @@ async function format() { eslint.ESLint.outputFixes(report) } -format() +await format() diff --git a/tools/update-vue3-export-names.js b/tools/update-vue3-export-names.js index 5f96150d5..389366386 100644 --- a/tools/update-vue3-export-names.js +++ b/tools/update-vue3-export-names.js @@ -10,7 +10,7 @@ const https = require('node:https') const { URL } = require('node:url') const tsParser = require('@typescript-eslint/parser') -main() +await main() async function main() { const names = new Set() From 92305cca954f42a564ca5873223e8d3b614d52d0 Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sun, 7 May 2023 12:32:22 +0900 Subject: [PATCH 4/9] Revert "chore: set top-level await" This reverts commit e4f621800d5ef837ec0666dc34490d2b969c614d. --- tools/update-lib-configs.js | 2 +- tools/update-lib-index.js | 2 +- tools/update-vue3-export-names.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/update-lib-configs.js b/tools/update-lib-configs.js index 0b2df3143..726e845e8 100644 --- a/tools/update-lib-configs.js +++ b/tools/update-lib-configs.js @@ -100,4 +100,4 @@ async function format() { eslint.ESLint.outputFixes(report) } -await format() +format() diff --git a/tools/update-lib-index.js b/tools/update-lib-index.js index 0d5b1a56c..c6356859d 100644 --- a/tools/update-lib-index.js +++ b/tools/update-lib-index.js @@ -61,4 +61,4 @@ async function format() { eslint.ESLint.outputFixes(report) } -await format() +format() diff --git a/tools/update-vue3-export-names.js b/tools/update-vue3-export-names.js index 389366386..5f96150d5 100644 --- a/tools/update-vue3-export-names.js +++ b/tools/update-vue3-export-names.js @@ -10,7 +10,7 @@ const https = require('node:https') const { URL } = require('node:url') const tsParser = require('@typescript-eslint/parser') -await main() +main() async function main() { const names = new Set() From 41b22e11ee836aefb246b00010378b550272c5d1 Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sun, 7 May 2023 12:36:27 +0900 Subject: [PATCH 5/9] chore: turn off 'unicorn/prefer-top-level-await' --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index 6792d0296..3159bb0f9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -132,6 +132,7 @@ module.exports = { 'unicorn/prefer-optional-catch-binding': 'off', // not supported by current ESLint parser version 'unicorn/prefer-module': 'off', 'unicorn/prevent-abbreviations': 'off', + 'unicorn/prefer-top-level-await': 'off', // turn off to prevent make breaking changes to source codes (ref: #2146) 'require-eslint-community': ['error'] }, From adf77e9c989800c5a78de1e3ead1514ce1fc225c Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sun, 7 May 2023 12:36:40 +0900 Subject: [PATCH 6/9] Revert "chore: run 'npm run lint:fix'" This reverts commit 27e8adad416d9f5dff09ea284d4cbe7eca583e86. --- .../no-invalid-meta-docs-categories.js | 6 +- lib/processor.js | 30 ++---- lib/rules/attributes-order.js | 48 ++++------ lib/rules/block-lang.js | 8 +- lib/rules/block-tag-newline.js | 18 ++-- lib/rules/comment-directive.js | 18 ++-- lib/rules/component-api-style.js | 2 +- lib/rules/define-emits-declaration.js | 6 +- lib/rules/define-props-declaration.js | 6 +- lib/rules/first-attribute-linebreak.js | 2 +- lib/rules/html-closing-bracket-newline.js | 9 +- lib/rules/html-self-closing.js | 2 +- lib/rules/match-component-file-name.js | 2 +- lib/rules/max-attributes-per-line.js | 2 +- lib/rules/max-len.js | 16 +++- lib/rules/multi-word-component-names.js | 2 +- .../multiline-html-element-content-newline.js | 6 +- lib/rules/no-boolean-default.js | 6 +- lib/rules/no-extra-parens.js | 10 +- lib/rules/no-multi-spaces.js | 2 +- lib/rules/no-multiple-template-root.js | 14 +-- lib/rules/no-restricted-call-after-await.js | 4 +- lib/rules/no-restricted-custom-event.js | 2 +- lib/rules/no-undef-components.js | 4 +- lib/rules/no-unused-components.js | 6 +- lib/rules/no-useless-mustaches.js | 4 +- lib/rules/no-useless-v-bind.js | 8 +- lib/rules/quote-props.js | 2 +- lib/rules/require-default-prop.js | 6 +- lib/rules/require-explicit-emits.js | 10 +- lib/rules/require-name-property.js | 2 +- lib/rules/require-prop-comment.js | 16 ++-- lib/rules/require-prop-types.js | 6 +- lib/rules/require-valid-default-prop.js | 39 ++++---- lib/rules/script-setup-uses-vars.js | 2 +- lib/rules/this-in-template.js | 2 +- lib/rules/v-for-delimiter-style.js | 2 +- lib/rules/v-on-function-call.js | 2 +- lib/rules/v-on-handler-style.js | 20 ++-- lib/rules/v-slot-style.js | 12 +-- lib/rules/valid-v-cloak.js | 2 +- lib/rules/valid-v-else-if.js | 2 +- lib/rules/valid-v-else.js | 2 +- lib/rules/valid-v-for.js | 2 +- lib/rules/valid-v-html.js | 2 +- lib/rules/valid-v-if.js | 2 +- lib/rules/valid-v-is.js | 2 +- lib/rules/valid-v-memo.js | 6 +- lib/rules/valid-v-on.js | 2 +- lib/rules/valid-v-once.js | 2 +- lib/rules/valid-v-pre.js | 2 +- lib/rules/valid-v-show.js | 2 +- lib/rules/valid-v-slot.js | 2 +- lib/rules/valid-v-text.js | 2 +- lib/utils/casing.js | 10 +- lib/utils/indent-common.js | 59 ++++++------ lib/utils/indent-ts.js | 14 +-- lib/utils/indent-utils.js | 2 +- lib/utils/index.js | 21 +++-- lib/utils/property-references.js | 10 +- lib/utils/ref-object-references.js | 2 +- lib/utils/regexp.js | 2 +- lib/utils/selector.js | 93 +++++++------------ lib/utils/ts-ast-utils.js | 53 ++++------- tests/integrations/eslint-plugin-import.js | 4 +- tests/lib/autofix.js | 2 +- tests/lib/rules-without-vue-eslint-parser.js | 2 +- tests/lib/rules/comment-directive.js | 2 +- tests/lib/rules/component-tags-order.js | 2 +- tests/lib/rules/html-indent.js | 4 +- .../rules/no-restricted-call-after-await.js | 2 +- tests/lib/rules/no-unused-properties.js | 2 +- tests/lib/rules/script-indent.js | 4 +- tests/lib/utils/casing.js | 2 +- tests/lib/utils/comments.js | 2 +- tests/lib/utils/html-comments.js | 6 +- tests/lib/utils/index.js | 2 +- tests/lib/utils/ref-object-references.js | 6 +- tests/lib/utils/regexp.js | 2 +- tests/lib/utils/selector.js | 6 +- tools/lib/configs.js | 4 +- tools/lib/rules.js | 4 +- tools/new-rule.js | 6 +- tools/update-docs-rules-index.js | 4 +- tools/update-docs.js | 12 +-- tools/update-lib-configs.js | 4 +- tools/update-lib-index.js | 4 +- tools/update-no-layout-rules-config.js | 4 +- tools/update-vue3-export-names.js | 8 +- 89 files changed, 341 insertions(+), 421 deletions(-) diff --git a/eslint-internal-rules/no-invalid-meta-docs-categories.js b/eslint-internal-rules/no-invalid-meta-docs-categories.js index 67f9789f3..24350c8f6 100644 --- a/eslint-internal-rules/no-invalid-meta-docs-categories.js +++ b/eslint-internal-rules/no-invalid-meta-docs-categories.js @@ -71,15 +71,15 @@ function checkMetaValidity(context, exportsNode) { // fixes.push(fixer.insertTextBefore(category.value, '['), fixer.insertTextAfter(category.value, ']')) // for vue3 migration - if (category.value.value === 'base') { - fixes.push(fixer.insertTextBefore(category.value, '[')) - } else { + if (category.value.value !== 'base') { fixes.push( fixer.insertTextBefore( category.value, `['vue3-${category.value.value}', ` ) ) + } else { + fixes.push(fixer.insertTextBefore(category.value, '[')) } fixes.push(fixer.insertTextAfter(category.value, ']')) } diff --git a/lib/processor.js b/lib/processor.js index 4952cca74..e0c10ef72 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -46,50 +46,40 @@ 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 226eb6df7..e6915aebf 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -120,39 +120,30 @@ 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 = @@ -163,21 +154,17 @@ 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 } @@ -185,7 +172,6 @@ function getAttributeType(attribute) { return ATTRS.ATTR_SHORTHAND_BOOL } return ATTRS.ATTR_STATIC - } } } @@ -196,9 +182,9 @@ function getAttributeType(attribute) { */ function getPosition(attribute, attributePosition) { const attributeType = getAttributeType(attribute) - return attributePosition[attributeType] == null - ? null - : attributePosition[attributeType] + return attributePosition[attributeType] != null + ? attributePosition[attributeType] + : null } /** diff --git a/lib/rules/block-lang.js b/lib/rules/block-lang.js index bc6d657fb..35898e938 100644 --- a/lib/rules/block-lang.js +++ b/lib/rules/block-lang.js @@ -42,12 +42,10 @@ const DEFAULT_LANGUAGES = { function getAllowsLangPhrase(lang) { const langs = [...lang].map((s) => `"${s}"`) switch (langs.length) { - case 1: { + case 1: return langs[0] - } - default: { - return `${langs.slice(0, -1).join(', ')}, and ${langs.at(-1)}` - } + 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 2f7c6db29..7096b2daf 100644 --- a/lib/rules/block-tag-newline.js +++ b/lib/rules/block-tag-newline.js @@ -25,12 +25,10 @@ function getLinebreakCount(text) { */ function getPhrase(lineBreaks) { switch (lineBreaks) { - case 1: { + case 1: return '1 line break' - } - default: { + default: return `${lineBreaks} line breaks` - } } } @@ -326,17 +324,17 @@ module.exports = { return (element) => { const { name } = element const elementsOptions = blocks[name] - if (elementsOptions) { + if (!elementsOptions) { + verifyElement(element, options) + } else { normalizeOptionValue({ singleline: elementsOptions.singleline || options.singleline, multiline: elementsOptions.multiline || options.multiline, maxEmptyLines: - elementsOptions.maxEmptyLines == null - ? options.maxEmptyLines - : elementsOptions.maxEmptyLines + elementsOptions.maxEmptyLines != null + ? elementsOptions.maxEmptyLines + : options.maxEmptyLines })(element) - } else { - verifyElement(element, options) } } } diff --git a/lib/rules/comment-directive.js b/lib/rules/comment-directive.js index aeca98aef..8ed085602 100644 --- a/lib/rules/comment-directive.js +++ b/lib/rules/comment-directive.js @@ -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' ? 'enableBlockRule' : 'enableLineRule', - data: { rule } + messageId: group === 'block' ? 'enableBlock' : 'enableLine' }) } else { context.report({ loc, - messageId: group === 'block' ? 'enableBlock' : 'enableLine' + messageId: group === 'block' ? 'enableBlockRule' : 'enableLineRule', + data: { rule } }) } } @@ -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' ? 'disableBlockRule' : 'disableLineRule', - data: { rule, key } + messageId: group === 'block' ? 'disableBlock' : 'disableLine', + data: { key } }) } else { context.report({ loc, - messageId: group === 'block' ? 'disableBlock' : 'disableLine', - data: { key } + messageId: group === 'block' ? 'disableBlockRule' : 'disableLineRule', + data: { rule, key } }) } } diff --git a/lib/rules/component-api-style.js b/lib/rules/component-api-style.js index 6fdcf32be..550eebfed 100644 --- a/lib/rules/component-api-style.js +++ b/lib/rules/component-api-style.js @@ -162,7 +162,7 @@ function buildAllowedPhrase(allowsOpt) { phrases.push('Options API') } return phrases.length > 2 - ? `${phrases.slice(0, -1).join(', ')} or ${phrases.at(-1)}` + ? `${phrases.slice(0, -1).join(', ')} or ${phrases.slice(-1)[0]}` : phrases.join(' or ') } diff --git a/lib/rules/define-emits-declaration.js b/lib/rules/define-emits-declaration.js index 47e7e187d..9ef9bbc03 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,9 +44,8 @@ module.exports = { }) } break - } - case 'runtime': { + case 'runtime': if (node.typeParameters && node.typeParameters.params.length > 0) { context.report({ node, @@ -54,7 +53,6 @@ module.exports = { }) } break - } } } }) diff --git a/lib/rules/define-props-declaration.js b/lib/rules/define-props-declaration.js index 6b78ac78e..f86f3ad98 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,9 +44,8 @@ module.exports = { }) } break - } - case 'runtime': { + case 'runtime': if (node.typeParameters && node.typeParameters.params.length > 0) { context.report({ node, @@ -54,7 +53,6 @@ module.exports = { }) } break - } } } }) diff --git a/lib/rules/first-attribute-linebreak.js b/lib/rules/first-attribute-linebreak.js index 11fefa44f..83f018e02 100644 --- a/lib/rules/first-attribute-linebreak.js +++ b/lib/rules/first-attribute-linebreak.js @@ -69,7 +69,7 @@ module.exports = { const firstAttribute = node.attributes[0] if (!firstAttribute) return - const lastAttribute = node.attributes.at(-1) + const lastAttribute = node.attributes[node.attributes.length - 1] const location = firstAttribute.loc.start.line === lastAttribute.loc.end.line diff --git a/lib/rules/html-closing-bracket-newline.js b/lib/rules/html-closing-bracket-newline.js index e92d45165..73afb9d69 100644 --- a/lib/rules/html-closing-bracket-newline.js +++ b/lib/rules/html-closing-bracket-newline.js @@ -12,15 +12,12 @@ 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/html-self-closing.js b/lib/rules/html-self-closing.js index 00f2c1147..19fc4b587 100644 --- a/lib/rules/html-self-closing.js +++ b/lib/rules/html-self-closing.js @@ -78,7 +78,7 @@ function getElementType(node) { */ function isEmpty(node, sourceCode) { const start = node.startTag.range[1] - const end = node.endTag == null ? node.range[1] : node.endTag.range[0] + const end = node.endTag != null ? node.endTag.range[0] : node.range[1] return sourceCode.text.slice(start, end).trim() === '' } diff --git a/lib/rules/match-component-file-name.js b/lib/rules/match-component-file-name.js index 5d92be0d1..70c34009b 100644 --- a/lib/rules/match-component-file-name.js +++ b/lib/rules/match-component-file-name.js @@ -6,7 +6,7 @@ const utils = require('../utils') const casing = require('../utils/casing') -const path = require('node:path') +const path = require('path') /** * @param {Expression | SpreadElement} node diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 7997e9bf0..0f204d500 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -51,7 +51,7 @@ function groupAttrsByLine(attributes) { const current = attributes[index] if (previous.loc.end.line === current.loc.start.line) { - propsPerLine.at(-1).push(current) + propsPerLine[propsPerLine.length - 1].push(current) } else { propsPerLine.push([current]) } diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index 2e619a154..588f2de75 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -212,7 +212,10 @@ module.exports = { const htmlAttributeValues = [] // The options object must be the last option specified… - const options = Object.assign({}, context.options.at(-1)) + const options = Object.assign( + {}, + context.options[context.options.length - 1] + ) // …but max code length… if (typeof context.options[0] === 'number') { @@ -344,15 +347,18 @@ module.exports = { scriptComments[0].loc.start.line ), Math.max( - scriptTokens.at(-1).loc.end.line, - scriptComments.at(-1).loc.end.line + scriptTokens[scriptTokens.length - 1].loc.end.line, + scriptComments[scriptComments.length - 1].loc.end.line ) ] - : [scriptTokens[0].loc.start.line, scriptTokens.at(-1).loc.end.line] + : [ + scriptTokens[0].loc.start.line, + scriptTokens[scriptTokens.length - 1].loc.end.line + ] } else if (scriptComments.length > 0) { scriptLinesRange = [ scriptComments[0].loc.start.line, - scriptComments.at(-1).loc.end.line + scriptComments[scriptComments.length - 1].loc.end.line ] } const templateLinesRange = templateBody && [ diff --git a/lib/rules/multi-word-component-names.js b/lib/rules/multi-word-component-names.js index be3388bb4..38426bfb0 100644 --- a/lib/rules/multi-word-component-names.js +++ b/lib/rules/multi-word-component-names.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('node:path') +const path = require('path') const casing = require('../utils/casing') const utils = require('../utils') diff --git a/lib/rules/multiline-html-element-content-newline.js b/lib/rules/multiline-html-element-content-newline.js index dc4111864..a8ccc3676 100644 --- a/lib/rules/multiline-html-element-content-newline.js +++ b/lib/rules/multiline-html-element-content-newline.js @@ -34,12 +34,10 @@ 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 e3cbebf2b..7dbeeceaf 100644 --- a/lib/rules/no-boolean-default.js +++ b/lib/rules/no-boolean-default.js @@ -92,16 +92,15 @@ module.exports = { */ function verifyDefaultExpression(defaultNode) { switch (booleanType) { - case 'no-default': { + case 'no-default': context.report({ node: defaultNode, message: 'Boolean prop should not set a default (Vue defaults it to false).' }) break - } - case 'default-false': { + case 'default-false': if (defaultNode.type !== 'Literal' || defaultNode.value !== false) { context.report({ node: defaultNode, @@ -109,7 +108,6 @@ module.exports = { }) } break - } } } return utils.compositingVisitors( diff --git a/lib/rules/no-extra-parens.js b/lib/rules/no-extra-parens.js index aaabb35f2..bf1ff1e46 100644 --- a/lib/rules/no-extra-parens.js +++ b/lib/rules/no-extra-parens.js @@ -97,15 +97,15 @@ function createForVueSyntax(context) { function isUnwrapChangeToFilter(expression) { let parenStack = null for (const token of tokenStore.getTokens(expression)) { - if (parenStack) { + if (!parenStack) { + if (token.value === '|') { + return true + } + } else { if (parenStack.isUpToken(token)) { parenStack = parenStack.upper continue } - } else { - if (token.value === '|') { - return true - } } if (isLeftParen(token)) { parenStack = { isUpToken: isRightParen, upper: parenStack } diff --git a/lib/rules/no-multi-spaces.js b/lib/rules/no-multi-spaces.js index f9871c3ed..e13cdff74 100644 --- a/lib/rules/no-multi-spaces.js +++ b/lib/rules/no-multi-spaces.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('node:path') +const path = require('path') /** * @param {RuleContext} context diff --git a/lib/rules/no-multiple-template-root.js b/lib/rules/no-multiple-template-root.js index e09b1e316..d5ac49b5a 100644 --- a/lib/rules/no-multiple-template-root.js +++ b/lib/rules/no-multiple-template-root.js @@ -59,7 +59,13 @@ module.exports = { loc: extraText.loc, message: 'The template root requires an element rather than texts.' }) - } else if (extraElement == null) { + } else if (extraElement != null) { + context.report({ + node: extraElement, + loc: extraElement.loc, + message: 'The template root requires exactly one element.' + }) + } else { for (const element of rootElements) { const tag = element.startTag const name = element.name @@ -80,12 +86,6 @@ module.exports = { }) } } - } else { - context.report({ - node: extraElement, - loc: extraElement.loc, - message: 'The template root requires exactly one element.' - }) } } } diff --git a/lib/rules/no-restricted-call-after-await.js b/lib/rules/no-restricted-call-after-await.js index e0bdc0eaf..d2b2a1414 100644 --- a/lib/rules/no-restricted-call-after-await.js +++ b/lib/rules/no-restricted-call-after-await.js @@ -3,8 +3,8 @@ * See LICENSE file in root directory for full license. */ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const { ReferenceTracker } = require('@eslint-community/eslint-utils') const utils = require('../utils') diff --git a/lib/rules/no-restricted-custom-event.js b/lib/rules/no-restricted-custom-event.js index 64a63d1a0..abe104b97 100644 --- a/lib/rules/no-restricted-custom-event.js +++ b/lib/rules/no-restricted-custom-event.js @@ -155,7 +155,7 @@ module.exports = { sourceCode.text[nameWithLoc.range[0]] }${JSON.stringify(option.suggest) .slice(1, -1) - .replaceAll("'", "\\'")}${ + .replace(/'/gu, "\\'")}${ sourceCode.text[nameWithLoc.range[1] - 1] }` ) diff --git a/lib/rules/no-undef-components.js b/lib/rules/no-undef-components.js index fd9f9a07c..9eb74ad05 100644 --- a/lib/rules/no-undef-components.js +++ b/lib/rules/no-undef-components.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('node:path') +const path = require('path') const utils = require('../utils') const casing = require('../utils/casing') @@ -15,7 +15,7 @@ const casing = require('../utils/casing') * @param {string} str */ function camelize(str) { - return str.replaceAll(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) + return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) } class DefinedInSetupComponents { diff --git a/lib/rules/no-unused-components.js b/lib/rules/no-unused-components.js index 0fa7d10b0..ffab616c1 100644 --- a/lib/rules/no-unused-components.js +++ b/lib/rules/no-unused-components.js @@ -33,9 +33,9 @@ module.exports = { create(context) { const options = context.options[0] || {} const ignoreWhenBindingPresent = - options.ignoreWhenBindingPresent === undefined - ? true - : options.ignoreWhenBindingPresent + options.ignoreWhenBindingPresent !== undefined + ? options.ignoreWhenBindingPresent + : true const usedComponents = new Set() /** @type { { node: Property, name: string }[] } */ let registeredComponents = [] diff --git a/lib/rules/no-useless-mustaches.js b/lib/rules/no-useless-mustaches.js index 624283051..51e3853ba 100644 --- a/lib/rules/no-useless-mustaches.js +++ b/lib/rules/no-useless-mustaches.js @@ -14,7 +14,7 @@ const utils = require('../utils') function stripQuotesForHTML(text) { if ( (text[0] === '"' || text[0] === "'" || text[0] === '`') && - text[0] === text.at(-1) + text[0] === text[text.length - 1] ) { return text.slice(1, -1) } @@ -144,7 +144,7 @@ module.exports = { return null } - return fixer.replaceText(node, text.replaceAll(/\\([\S\s])/g, '$1')) + return fixer.replaceText(node, text.replace(/\\([\S\s])/g, '$1')) } }) } diff --git a/lib/rules/no-useless-v-bind.js b/lib/rules/no-useless-v-bind.js index bbbea7c4f..9f9da6e27 100644 --- a/lib/rules/no-useless-v-bind.js +++ b/lib/rules/no-useless-v-bind.js @@ -131,13 +131,13 @@ module.exports = { let attrValue if (quoteChar === '"') { - attrValue = strValue.replaceAll(DOUBLE_QUOTES_RE, '"') + attrValue = strValue.replace(DOUBLE_QUOTES_RE, '"') } else if (quoteChar === "'") { - attrValue = strValue.replaceAll(SINGLE_QUOTES_RE, ''') + attrValue = strValue.replace(SINGLE_QUOTES_RE, ''') } else { attrValue = strValue - .replaceAll(DOUBLE_QUOTES_RE, '"') - .replaceAll(SINGLE_QUOTES_RE, ''') + .replace(DOUBLE_QUOTES_RE, '"') + .replace(SINGLE_QUOTES_RE, ''') } yield fixer.replaceText(expression, attrValue) } diff --git a/lib/rules/quote-props.js b/lib/rules/quote-props.js index 518f78a04..aabfc2bf5 100644 --- a/lib/rules/quote-props.js +++ b/lib/rules/quote-props.js @@ -40,7 +40,7 @@ module.exports = wrapCoreRule('quote-props', { )) { yield fixer.replaceTextRange( fix.range, - fix.text.replaceAll(/["']/gu, expectedQuote) + fix.text.replace(/["']/gu, expectedQuote) ) } } diff --git a/lib/rules/require-default-prop.js b/lib/rules/require-default-prop.js index acd532ce0..21ee6825e 100644 --- a/lib/rules/require-default-prop.js +++ b/lib/rules/require-default-prop.js @@ -150,9 +150,9 @@ module.exports = { continue } const propName = - prop.propName == null - ? `[${context.getSourceCode().getText(prop.node.key)}]` - : prop.propName + prop.propName != null + ? prop.propName + : `[${context.getSourceCode().getText(prop.node.key)}]` context.report({ node: prop.node, diff --git a/lib/rules/require-explicit-emits.js b/lib/rules/require-explicit-emits.js index 524ec8b8c..167613804 100644 --- a/lib/rules/require-explicit-emits.js +++ b/lib/rules/require-explicit-emits.js @@ -431,7 +431,7 @@ function buildSuggest(define, emits, nameWithLoc, context) { define.type === 'ObjectExpression' ? '`emits` option' : '`defineEmits`' const certainEmits = emits.filter((e) => e.key) if (certainEmits.length > 0) { - const last = certainEmits.at(-1) + const last = certainEmits[certainEmits.length - 1] return [ { messageId: 'addOneOption', @@ -529,7 +529,9 @@ function buildSuggest(define, emits, nameWithLoc, context) { `\nemits: ['${nameWithLoc.name}'],` ) } else if (object.properties.length > 0) { - const before = propertyNodes.at(-1) || object.properties.at(-1) + const before = + propertyNodes[propertyNodes.length - 1] || + object.properties[object.properties.length - 1] return fixer.insertTextAfter( before, `,\nemits: ['${nameWithLoc.name}']` @@ -562,7 +564,9 @@ function buildSuggest(define, emits, nameWithLoc, context) { `\nemits: {'${nameWithLoc.name}': null},` ) } else if (object.properties.length > 0) { - const before = propertyNodes.at(-1) || object.properties.at(-1) + const before = + propertyNodes[propertyNodes.length - 1] || + object.properties[object.properties.length - 1] return fixer.insertTextAfter( before, `,\nemits: {'${nameWithLoc.name}': null}` diff --git a/lib/rules/require-name-property.js b/lib/rules/require-name-property.js index 84a203a01..62eb0cfd5 100644 --- a/lib/rules/require-name-property.js +++ b/lib/rules/require-name-property.js @@ -4,7 +4,7 @@ */ 'use strict' -const path = require('node:path') +const path = require('path') const utils = require('../utils') const { getVueComponentDefinitionType } = require('../utils') /** diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js index 5a9f6f744..8dd852cbc 100644 --- a/lib/rules/require-prop-comment.js +++ b/lib/rules/require-prop-comment.js @@ -68,28 +68,26 @@ module.exports = { const precedingComments = sourceCode.getCommentsBefore(prop.node) const lastPrecedingComment = - precedingComments.length > 0 ? precedingComments.at(-1) : undefined + precedingComments.length > 0 + ? precedingComments[precedingComments.length - 1] + : undefined /** @type {string|undefined} */ 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/require-prop-types.js b/lib/rules/require-prop-types.js index e4f5f890d..e4d9fdcc1 100644 --- a/lib/rules/require-prop-types.js +++ b/lib/rules/require-prop-types.js @@ -54,7 +54,9 @@ module.exports = { const { value, node, propName } = prop let hasType = true - if (value) { + if (!value) { + hasType = false + } else { switch (value.type) { case 'ObjectExpression': { // foo: { @@ -72,8 +74,6 @@ module.exports = { break } } - } else { - hasType = false } if (!hasType) { diff --git a/lib/rules/require-valid-default-prop.js b/lib/rules/require-valid-default-prop.js index ba44b823c..50a3d191b 100644 --- a/lib/rules/require-valid-default-prop.js +++ b/lib/rules/require-valid-default-prop.js @@ -230,9 +230,9 @@ module.exports = { */ function report(node, prop, expectedTypeNames) { const propName = - prop.propName == null - ? `[${context.getSourceCode().getText(prop.node.key)}]` - : prop.propName + prop.propName != null + ? prop.propName + : `[${context.getSourceCode().getText(prop.node.key)}]` context.report({ node, message: @@ -280,7 +280,21 @@ module.exports = { if (!defType) continue - if (defType.function) { + if (!defType.function) { + if ( + typeNames.has(defType.type) && + !FUNCTION_VALUE_TYPES.has(defType.type) + ) { + continue + } + report( + defExpr, + prop, + [...typeNames].map((type) => + FUNCTION_VALUE_TYPES.has(type) ? 'Function' : type + ) + ) + } else { if (typeNames.has('Function')) { continue } @@ -296,20 +310,6 @@ module.exports = { default: defType }) } - } else { - if ( - typeNames.has(defType.type) && - !FUNCTION_VALUE_TYPES.has(defType.type) - ) { - continue - } - report( - defExpr, - prop, - [...typeNames].map((type) => - FUNCTION_VALUE_TYPES.has(type) ? 'Function' : type - ) - ) } } return propContexts @@ -415,7 +415,8 @@ module.exports = { * @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node */ ':function'(node) { - const data = scriptSetupPropsContexts.at(-1) + const data = + scriptSetupPropsContexts[scriptSetupPropsContexts.length - 1] if (!data || !scopeStack) { return } diff --git a/lib/rules/script-setup-uses-vars.js b/lib/rules/script-setup-uses-vars.js index f76c530a0..6af828177 100644 --- a/lib/rules/script-setup-uses-vars.js +++ b/lib/rules/script-setup-uses-vars.js @@ -15,7 +15,7 @@ const casing = require('../utils/casing') * @param {string} str */ function camelize(str) { - return str.replaceAll(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) + return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) } module.exports = { diff --git a/lib/rules/this-in-template.js b/lib/rules/this-in-template.js index f31ad114f..381871c42 100644 --- a/lib/rules/this-in-template.js +++ b/lib/rules/this-in-template.js @@ -30,7 +30,7 @@ module.exports = { * @returns {Object} AST event handlers. */ create(context) { - const options = context.options[0] === 'always' ? 'always' : 'never' + const options = context.options[0] !== 'always' ? 'never' : 'always' /** * @typedef {object} ScopeStack * @property {ScopeStack | null} parent diff --git a/lib/rules/v-for-delimiter-style.js b/lib/rules/v-for-delimiter-style.js index 861ef5570..f4f262100 100644 --- a/lib/rules/v-for-delimiter-style.js +++ b/lib/rules/v-for-delimiter-style.js @@ -35,7 +35,7 @@ module.exports = { const delimiterToken = /** @type {Token} */ ( tokenStore.getTokenAfter( node.left.length > 0 - ? node.left.at(-1) + ? node.left[node.left.length - 1] : tokenStore.getFirstToken(node), (token) => token.type !== 'Punctuator' || token.value !== ')' ) diff --git a/lib/rules/v-on-function-call.js b/lib/rules/v-on-function-call.js index 81b8a4b8e..e2409794b 100644 --- a/lib/rules/v-on-function-call.js +++ b/lib/rules/v-on-function-call.js @@ -162,7 +162,7 @@ module.exports = { const range = leftQuote && rightQuote ? [leftQuote.range[1], rightQuote.range[0]] - : [tokens[0].range[0], tokens.at(-1).range[1]] + : [tokens[0].range[0], tokens[tokens.length - 1].range[1]] return fixer.replaceTextRange( range, diff --git a/lib/rules/v-on-handler-style.js b/lib/rules/v-on-handler-style.js index a17ffe666..aa1214e38 100644 --- a/lib/rules/v-on-handler-style.js +++ b/lib/rules/v-on-handler-style.js @@ -179,13 +179,11 @@ 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 } @@ -198,7 +196,7 @@ module.exports = { function reportForMethodHandler(node, kind) { switch (kind) { case 'inline': - case 'inline-function': { + case 'inline-function': context.report({ node, messageId: @@ -207,7 +205,6 @@ module.exports = { : 'preferInlineFunctionOverMethod' }) return true - } } // This path is currently not taken. return false @@ -220,13 +217,11 @@ 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 } @@ -241,7 +236,7 @@ module.exports = { includeComments: true }) const firstToken = tokens[0] - const lastToken = tokens.at(-1) + const lastToken = tokens[tokens.length - 1] const hasQuote = isQuote(firstToken) /** @type {Range} */ @@ -534,9 +529,8 @@ 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 daa3d96da..1bb5f57f2 100644 --- a/lib/rules/v-slot-style.js +++ b/lib/rules/v-slot-style.js @@ -141,18 +141,14 @@ 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/rules/valid-v-cloak.js b/lib/rules/valid-v-cloak.js index 2af5c2cf3..8fe19504f 100644 --- a/lib/rules/valid-v-cloak.js +++ b/lib/rules/valid-v-cloak.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-else-if.js b/lib/rules/valid-v-else-if.js index 62893fe81..d8888534d 100644 --- a/lib/rules/valid-v-else-if.js +++ b/lib/rules/valid-v-else-if.js @@ -65,7 +65,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-else.js b/lib/rules/valid-v-else.js index 13d538541..8c7f8e49a 100644 --- a/lib/rules/valid-v-else.js +++ b/lib/rules/valid-v-else.js @@ -65,7 +65,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-for.js b/lib/rules/valid-v-for.js index 77b2c4b3e..9dcf76350 100644 --- a/lib/rules/valid-v-for.js +++ b/lib/rules/valid-v-for.js @@ -133,7 +133,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-html.js b/lib/rules/valid-v-html.js index ed222d692..af4636a50 100644 --- a/lib/rules/valid-v-html.js +++ b/lib/rules/valid-v-html.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-if.js b/lib/rules/valid-v-if.js index 5ff9e8228..b728a829b 100644 --- a/lib/rules/valid-v-if.js +++ b/lib/rules/valid-v-if.js @@ -57,7 +57,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-is.js b/lib/rules/valid-v-is.js index ef6cdd5dd..2f97d1fd9 100644 --- a/lib/rules/valid-v-is.js +++ b/lib/rules/valid-v-is.js @@ -55,7 +55,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-memo.js b/lib/rules/valid-v-memo.js index 1a038deea..66ba9a75d 100644 --- a/lib/rules/valid-v-memo.js +++ b/lib/rules/valid-v-memo.js @@ -59,7 +59,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) @@ -102,7 +102,9 @@ module.exports = { break } case 'SequenceExpression': { - expressions.push(expression.expressions.at(-1)) + expressions.push( + expression.expressions[expression.expressions.length - 1] + ) break } case 'ConditionalExpression': { diff --git a/lib/rules/valid-v-on.js b/lib/rules/valid-v-on.js index 93303e7bd..7c5471f28 100644 --- a/lib/rules/valid-v-on.js +++ b/lib/rules/valid-v-on.js @@ -118,7 +118,7 @@ module.exports = { let innerText = valueText if ( (valueText[0] === '"' || valueText[0] === "'") && - valueText[0] === valueText.at(-1) + valueText[0] === valueText[valueText.length - 1] ) { // quoted innerText = valueText.slice(1, -1) diff --git a/lib/rules/valid-v-once.js b/lib/rules/valid-v-once.js index 9756cda39..987218585 100644 --- a/lib/rules/valid-v-once.js +++ b/lib/rules/valid-v-once.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-pre.js b/lib/rules/valid-v-pre.js index f54a35e50..7cd9e30ec 100644 --- a/lib/rules/valid-v-pre.js +++ b/lib/rules/valid-v-pre.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-show.js b/lib/rules/valid-v-show.js index 2ad7d6027..c547b8c31 100644 --- a/lib/rules/valid-v-show.js +++ b/lib/rules/valid-v-show.js @@ -41,7 +41,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/rules/valid-v-slot.js b/lib/rules/valid-v-slot.js index 730c17de6..c33107966 100644 --- a/lib/rules/valid-v-slot.js +++ b/lib/rules/valid-v-slot.js @@ -377,7 +377,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'disallowAnyModifier' }) diff --git a/lib/rules/valid-v-text.js b/lib/rules/valid-v-text.js index b0115a094..c6ef676d1 100644 --- a/lib/rules/valid-v-text.js +++ b/lib/rules/valid-v-text.js @@ -39,7 +39,7 @@ module.exports = { node, loc: { start: node.key.modifiers[0].loc.start, - end: node.key.modifiers.at(-1).loc.end + end: node.key.modifiers[node.key.modifiers.length - 1].loc.end }, messageId: 'unexpectedModifier' }) diff --git a/lib/utils/casing.js b/lib/utils/casing.js index eddef6cd7..a47e978cd 100644 --- a/lib/utils/casing.js +++ b/lib/utils/casing.js @@ -27,8 +27,8 @@ function hasUpper(str) { */ function kebabCase(str) { return str - .replaceAll('_', '-') - .replaceAll(/\B([A-Z])/gu, '-$1') + .replace(/_/gu, '-') + .replace(/\B([A-Z])/gu, '-$1') .toLowerCase() } @@ -52,8 +52,8 @@ function isKebabCase(str) { */ function snakeCase(str) { return str - .replaceAll(/\B([A-Z])/gu, '_$1') - .replaceAll('-', '_') + .replace(/\B([A-Z])/gu, '_$1') + .replace(/-/gu, '_') .toLowerCase() } @@ -74,7 +74,7 @@ function camelCase(str) { if (isPascalCase(str)) { return str.charAt(0).toLowerCase() + str.slice(1) } - return str.replaceAll(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : '')) + return str.replace(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : '')) } /** diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index 983b12069..4e3edb229 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -372,7 +372,7 @@ module.exports.defineVisitor = function create( } const elementTokens = getFirstAndLastTokens( node, - lastToken == null ? 0 : lastToken.range[1] + lastToken != null ? lastToken.range[1] : 0 ) // Collect comma/comment tokens between the last token of the previous node and the first token of this node. @@ -578,15 +578,15 @@ module.exports.defineVisitor = function create( function processTopLevelNode(node, expectedIndent) { const token = tokenStore.getFirstToken(node) const offsetInfo = offsets.get(token) - if (offsetInfo == null) { + if (offsetInfo != null) { + offsetInfo.expectedIndent = expectedIndent + } else { offsets.set(token, { baseToken: null, offset: 0, baseline: false, expectedIndent }) - } else { - offsetInfo.expectedIndent = expectedIndent } } @@ -639,7 +639,9 @@ module.exports.defineVisitor = function create( const offsetInfo = offsets.get(token) if (offsetInfo != null) { - if (offsetInfo.expectedIndent == null) { + if (offsetInfo.expectedIndent != null) { + expectedIndents.push(offsetInfo.expectedIndent) + } else { const baseOffsetInfo = offsets.get(offsetInfo.baseToken) if ( baseOffsetInfo != null && @@ -654,8 +656,6 @@ module.exports.defineVisitor = function create( break } } - } else { - expectedIndents.push(offsetInfo.expectedIndent) } } } @@ -943,12 +943,7 @@ module.exports.defineVisitor = function create( }, /** @param {VElement} node */ VElement(node) { - if (PREFORMATTED_ELEMENT_NAMES.has(node.name)) { - const startTagToken = tokenStore.getFirstToken(node) - const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag) - setOffset(endTagToken, 0, startTagToken) - setPreformattedTokens(node) - } else { + if (!PREFORMATTED_ELEMENT_NAMES.has(node.name)) { const isTopLevel = node.parent.type !== 'VElement' const offset = isTopLevel ? options.baseIndent : 1 processNodeList( @@ -958,6 +953,11 @@ module.exports.defineVisitor = function create( offset, false ) + } else { + const startTagToken = tokenStore.getFirstToken(node) + const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag) + setOffset(endTagToken, 0, startTagToken) + setPreformattedTokens(node) } }, /** @param {VEndTag} node */ @@ -1050,9 +1050,9 @@ module.exports.defineVisitor = function create( ) if (closeToken != null && closeToken.type.endsWith('TagClose')) { const offset = - closeToken.type === 'HTMLSelfClosingTagClose' - ? options.closeBracket.selfClosingTag - : options.closeBracket.startTag + closeToken.type !== 'HTMLSelfClosingTagClose' + ? options.closeBracket.startTag + : options.closeBracket.selfClosingTag setOffset(closeToken, offset, openToken) } }, @@ -1082,7 +1082,7 @@ module.exports.defineVisitor = function create( 'ArrayExpression, ArrayPattern'(node) { const firstToken = tokenStore.getFirstToken(node) const rightToken = tokenStore.getTokenAfter( - node.elements.at(-1) || firstToken, + node.elements[node.elements.length - 1] || firstToken, isClosingBracketToken ) processNodeList(node.elements, firstToken, rightToken, 1) @@ -1290,7 +1290,9 @@ module.exports.defineVisitor = function create( ...tokenStore.getTokensBetween(exportToken, node.source), tokenStore.getFirstToken(node.source) ] - if (node.exported) { + if (!node.exported) { + setOffset(tokens, 1, exportToken) + } else { // export * as foo from "mod" const starToken = /** @type {Token} */ (tokens.find(isWildcard)) const asToken = tokenStore.getTokenAfter(starToken) @@ -1301,8 +1303,6 @@ module.exports.defineVisitor = function create( setOffset(asToken, 1, starToken) setOffset(exportedToken, 1, starToken) setOffset(afterTokens, 1, exportToken) - } else { - setOffset(tokens, 1, exportToken) } // assertions @@ -1492,7 +1492,7 @@ module.exports.defineVisitor = function create( ) } const rightParenToken = tokenStore.getTokenAfter( - node.params.at(-1) || leftParenToken, + node.params[node.params.length - 1] || leftParenToken, isClosingParenToken ) setOffset(leftParenToken, 1, bodyBaseToken) @@ -1563,7 +1563,10 @@ module.exports.defineVisitor = function create( if (namedSpecifiers.length > 0) { const leftBrace = tokenStore.getTokenBefore(namedSpecifiers[0]) const rightBrace = /** @type {Token} */ ( - tokenStore.getTokenAfter(namedSpecifiers.at(-1), isClosingBraceToken) + tokenStore.getTokenAfter( + namedSpecifiers[namedSpecifiers.length - 1], + isClosingBraceToken + ) ) processNodeList(namedSpecifiers, leftBrace, rightBrace, 1) for (const token of [ @@ -1716,7 +1719,7 @@ module.exports.defineVisitor = function create( 'ObjectExpression, ObjectPattern'(node) { const firstToken = tokenStore.getFirstToken(node) const rightToken = tokenStore.getTokenAfter( - node.properties.at(-1) || firstToken, + node.properties[node.properties.length - 1] || firstToken, isClosingBraceToken ) processNodeList(node.properties, firstToken, rightToken, 1) @@ -1729,11 +1732,7 @@ module.exports.defineVisitor = function create( SwitchCase(node) { const caseToken = tokenStore.getFirstToken(node) - if (node.test == null) { - const colonToken = tokenStore.getTokenAfter(caseToken) - - setOffset(colonToken, 1, caseToken) - } else { + if (node.test != null) { const testToken = tokenStore.getTokenAfter(caseToken) const colonToken = tokenStore.getTokenAfter( node.test, @@ -1741,6 +1740,10 @@ module.exports.defineVisitor = function create( ) setOffset([testToken, colonToken], 1, caseToken) + } else { + const colonToken = tokenStore.getTokenAfter(caseToken) + + setOffset(colonToken, 1, caseToken) } if ( diff --git a/lib/utils/indent-ts.js b/lib/utils/indent-ts.js index 901a9c566..c7ad7ad63 100644 --- a/lib/utils/indent-ts.js +++ b/lib/utils/indent-ts.js @@ -418,7 +418,7 @@ function defineVisitor({ const leftParenToken = currToken const rightParenToken = /**@type {Token} */ ( tokenStore.getTokenAfter( - node.params.at(-1) || leftParenToken, + node.params[node.params.length - 1] || leftParenToken, isClosingParenToken ) ) @@ -493,7 +493,7 @@ function defineVisitor({ const leftBracketToken = tokenStore.getFirstToken(node) const rightBracketToken = /**@type {Token} */ ( tokenStore.getTokenAfter( - node.parameters.at(-1) || leftBracketToken, + node.parameters[node.parameters.length - 1] || leftBracketToken, isClosingBracketToken ) ) @@ -864,7 +864,7 @@ function defineVisitor({ firstToken ) const rightParenToken = tokenStore.getTokenAfter( - node.params.at(-1) || leftParenToken, + node.params[node.params.length - 1] || leftParenToken, isClosingParenToken ) processNodeList(node.params, leftParenToken, rightParenToken, 1) @@ -923,7 +923,7 @@ function defineVisitor({ const leftParenToken = currToken const rightParenToken = /** @type {Token} */ ( tokenStore.getTokenAfter( - node.params.at(-1) || leftParenToken, + node.params[node.params.length - 1] || leftParenToken, isClosingParenToken ) ) @@ -994,7 +994,7 @@ function defineVisitor({ leftParenToken = tokenStore.getTokenAfter(node.typeParameters) } const rightParenToken = tokenStore.getTokenAfter( - node.params.at(-1) || leftParenToken, + node.params[node.params.length - 1] || leftParenToken, isClosingParenToken ) setOffset(leftParenToken, 1, bodyBaseToken) @@ -1306,7 +1306,9 @@ function defineVisitor({ } if (decorators[0] === node) { if (range[0] === node.range[0]) { - const startParentToken = tokenStore.getTokenAfter(decorators.at(-1)) + const startParentToken = tokenStore.getTokenAfter( + decorators[decorators.length - 1] + ) setOffset(startParentToken, 0, atToken) } else { const startParentToken = tokenStore.getFirstToken( diff --git a/lib/utils/indent-utils.js b/lib/utils/indent-utils.js index e5189d638..5e40ba996 100644 --- a/lib/utils/indent-utils.js +++ b/lib/utils/indent-utils.js @@ -101,7 +101,7 @@ function isPipeOperator(token) { * @returns {T | undefined} The last element or undefined. */ function last(xs) { - return xs.length === 0 ? undefined : xs.at(-1) + return xs.length === 0 ? undefined : xs[xs.length - 1] } module.exports = { diff --git a/lib/utils/index.js b/lib/utils/index.js index 7f05dced4..249097418 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -52,7 +52,7 @@ const VUE2_BUILTIN_COMPONENT_NAMES = new Set( const VUE3_BUILTIN_COMPONENT_NAMES = new Set( require('./vue3-builtin-components') ) -const path = require('node:path') +const path = require('path') const vueEslintParser = require('vue-eslint-parser') const { traverseNodes, getFallbackKeys, NS } = vueEslintParser.AST const { findVariable } = require('@eslint-community/eslint-utils') @@ -567,7 +567,7 @@ module.exports = { let valueText = context.getSourceCode().getText(node.value) if ( (valueText[0] === '"' || valueText[0] === "'") && - valueText[0] === valueText.at(-1) + valueText[0] === valueText[valueText.length - 1] ) { // quoted valueText = valueText.slice(1, -1) @@ -1670,10 +1670,11 @@ module.exports = { } for (let i = 1; i <= alen; i++) { for (let j = 1; j <= blen; j++) { - dp[i][j] = - a[i - 1] === b[j - 1] - ? dp[i - 1][j - 1] - : Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 + if (a[i - 1] === b[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + } else { + dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 + } } } return dp[alen][blen] @@ -1818,7 +1819,7 @@ module.exports = { } case 'CallExpression': { if (pathNodes.length > 0 && target.callee === node) { - const mem = pathNodes.at(-1) + const mem = pathNodes[pathNodes.length - 1] const callName = getStaticPropertyName(mem) if ( callName && @@ -1943,7 +1944,7 @@ function isIterable(v) { * @param {String} filename */ function createRequire(filename) { - const Module = require('node:module') + const Module = require('module') const moduleCreateRequire = // Added in v12.2.0 Module.createRequire || @@ -2498,7 +2499,7 @@ function getVueComponentDefinitionType(node) { function isObjectArgument(node) { return ( node.arguments.length > 0 && - skipTSAsExpression(node.arguments.at(-1)).type === 'ObjectExpression' + skipTSAsExpression(node.arguments.slice(-1)[0]).type === 'ObjectExpression' ) } @@ -2556,7 +2557,7 @@ function getVueObjectType(context, node) { // Vue.component('xxx', {}) || component('xxx', {}) if ( getVueComponentDefinitionType(node) != null && - skipTSAsExpression(parent.arguments.at(-1)) === node + skipTSAsExpression(parent.arguments.slice(-1)[0]) === node ) { return 'definition' } diff --git a/lib/utils/property-references.js b/lib/utils/property-references.js index 4d4e39805..4be7b17c7 100644 --- a/lib/utils/property-references.js +++ b/lib/utils/property-references.js @@ -650,14 +650,14 @@ function definePropertyReferenceExtractor( if (ignoreRef(id.name)) { continue } - if (isFunctionalTemplate) { - if (id.name === 'props') { - references.push(extractFromExpression(id, true)) - } - } else { + if (!isFunctionalTemplate) { references.push( extractFromName(id.name, id, () => extractFromExpression(id, true)) ) + } else { + if (id.name === 'props') { + references.push(extractFromExpression(id, true)) + } } } return mergePropertyReferences(references) diff --git a/lib/utils/ref-object-references.js b/lib/utils/ref-object-references.js index b9c47585a..ceecd89e3 100644 --- a/lib/utils/ref-object-references.js +++ b/lib/utils/ref-object-references.js @@ -107,7 +107,7 @@ function* iterateDefineRefs(globalScope) { const expr = /** @type {CallExpression} */ (node) yield { node: expr, - name: path.at(-1) + name: path[path.length - 1] } } } diff --git a/lib/utils/regexp.js b/lib/utils/regexp.js index d6e210930..f19d55a6e 100644 --- a/lib/utils/regexp.js +++ b/lib/utils/regexp.js @@ -12,7 +12,7 @@ const RE_REGEXP_STR = /^\/(.+)\/(.*)$/u */ function escape(string) { return string && RE_HAS_REGEXP_CHAR.test(string) - ? string.replaceAll(RE_REGEXP_CHAR, '\\$&') + ? string.replace(RE_REGEXP_CHAR, '\\$&') : string } diff --git a/lib/utils/selector.js b/lib/utils/selector.js index 67960d59a..862f695fb 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,8 +182,7 @@ function combination(left, combinator, right) { } return false } - } - case '>': { + case '>': // child return (element, subject) => { if (right(element, null)) { @@ -194,8 +193,7 @@ function combination(left, combinator, right) { } return false } - } - case '+': { + case '+': // adjacent return (element, subject) => { if (right(element, null)) { @@ -206,8 +204,7 @@ function combination(left, combinator, right) { } return false } - } - case '~': { + case '~': // sibling return (element, subject) => { if (right(element, null)) { @@ -219,10 +216,8 @@ function combination(left, combinator, right) { } return false } - } - default: { + default: throw new SelectorError(`Unknown combinator: ${combinator}.`) - } } } @@ -233,35 +228,26 @@ 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}.` ) - } } } @@ -278,37 +264,30 @@ 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}.`) - } } /** @@ -397,22 +376,19 @@ 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) @@ -425,22 +401,19 @@ 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) @@ -453,25 +426,21 @@ 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-ast-utils.js b/lib/utils/ts-ast-utils.js index bef2a05a8..e7cc52420 100644 --- a/lib/utils/ts-ast-utils.js +++ b/lib/utils/ts-ast-utils.js @@ -243,50 +243,39 @@ function resolveQualifiedType(context, node, qualifier) { */ function inferRuntimeType(context, node, checked = new Set()) { switch (node.type) { - case 'TSStringKeyword': { + case 'TSStringKeyword': 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 ['Object'] - } - case 'TSFunctionType': { + case 'TSFunctionType': return ['Function'] - } case 'TSArrayType': - case 'TSTupleType': { + case 'TSTupleType': return ['Array'] - } - 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'] } } return [`null`] - } - case 'TSTypeReference': { + case 'TSTypeReference': if (node.typeName.type === 'Identifier') { const variable = findVariable(context.getScope(), node.typeName.name) if (variable && variable.defs.length === 1) { @@ -310,9 +299,8 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Map': case 'WeakSet': case 'WeakMap': - case 'Date': { + case 'Date': return [node.typeName.name] - } case 'Record': case 'Partial': case 'Readonly': @@ -321,15 +309,13 @@ function inferRuntimeType(context, node, checked = new Set()) { case 'Exclude': case 'Extract': case 'Required': - case 'InstanceType': { + case 'InstanceType': return ['Object'] - } } } return [`null`] - } - case 'TSUnionType': { + case 'TSUnionType': const set = new Set() for (const t of node.types) { for (const tt of inferRuntimeType(context, t, checked)) { @@ -337,14 +323,11 @@ function inferRuntimeType(context, node, checked = new Set()) { } } return [...set] - } - case 'TSIntersectionType': { + case 'TSIntersectionType': return ['Object'] - } - default: { - return [`null`] - } // no runtime check + default: + return [`null`] // no runtime check } } diff --git a/tests/integrations/eslint-plugin-import.js b/tests/integrations/eslint-plugin-import.js index c4a0c1c15..a408fdcdd 100644 --- a/tests/integrations/eslint-plugin-import.js +++ b/tests/integrations/eslint-plugin-import.js @@ -5,8 +5,8 @@ */ 'use strict' -const cp = require('node:child_process') -const path = require('node:path') +const cp = require('child_process') +const path = require('path') const semver = require('semver') const ESLINT = `.${path.sep}node_modules${path.sep}.bin${path.sep}eslint` diff --git a/tests/lib/autofix.js b/tests/lib/autofix.js index a0506ccc7..bb08d0f36 100644 --- a/tests/lib/autofix.js +++ b/tests/lib/autofix.js @@ -6,7 +6,7 @@ const Linter = require('eslint').Linter const parser = require('vue-eslint-parser') -const assert = require('node:assert') +const assert = require('assert') const rules = require('../..').rules diff --git a/tests/lib/rules-without-vue-eslint-parser.js b/tests/lib/rules-without-vue-eslint-parser.js index 6a70c2e17..a0c8a80a0 100644 --- a/tests/lib/rules-without-vue-eslint-parser.js +++ b/tests/lib/rules-without-vue-eslint-parser.js @@ -6,7 +6,7 @@ const Linter = require('eslint').Linter const rules = require('../..').rules -const assert = require('node:assert') +const assert = require('assert') describe("Don't crash even if without vue-eslint-parser.", () => { const code = '' diff --git a/tests/lib/rules/comment-directive.js b/tests/lib/rules/comment-directive.js index 78905aaea..4a8e158c3 100644 --- a/tests/lib/rules/comment-directive.js +++ b/tests/lib/rules/comment-directive.js @@ -5,7 +5,7 @@ 'use strict' -const assert = require('node:assert') +const assert = require('assert') const { ESLint } = require('../../eslint-compat') // Initialize linter. diff --git a/tests/lib/rules/component-tags-order.js b/tests/lib/rules/component-tags-order.js index e9d5736bf..51ed7140b 100644 --- a/tests/lib/rules/component-tags-order.js +++ b/tests/lib/rules/component-tags-order.js @@ -5,7 +5,7 @@ const rule = require('../../../lib/rules/component-tags-order') const RuleTester = require('eslint').RuleTester -const assert = require('node:assert') +const assert = require('assert') const { ESLint } = require('../../eslint-compat') // Initialize linter. diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index e98ba1e5a..688f44f6b 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -5,8 +5,8 @@ */ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/html-indent') diff --git a/tests/lib/rules/no-restricted-call-after-await.js b/tests/lib/rules/no-restricted-call-after-await.js index 81d1d02f4..69eaf3a42 100644 --- a/tests/lib/rules/no-restricted-call-after-await.js +++ b/tests/lib/rules/no-restricted-call-after-await.js @@ -3,7 +3,7 @@ */ 'use strict' -const path = require('node:path') +const path = require('path') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/no-restricted-call-after-await') diff --git a/tests/lib/rules/no-unused-properties.js b/tests/lib/rules/no-unused-properties.js index 676fd623f..8d4b2b215 100644 --- a/tests/lib/rules/no-unused-properties.js +++ b/tests/lib/rules/no-unused-properties.js @@ -5,7 +5,7 @@ 'use strict' const { RuleTester, Linter } = require('eslint') -const assert = require('node:assert') +const assert = require('assert') const rule = require('../../../lib/rules/no-unused-properties') const tester = new RuleTester({ diff --git a/tests/lib/rules/script-indent.js b/tests/lib/rules/script-indent.js index 7e56fd9fe..3013b13e6 100644 --- a/tests/lib/rules/script-indent.js +++ b/tests/lib/rules/script-indent.js @@ -5,8 +5,8 @@ */ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const semver = require('semver') const RuleTester = require('eslint').RuleTester const rule = require('../../../lib/rules/script-indent') diff --git a/tests/lib/utils/casing.js b/tests/lib/utils/casing.js index 7d03fe4f4..dfcc70e0b 100644 --- a/tests/lib/utils/casing.js +++ b/tests/lib/utils/casing.js @@ -1,7 +1,7 @@ 'use strict' const casing = require('../../../lib/utils/casing') -const assert = require('node:assert') +const assert = require('assert') describe('getConverter()', () => { it('should convert string to camelCase', () => { diff --git a/tests/lib/utils/comments.js b/tests/lib/utils/comments.js index 59e550a5d..9e567f6b1 100644 --- a/tests/lib/utils/comments.js +++ b/tests/lib/utils/comments.js @@ -1,6 +1,6 @@ 'use strict' -const assert = require('node:assert') +const assert = require('assert') const { isBlockComment, isJSDocComment diff --git a/tests/lib/utils/html-comments.js b/tests/lib/utils/html-comments.js index 949756ec6..066c73eb1 100644 --- a/tests/lib/utils/html-comments.js +++ b/tests/lib/utils/html-comments.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('node:fs') -const path = require('node:path') -const assert = require('node:assert') +const fs = require('fs') +const path = require('path') +const assert = require('assert') const Linter = require('eslint').Linter diff --git a/tests/lib/utils/index.js b/tests/lib/utils/index.js index 770cb2325..9b02ba46b 100644 --- a/tests/lib/utils/index.js +++ b/tests/lib/utils/index.js @@ -2,7 +2,7 @@ const espree = require('espree') const utils = require('../../../lib/utils/index') -const assert = require('node:assert') +const assert = require('assert') function parse(code) { return espree.parse(code, { ecmaVersion: 2020 }).body[0].declarations[0].init diff --git a/tests/lib/utils/ref-object-references.js b/tests/lib/utils/ref-object-references.js index 13ac39a89..66ee89d0c 100644 --- a/tests/lib/utils/ref-object-references.js +++ b/tests/lib/utils/ref-object-references.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('node:fs') -const path = require('node:path') -const assert = require('node:assert') +const fs = require('fs') +const path = require('path') +const assert = require('assert') const Linter = require('eslint').Linter diff --git a/tests/lib/utils/regexp.js b/tests/lib/utils/regexp.js index 4ae39b3d1..830fa2a11 100644 --- a/tests/lib/utils/regexp.js +++ b/tests/lib/utils/regexp.js @@ -1,7 +1,7 @@ 'use strict' const { escape, toRegExp } = require('../../../lib/utils/regexp') -const assert = require('node:assert') +const assert = require('assert') const ESCAPED = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\' const UNESCAPED = '^$.*+?()[]{}|\\' diff --git a/tests/lib/utils/selector.js b/tests/lib/utils/selector.js index 69f49c617..6677a9fa4 100644 --- a/tests/lib/utils/selector.js +++ b/tests/lib/utils/selector.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('node:fs') -const path = require('node:path') -const assert = require('node:assert') +const fs = require('fs') +const path = require('path') +const assert = require('assert') const Linter = require('eslint').Linter diff --git a/tools/lib/configs.js b/tools/lib/configs.js index 78b98e87c..031f41e67 100644 --- a/tools/lib/configs.js +++ b/tools/lib/configs.js @@ -5,8 +5,8 @@ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const ROOT = path.resolve(__dirname, '../../lib/configs') module.exports = fs diff --git a/tools/lib/rules.js b/tools/lib/rules.js index 00a1f7b57..745c63595 100644 --- a/tools/lib/rules.js +++ b/tools/lib/rules.js @@ -5,8 +5,8 @@ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const ROOT = path.resolve(__dirname, '../../lib/rules') module.exports = fs diff --git a/tools/new-rule.js b/tools/new-rule.js index da98332bd..3e4e5b9ff 100644 --- a/tools/new-rule.js +++ b/tools/new-rule.js @@ -1,6 +1,6 @@ -const path = require('node:path') -const fs = require('node:fs') -const cp = require('node:child_process') +const path = require('path') +const fs = require('fs') +const cp = require('child_process') const logger = console // main diff --git a/tools/update-docs-rules-index.js b/tools/update-docs-rules-index.js index 970c28d8f..3a5eb600c 100644 --- a/tools/update-docs-rules-index.js +++ b/tools/update-docs-rules-index.js @@ -4,8 +4,8 @@ */ 'use strict' -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const rules = require('./lib/rules') const { getPresetIds, formatItems } = require('./lib/utils') const removedRules = require('../lib/removed-rules') diff --git a/tools/update-docs.js b/tools/update-docs.js index 6a471325c..75f6dab6d 100644 --- a/tools/update-docs.js +++ b/tools/update-docs.js @@ -18,8 +18,8 @@ For example: ``` */ -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const rules = require('./lib/rules') const removedRules = require('../lib/removed-rules') const { getPresetIds, formatItems } = require('./lib/utils') @@ -75,7 +75,7 @@ class DocFile { const fileIntroPattern = /^---\n(.*\n)+---\n*/g this.content = fileIntroPattern.test(this.content) - ? this.content.replaceAll(fileIntroPattern, computed) + ? this.content.replace(fileIntroPattern, computed) : `${computed}${this.content.trim()}\n` return this @@ -158,7 +158,7 @@ class DocFile { updateCodeBlocks() { const { meta } = this.rule - this.content = this.content.replaceAll( + this.content = this.content.replace( /)\n+```/gm, '$1\n\n```' ) - this.content = this.content.replaceAll( + this.content = this.content.replace( /```\n+<\/eslint-code-block>/gm, '```\n\n' ) diff --git a/tools/update-lib-configs.js b/tools/update-lib-configs.js index 726e845e8..f75aff4fa 100644 --- a/tools/update-lib-configs.js +++ b/tools/update-lib-configs.js @@ -9,8 +9,8 @@ This script updates `lib/configs/*.js` files from rule's meta data. */ -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const eslint = require('eslint') const categories = require('./lib/categories') diff --git a/tools/update-lib-index.js b/tools/update-lib-index.js index c6356859d..f57ae1e58 100644 --- a/tools/update-lib-index.js +++ b/tools/update-lib-index.js @@ -9,8 +9,8 @@ This script updates `lib/index.js` file from rule's meta data. */ -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const eslint = require('eslint') const rules = require('./lib/rules') const configs = require('./lib/configs') diff --git a/tools/update-no-layout-rules-config.js b/tools/update-no-layout-rules-config.js index 932a903bb..ee4690e41 100644 --- a/tools/update-no-layout-rules-config.js +++ b/tools/update-no-layout-rules-config.js @@ -10,8 +10,8 @@ * and disables all layout rules */ -const fs = require('node:fs') -const path = require('node:path') +const fs = require('fs') +const path = require('path') const rules = require('./lib/rules') const rulesToDisable = rules.filter(({ meta }) => meta.type === 'layout') diff --git a/tools/update-vue3-export-names.js b/tools/update-vue3-export-names.js index 5f96150d5..8a3de00ea 100644 --- a/tools/update-vue3-export-names.js +++ b/tools/update-vue3-export-names.js @@ -4,10 +4,10 @@ This script updates `lib/utils/vue3-export-names.json` file from vue type. */ -const fs = require('node:fs') -const path = require('node:path') -const https = require('node:https') -const { URL } = require('node:url') +const fs = require('fs') +const path = require('path') +const https = require('https') +const { URL } = require('url') const tsParser = require('@typescript-eslint/parser') main() From f29cc81ccd156ddfbe276e731190fde9ff55cf73 Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Sun, 7 May 2023 12:42:36 +0900 Subject: [PATCH 7/9] chore: turn off lint rules --- .eslintrc.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3159bb0f9..2b7e386a6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -128,11 +128,17 @@ 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', // turn off to prevent make breaking changes (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-top-level-await': 'off', // turn off to prevent make breaking changes to source codes (ref: #2146) + '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', // turn off to prevent make breaking changes (ref: #2146) + 'unicorn/prefer-top-level-await': 'off', // turn off to prevent make breaking changes (ref: #2146) + 'unicorn/switch-case-braces': 'off', // turn off to prevent make breaking changes (ref: #2146) 'require-eslint-community': ['error'] }, From 49e398fe2ea87d6b98db5215390d13404eb45c6e Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Mon, 8 May 2023 01:03:45 +0900 Subject: [PATCH 8/9] Update .eslintrc.js --- .eslintrc.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index cbe93f428..2b7e386a6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -140,11 +140,7 @@ module.exports = { 'unicorn/prefer-top-level-await': 'off', // turn off to prevent make breaking changes (ref: #2146) 'unicorn/switch-case-braces': 'off', // turn off to prevent make breaking changes (ref: #2146) - 'require-eslint-community': ['error'], - - // FIXME: The version we are currently using is not compatible. - // May be removed in #2146. https://github.com/vuejs/eslint-plugin-vue/pull/2146 - 'unicorn/expiring-todo-comments': 'off' + 'require-eslint-community': ['error'] }, overrides: [ { From 6f4dca6ea72de8cb9eac45e84ec1c2a0898927fc Mon Sep 17 00:00:00 2001 From: TatsuyaYamamoto Date: Mon, 8 May 2023 23:19:50 +0900 Subject: [PATCH 9/9] chore: edit comments --- .eslintrc.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2b7e386a6..e7f17d88b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -128,7 +128,7 @@ 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', // turn off to prevent make breaking changes (ref: #2146) + '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', @@ -136,9 +136,9 @@ module.exports = { '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', // 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', // turn off to prevent make breaking changes (ref: #2146) + 'unicorn/switch-case-braces': 'off', // remove when there are few pull requests (ref: #2146) 'require-eslint-community': ['error'] },