Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jun 12, 2020
1 parent 204cca9 commit e049316
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 340 deletions.
16 changes: 7 additions & 9 deletions lib/rules/comment-directive.js
Expand Up @@ -5,6 +5,12 @@

'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')

/**
* @typedef {object} RuleAndLocation
* @property {string} RuleAndLocation.ruleId
Expand Down Expand Up @@ -252,15 +258,7 @@ function locToKey(location) {
* @returns {VElement[]} The top-level elements
*/
function extractTopLevelHTMLElements(documentFragment) {
return documentFragment.children.filter(isVElement)

/**
* @param {any} e
* @returns {e is VElement}
*/
function isVElement(e) {
return e.type === 'VElement'
}
return documentFragment.children.filter(utils.isVElement)
}
/**
* Extracts the top-level comments in document fragment.
Expand Down
12 changes: 2 additions & 10 deletions lib/rules/component-definition-name-casing.js
Expand Up @@ -97,18 +97,10 @@ module.exports = {
}
}),
utils.executeOnVue(context, (obj) => {
const node = obj.properties.find(
/**
* @param {ObjectExpression['properties'][0]} item
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
*/
(item) =>
item.type === 'Property' &&
utils.getStaticPropertyName(item) === 'name' &&
canConvert(item.value)
)
const node = utils.findProperty(obj, 'name')

