Skip to content
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

feat(require-name-property): support check defineOptions #2686

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
132 changes: 84 additions & 48 deletions lib/rules/require-name-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,54 @@ function isNameProperty(node) {
)
}

/**
* Report error if there has no name property
* @param {RuleContext} context
* @param {CallExpression | ObjectExpression} node
* @param {ObjectExpression} expression
*/
function report(context, node, expression) {
context.report({
node,
messageId: 'missingName',
suggest: [
{
messageId: 'addName',
fix(fixer) {
const extension = path.extname(context.getFilename())
const filename = path.basename(context.getFilename(), extension)
const sourceCode = context.getSourceCode()

if (expression.properties.length > 0) {
const firstToken = sourceCode.getFirstToken(
expression.properties[0]
)
const indentText = getLineEmptyIndent(sourceCode, firstToken)
// insert name property before the first property
return fixer.insertTextBefore(
expression.properties[0],
`name: '${filename}',\n${indentText}`
)
}

const firstToken = sourceCode.getFirstToken(expression)
const lastToken = sourceCode.getLastToken(expression)
// if the component is empty, insert name property and indent
if (firstToken.value === '{' && lastToken.value === '}') {
const indentText = getLineEmptyIndent(sourceCode, firstToken)
return fixer.replaceTextRange(
[firstToken.range[1], lastToken.range[0]],
`\n${indentText} name: '${filename}'\n${indentText}`
)
}

return null
}
}
]
})
}

module.exports = {
meta: {
type: 'suggestion',
Expand All @@ -51,65 +99,53 @@ module.exports = {
},
fixable: null,
hasSuggestions: true,
schema: [],
schema: [
{
type: 'object',
properties: {
checkScriptSetup: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
missingName: 'Required name property is not set.',
addName: 'Add name property to component.'
}
},
/** @param {RuleContext} context */
create(context) {
if (utils.isScriptSetup(context)) {
return {}
}
return utils.executeOnVue(context, (component, type) => {
if (type === 'definition') {
const defType = getVueComponentDefinitionType(component)
if (defType === 'mixin') {
return
}
}

if (component.properties.some(isNameProperty)) return
const option = context.options[1] || {}
const checkScriptSetup = !!option.checkScriptSetup

context.report({
node: component,
messageId: 'missingName',
suggest: [
{
messageId: 'addName',
fix(fixer) {
const extension = path.extname(context.getFilename())
const filename = path.basename(context.getFilename(), extension)
const sourceCode = context.getSourceCode()
// fix only when property is not empty
if (component.properties.length > 0) {
const firstToken = sourceCode.getFirstToken(
component.properties[0]
)
const indentText = getLineEmptyIndent(sourceCode, firstToken)
// insert name property before the first property
return fixer.insertTextBefore(
component.properties[0],
`name: '${filename}',\n${indentText}`
)
}
return utils.compositingVisitors(
checkScriptSetup
? utils.defineScriptSetupVisitor(context, {
onDefineOptionsEnter(node) {
if (node.arguments.length === 0) return
const define = node.arguments[0]
if (define.type !== 'ObjectExpression') return
const nameNode = utils.findProperty(define, 'name')
if (nameNode) return

const firstToken = sourceCode.getFirstToken(component)
const lastToken = sourceCode.getLastToken(component)
// if the component is empty, insert name property and indent
if (firstToken.value === '{' && lastToken.value === '}') {
const indentText = getLineEmptyIndent(sourceCode, firstToken)
return fixer.replaceTextRange(
[firstToken.range[1], lastToken.range[0]],
`\n${indentText} name: '${filename}'\n${indentText}`
)
}
return null
report(context, node, define)
}
})
: {},
utils.executeOnVue(context, (component, type) => {
if (type === 'definition') {
const defType = getVueComponentDefinitionType(component)
if (defType === 'mixin') {
return
}
]
}

if (component.properties.some(isNameProperty)) return

report(context, component, component)
})
})
)
}
}