Skip to content

Meta: Install eslint-plugin-unicorn #1857

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ module.exports = {
'plugin:eslint-plugin/recommended',
'prettier',
'plugin:node-dependencies/recommended',
'plugin:jsonc/recommended-with-jsonc'
'plugin:jsonc/recommended-with-jsonc',
'plugin:unicorn/recommended'
],
plugins: ['eslint-plugin', 'prettier'],
plugins: ['eslint-plugin', 'prettier', 'unicorn'],
rules: {
'accessor-pairs': 2,
camelcase: [2, { properties: 'never' }],
Expand Down Expand Up @@ -117,7 +118,19 @@ module.exports = {
'prefer-arrow-callback': 'error',
'prefer-spread': 'error',

'dot-notation': 'error'
'dot-notation': 'error',

'unicorn/consistent-function-scoping': [
'error',
{ checkArrowFunctions: false }
],
'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-useless-undefined': 'off',
'unicorn/prefer-optional-catch-binding': 'off', // not supported by current ESLint parser version
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off'
},
overrides: [
{
Expand Down
28 changes: 13 additions & 15 deletions docs/.vuepress/enhanceApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ export default (
// siteData, // site metadata
}
) => {
if (typeof window !== 'undefined') {
if (typeof window.process === 'undefined') {
window.process = new Proxy(
{
env: {},
cwd: () => undefined
},
{
get(target, name) {
// For debug
// console.log(name)
return target[name]
}
if (typeof window !== 'undefined' && typeof window.process === 'undefined') {
window.process = new Proxy(
{
env: {},
cwd: () => undefined
},
{
get(target, name) {
// For debug
// console.log(name)
return target[name]
}
)
}
}
)
}
}
10 changes: 5 additions & 5 deletions eslint-internal-rules/no-invalid-meta-docs-categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
/**
* Gets the property of the Object node passed in that has the name specified.
*
* @param {string} property Name of the property to return.
* @param {string} propertyName Name of the property to return.
* @param {ASTNode} node The ObjectExpression node.
* @returns {ASTNode} The Property node or null if not found.
*/
function getPropertyFromObject(property, node) {
function getPropertyFromObject(propertyName, node) {
if (node && node.type === 'ObjectExpression') {
const properties = node.properties

for (let i = 0; i < properties.length; i++) {
if (properties[i].key.name === property) {
return properties[i]
for (const property of properties) {
if (property.key.name === propertyName) {
return property
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions eslint-internal-rules/no-invalid-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
/**
* Gets the property of the Object node passed in that has the name specified.
*
* @param {string} property Name of the property to return.
* @param {string} propertyName Name of the property to return.
* @param {ASTNode} node The ObjectExpression node.
* @returns {ASTNode} The Property node or null if not found.
*/
function getPropertyFromObject(property, node) {
function getPropertyFromObject(propertyName, node) {
if (node && node.type === 'ObjectExpression') {
const properties = node.properties

for (let i = 0; i < properties.length; i++) {
if (properties[i].key.name === property) {
return properties[i]
for (const property of properties) {
if (property.key.name === propertyName) {
return property
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ module.exports = {
return false
} else {
const disableDirectiveKeys = []
if (state.block.disableAllKeys.size) {
if (state.block.disableAllKeys.size > 0) {
disableDirectiveKeys.push(...state.block.disableAllKeys)
}
if (state.line.disableAllKeys.size) {
if (state.line.disableAllKeys.size > 0) {
disableDirectiveKeys.push(...state.line.disableAllKeys)
}
if (message.ruleId) {
Expand All @@ -101,7 +101,7 @@ module.exports = {
}
}

if (disableDirectiveKeys.length) {
if (disableDirectiveKeys.length > 0) {
// Store used eslint-disable comment key
usedDisableDirectiveKeys.push(...disableDirectiveKeys)
return false
Expand All @@ -111,7 +111,7 @@ module.exports = {
}
})

if (unusedDisableDirectiveReports.size) {
if (unusedDisableDirectiveReports.size > 0) {
for (const key of usedDisableDirectiveKeys) {
// Remove used eslint-disable comments
unusedDisableDirectiveReports.delete(key)
Expand Down
36 changes: 24 additions & 12 deletions lib/rules/attribute-hyphenation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ const svgAttributes = require('../utils/svg-attributes-weird-case.json')
// Rule Definition
// ------------------------------------------------------------------------------

/**
* @param {VDirective | VAttribute} node
* @returns {string | null}
*/
function getAttributeName(node) {
if (!node.directive) {
return node.key.rawName
}

if (
node.key.name.name === 'bind' &&
node.key.argument &&
node.key.argument.type === 'VIdentifier'
) {
return node.key.argument.rawName
}

return null
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -52,12 +72,10 @@ module.exports = {
const option = context.options[0]
const optionsPayload = context.options[1]
const useHyphenated = option !== 'never'
let ignoredAttributes = ['data-', 'aria-', 'slot-scope'].concat(
svgAttributes
)
const ignoredAttributes = ['data-', 'aria-', 'slot-scope', ...svgAttributes]

if (optionsPayload && optionsPayload.ignore) {
ignoredAttributes = ignoredAttributes.concat(optionsPayload.ignore)
ignoredAttributes.push(...optionsPayload.ignore)
}

const caseConverter = casing.getExactConverter(
Expand Down Expand Up @@ -112,14 +130,8 @@ module.exports = {
)
return

const name = !node.directive
? node.key.rawName
: node.key.name.name === 'bind'
? node.key.argument &&
node.key.argument.type === 'VIdentifier' &&
node.key.argument.rawName
: /* otherwise */ false
if (!name || isIgnoredAttribute(name)) return
const name = getAttributeName(node)
if (name === null || isIgnoredAttribute(name)) return

reportIssue(node, name)
}
Expand Down
79 changes: 41 additions & 38 deletions lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,31 @@ function getAttributeType(attribute) {
if (attribute.directive) {
if (!isVBind(attribute)) {
const name = attribute.key.name.name
if (name === 'for') {
return ATTRS.LIST_RENDERING
} else if (
name === 'if' ||
name === 'else-if' ||
name === 'else' ||
name === 'show' ||
name === 'cloak'
) {
return ATTRS.CONDITIONALS
} else if (name === 'pre' || name === 'once') {
return ATTRS.RENDER_MODIFIERS
} else if (name === 'model') {
return ATTRS.TWO_WAY_BINDING
} else if (name === 'on') {
return ATTRS.EVENTS
} else if (name === 'html' || name === 'text') {
return ATTRS.CONTENT
} else if (name === 'slot') {
return ATTRS.SLOT
} else if (name === 'is') {
return ATTRS.DEFINITION
} else {
return ATTRS.OTHER_DIRECTIVES
switch (name) {
case 'for':
return ATTRS.LIST_RENDERING
case 'if':
case 'else-if':
case 'else':
case 'show':
case 'cloak':
return ATTRS.CONDITIONALS
case 'pre':
case 'once':
return ATTRS.RENDER_MODIFIERS
case 'model':
return ATTRS.TWO_WAY_BINDING
case 'on':
return ATTRS.EVENTS
case 'html':
case 'text':
return ATTRS.CONTENT
case 'slot':
return ATTRS.SLOT
case 'is':
return ATTRS.DEFINITION
default:
return ATTRS.OTHER_DIRECTIVES
}
}
propName =
Expand All @@ -144,16 +145,19 @@ function getAttributeType(attribute) {
} else {
propName = attribute.key.name
}
if (propName === 'is') {
return ATTRS.DEFINITION
} else if (propName === 'id') {
return ATTRS.GLOBAL
} else if (propName === 'ref' || propName === 'key') {
return ATTRS.UNIQUE
} else if (propName === 'slot' || propName === 'slot-scope') {
return ATTRS.SLOT
} else {
return ATTRS.OTHER_ATTR
switch (propName) {
case 'is':
return ATTRS.DEFINITION
case 'id':
return ATTRS.GLOBAL
case 'ref':
case 'key':
return ATTRS.UNIQUE
case 'slot':
case 'slot-scope':
return ATTRS.SLOT
default:
return ATTRS.OTHER_ATTR
}
}

Expand Down Expand Up @@ -213,13 +217,13 @@ function create(context) {

/** @type { { [key: string]: number } } */
const attributePosition = {}
attributeOrder.forEach((item, i) => {
for (const [i, item] of attributeOrder.entries()) {
if (Array.isArray(item)) {
for (const attr of item) {
attributePosition[attr] = i
}
} else attributePosition[item] = i
})
}

/**
* @param {VAttribute | VDirective} node
Expand Down Expand Up @@ -319,8 +323,7 @@ function create(context) {
})

const results = []
for (let index = 0; index < attributes.length; index++) {
const attr = attributes[index]
for (const [index, attr] of attributes.entries()) {
const position = getPositionFromAttrIndex(index)
if (position == null) {
// The omitted order is skipped.
Expand Down
27 changes: 16 additions & 11 deletions lib/rules/block-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@ function getAllowsLangPhrase(lang) {
/**
* Normalizes a given option.
* @param {string} blockName The block name.
* @param { UserBlockOptions } option An option to parse.
* @param {UserBlockOptions} option An option to parse.
* @returns {BlockOptions} Normalized option.
*/
function normalizeOption(blockName, option) {
const lang = new Set(
Array.isArray(option.lang) ? option.lang : option.lang ? [option.lang] : []
)
/** @type {Set<string>} */
let lang

if (Array.isArray(option.lang)) {
lang = new Set(option.lang)
} else if (typeof option.lang === 'string') {
lang = new Set([option.lang])
} else {
lang = new Set()
}

let hasDefault = false
for (const def of DEFAULT_LANGUAGES[blockName] || []) {
if (lang.has(def)) {
Expand Down Expand Up @@ -165,8 +173,7 @@ module.exports = {
style: { allowNoLang: true }
}
)
if (!Object.keys(options).length) {
// empty
if (Object.keys(options).length === 0) {
return {}
}

Expand Down Expand Up @@ -198,11 +205,9 @@ module.exports = {
if (!option.allowNoLang) {
messageId = 'expected'
} else if (option.lang.size === 0) {
if ((DEFAULT_LANGUAGES[tag] || []).includes(lang.value.value)) {
messageId = 'unexpectedDefault'
} else {
messageId = 'unexpected'
}
messageId = (DEFAULT_LANGUAGES[tag] || []).includes(lang.value.value)
? 'unexpectedDefault'
: 'unexpected'
} else {
messageId = 'useOrNot'
}
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/block-tag-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,8 @@ module.exports = {
}

const option =
options.multiline === options.singleline
? options.singleline
: /[\n\r\u2028\u2029]/u.test(text.trim())
options.multiline !== options.singleline &&
/[\n\r\u2028\u2029]/u.test(text.trim())
? options.multiline
: options.singleline
if (option === 'ignore') {
Expand Down
Loading