Skip to content

Commit ff10e77

Browse files
author
sunxinyu
committed
feat: rule fixer for require-name-property
1 parent 9673a61 commit ff10e77

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/rules/require-name-property.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
'use strict'
66

7+
const path = require('path')
78
const utils = require('../utils')
89
const { getVueComponentDefinitionType } = require('../utils')
910

@@ -27,7 +28,8 @@ module.exports = {
2728
categories: undefined,
2829
url: 'https://eslint.vuejs.org/rules/require-name-property.html'
2930
},
30-
fixable: null,
31+
fixable: 'whitespace',
32+
hasSuggestions: true,
3133
schema: []
3234
},
3335
/** @param {RuleContext} context */
@@ -47,7 +49,23 @@ module.exports = {
4749

4850
context.report({
4951
node: component,
50-
message: 'Required name property is not set.'
52+
message: 'Required name property is not set.',
53+
fix: (fixer) => {
54+
const extension = path.extname(context.getFilename())
55+
const filename = path.basename(context.getFilename(), extension)
56+
// fix only when property is not empty
57+
if (component.properties.length > 0) {
58+
const startColumn = component.properties[0].loc.start.column
59+
// insert name property before the first property
60+
return fixer.insertTextBefore(
61+
component.properties[0],
62+
`name: '${filename}',\n${Array.from({
63+
length: startColumn + 1
64+
}).join(' ')}`
65+
)
66+
}
67+
return null
68+
}
5169
})
5270
})
5371
}

0 commit comments

Comments
 (0)