if (!node) return
if (!canConvert(node.value)) return
convertName(node.value)
})
)
Expand Down
8 changes: 1 addition & 7 deletions lib/rules/component-tags-order.js
Expand Up @@ -81,13 +81,7 @@ module.exports = {

function getTopLevelHTMLElements() {
if (documentFragment) {
return documentFragment.children.filter(
/**
* @param {VElement | VText | VExpressionContainer} e
* @returns {e is VElement}
*/
(e) => e.type === 'VElement'
)
return documentFragment.children.filter(utils.isVElement)
}
return []
}
Expand Down
11 changes: 3 additions & 8 deletions lib/rules/custom-event-name-casing.js
Expand Up @@ -131,14 +131,9 @@ module.exports = {
const contextReferenceIds = new Set()
const emitReferenceIds = new Set()
if (contextParam.type === 'ObjectPattern') {
const emitProperty = contextParam.properties.find(
/**
* @param {ESNode} p
* @returns {p is AssignmentProperty}
*/
(p) =>
p.type === 'Property' &&
utils.getStaticPropertyName(p) === 'emit'
const emitProperty = utils.findAssignmentProperty(
contextParam,
'emit'
)
if (!emitProperty || emitProperty.value.type !== 'Identifier') {
return
Expand Down
12 changes: 2 additions & 10 deletions lib/rules/match-component-file-name.js
Expand Up @@ -131,20 +131,12 @@ module.exports = {
}
}),
utils.executeOnVue(context, (object) => {
const node = object.properties.find(
/**
* @param {ObjectExpression['properties'][0]} item
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
*/
(item) =>
item.type === 'Property' &&
utils.getStaticPropertyName(item) === 'name' &&
canVerify(item.value)
)
const node = utils.findProperty(object, 'name')

componentCount++

if (!node) return
if (!canVerify(node.value)) return
verifyName(node.value)
}),
{
Expand Down
27 changes: 10 additions & 17 deletions lib/rules/name-property-casing.js
Expand Up @@ -41,35 +41,28 @@ module.exports = {
// ----------------------------------------------------------------------

return utils.executeOnVue(context, (obj) => {
const node = obj.properties.find(
/**
* @param {ESNode} item
* @returns {item is Property & { value : Literal } }
*/
(item) =>
item.type === 'Property' &&
utils.getStaticPropertyName(item) === 'name' &&
item.value.type === 'Literal'
)
const node = utils.findProperty(obj, 'name')

if (!node) return
const valueNode = node.value
if (valueNode.type !== 'Literal') return

if (!casing.getChecker(caseType)(`${node.value.value}`)) {
const value = casing.getExactConverter(caseType)(`${node.value.value}`)
if (!casing.getChecker(caseType)(`${valueNode.value}`)) {
const value = casing.getExactConverter(caseType)(`${valueNode.value}`)
context.report({
node: node.value,
node: valueNode,
message: 'Property name "{{value}}" is not {{caseType}}.',
data: {
value: `${node.value.value}`,
value: `${valueNode.value}`,
caseType
},
fix: (fixer) =>
fixer.replaceText(
node.value,
valueNode,
context
.getSourceCode()
.getText(node.value)
.replace(`${node.value.value}`, value)
.getText(valueNode)
.replace(`${valueNode.value}`, value)
)
})
}
Expand Down
10 changes: 1 addition & 9 deletions lib/rules/no-arrow-functions-in-watch.js
Expand Up @@ -19,15 +19,7 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
return utils.executeOnVue(context, (obj) => {
const watchNode = obj.properties.find(
/**
* @param {ESNode} property
* @returns {property is Property}
*/
(property) =>
property.type === 'Property' &&
utils.getStaticPropertyName(property) === 'watch'
)
const watchNode = utils.findProperty(obj, 'watch')
if (watchNode == null) {
return
}
Expand Down
14 changes: 1 addition & 13 deletions lib/rules/no-boolean-default.js
Expand Up @@ -53,19 +53,7 @@ function getBooleanProps(props) {
* @param {ObjectExpressionProp} propDef
*/
function getDefaultNode(propDef) {
return propDef.value.properties.find(
/**
* @param {ESNode} p
* @returns {p is Property}
*/
(p) => {
return (
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'default'
)
}
)
return utils.findProperty(propDef.value, 'default')
}

module.exports = {
Expand Down
47 changes: 21 additions & 26 deletions lib/rules/no-deprecated-data-object-declaration.js
Expand Up @@ -67,34 +67,29 @@ module.exports = {
const sourceCode = context.getSourceCode()

return utils.executeOnVue(context, (obj) => {
obj.properties
.filter(
/**
* @param {ESNode} p
* @returns {p is Property}
*/
(p) =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'data' &&
p.value.type !== 'FunctionExpression' &&
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)
.forEach((p) => {
context.report({
node: p,
messageId: 'objectDeclarationIsDeprecated',
fix(fixer) {
const tokens = getFirstAndLastTokens(p.value, sourceCode)
const invalidData = utils.findProperty(
obj,
'data',
(p) =>
p.value.type !== 'FunctionExpression' &&
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)

return [
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
fixer.insertTextAfter(tokens.last, ';\n}')
]
}
})
if (invalidData) {
context.report({
node: invalidData,
messageId: 'objectDeclarationIsDeprecated',
fix(fixer) {
const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)

return [
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
fixer.insertTextAfter(tokens.last, ';\n}')
]
}
})
}
})
}
}
7 changes: 1 addition & 6 deletions lib/rules/no-duplicate-attr-inheritance.js
Expand Up @@ -32,12 +32,7 @@ module.exports = {

return Object.assign(
utils.executeOnVue(context, (node) => {
const inheritAttrsProp = node.properties.find(
/** @param {ESNode} prop @returns {prop is Property} */
(prop) =>
prop.type === 'Property' &&
utils.getStaticPropertyName(prop) === 'inheritAttrs'
)
const inheritAttrsProp = utils.findProperty(node, 'inheritAttrs')

if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
inheritsAttrs = inheritAttrsProp.value.value
Expand Down
13 changes: 2 additions & 11 deletions lib/rules/no-reserved-component-names.js
Expand Up @@ -180,19 +180,10 @@ module.exports = {
.filter(({ name }) => reservedNames.has(name))
.forEach(({ node, name }) => report(node, name))

const node = obj.properties.find(
/**
* @param {ObjectExpression['properties'][0]} item
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
*/
(item) =>
item.type === 'Property' &&
item.key.type === 'Identifier' &&
item.key.name === 'name' &&
canVerify(item.value)
)
const node = utils.findProperty(obj, 'name')

if (!node) return
if (!canVerify(node.value)) return
reportIfInvalid(node.value)
})
)
Expand Down
46 changes: 20 additions & 26 deletions lib/rules/no-shared-component-data.js
Expand Up @@ -57,34 +57,28 @@ module.exports = {
const sourceCode = context.getSourceCode()

return utils.executeOnVueComponent(context, (obj) => {
obj.properties
.filter(
/**
* @param {ESNode} p
* @returns {p is Property}
*/
(p) =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'data' &&
p.value.type !== 'FunctionExpression' &&
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)
.forEach((p) => {
context.report({
node: p,
message: '`data` property in component must be a function.',
fix(fixer) {
const tokens = getFirstAndLastTokens(p.value, sourceCode)
const invalidData = utils.findProperty(
obj,
'data',
(p) =>
p.value.type !== 'FunctionExpression' &&
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)
if (invalidData) {
context.report({
node: invalidData,
message: '`data` property in component must be a function.',
fix(fixer) {
const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)

return [
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
fixer.insertTextAfter(tokens.last, ';\n}')
]
}
})
return [
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
fixer.insertTextAfter(tokens.last, ';\n}')
]
}
})
}
})
}
}
8 changes: 1 addition & 7 deletions lib/rules/order-in-components.js
Expand Up @@ -250,13 +250,7 @@ module.exports = {
*/
function checkOrder(propertiesNodes) {
const properties = propertiesNodes
.filter(
/**
* @param {ASTNode} property
* @returns {property is Property}
*/
(property) => property.type === 'Property'
)
.filter(utils.isProperty)
.map((property) => {
return {
node: property,
Expand Down
8 changes: 1 addition & 7 deletions lib/rules/padding-line-between-blocks.js
Expand Up @@ -151,13 +151,7 @@ module.exports = {
* @returns {VElement[]}
*/
function getTopLevelHTMLElements() {
return documentFragment.children.filter(
/**
* @param {VElement | VExpressionContainer | VText} e
* @returns {e is VElement}
*/
(e) => e.type === 'VElement'
)
return documentFragment.children.filter(utils.isVElement)
}

/**
Expand Down

0 comments on commit e049316

Please sign in to comment.