Skip to content

Commit

Permalink
feat: rule fixer for require-name-property
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxinyu committed Nov 10, 2022
1 parent 9673a61 commit ff10e77
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/rules/require-name-property.js
Expand Up @@ -4,6 +4,7 @@
*/
'use strict'

const path = require('path')
const utils = require('../utils')
const { getVueComponentDefinitionType } = require('../utils')

Expand All @@ -27,7 +28,8 @@ module.exports = {
categories: undefined,
url: 'https://eslint.vuejs.org/rules/require-name-property.html'
},
fixable: null,
fixable: 'whitespace',
hasSuggestions: true,
schema: []
},
/** @param {RuleContext} context */
Expand All @@ -47,7 +49,23 @@ module.exports = {

context.report({
node: component,
message: 'Required name property is not set.'
message: 'Required name property is not set.',
fix: (fixer) => {
const extension = path.extname(context.getFilename())
const filename = path.basename(context.getFilename(), extension)
// fix only when property is not empty
if (component.properties.length > 0) {
const startColumn = component.properties[0].loc.start.column
// insert name property before the first property
return fixer.insertTextBefore(
component.properties[0],
`name: '${filename}',\n${Array.from({
length: startColumn + 1
}).join(' ')}`
)
}
return null
}
})
})
}
Expand Down

0 comments on commit ff10e77

Please sign in to comment.