7
7
const utils = require ( '../utils' )
8
8
const regexp = require ( '../utils/regexp' )
9
9
10
+ /**
11
+ * @typedef {import('../utils').ComponentArrayProp } ComponentArrayProp
12
+ * @typedef {import('../utils').ComponentObjectProp } ComponentObjectProp
13
+ * @typedef {import('../utils').ComponentTypeProp } ComponentTypeProp
14
+ */
15
+
10
16
/**
11
17
* @typedef {object } ParsedOption
12
18
* @property { (name: string) => boolean } test
@@ -88,39 +94,58 @@ module.exports = {
88
94
/** @type {ParsedOption[] } */
89
95
const options = context . options . map ( parseOption )
90
96
91
- return utils . defineVueVisitor ( context , {
92
- onVueObjectEnter ( node ) {
93
- for ( const prop of utils . getComponentProps ( node ) ) {
94
- if ( ! prop . propName ) {
95
- continue
96
- }
97
+ /**
98
+ * @param {(ComponentArrayProp | ComponentObjectProp | ComponentTypeProp)[] } props
99
+ * @param { { [key: string]: Property | undefined } } [withDefaultsProps]
100
+ */
101
+ function processProps ( props , withDefaultsProps ) {
102
+ for ( const prop of props ) {
103
+ if ( ! prop . propName ) {
104
+ continue
105
+ }
97
106
98
- for ( const option of options ) {
99
- if ( option . test ( prop . propName ) ) {
100
- const message =
101
- option . message ||
102
- `Using \`${ prop . propName } \` props is not allowed.`
103
- context . report ( {
104
- node : prop . key ,
105
- messageId : 'restrictedProp' ,
106
- data : { message } ,
107
- suggest : createSuggest ( prop . key , option )
108
- } )
109
- break
110
- }
107
+ for ( const option of options ) {
108
+ if ( option . test ( prop . propName ) ) {
109
+ const message =
110
+ option . message ||
111
+ `Using \`${ prop . propName } \` props is not allowed.`
112
+ context . report ( {
113
+ node : prop . key ,
114
+ messageId : 'restrictedProp' ,
115
+ data : { message } ,
116
+ suggest : createSuggest (
117
+ prop . key ,
118
+ option ,
119
+ withDefaultsProps && withDefaultsProps [ prop . propName ]
120
+ )
121
+ } )
122
+ break
111
123
}
112
124
}
113
125
}
114
- } )
126
+ }
127
+ return utils . compositingVisitors (
128
+ utils . defineScriptSetupVisitor ( context , {
129
+ onDefinePropsEnter ( node , props ) {
130
+ processProps ( props , utils . getWithDefaultsProps ( node ) )
131
+ }
132
+ } ) ,
133
+ utils . defineVueVisitor ( context , {
134
+ onVueObjectEnter ( node ) {
135
+ processProps ( utils . getComponentProps ( node ) )
136
+ }
137
+ } )
138
+ )
115
139
}
116
140
}
117
141
118
142
/**
119
143
* @param {Expression } node
120
144
* @param {ParsedOption } option
145
+ * @param {Property } [withDefault]
121
146
* @returns {Rule.SuggestionReportDescriptor[] }
122
147
*/
123
- function createSuggest ( node , option ) {
148
+ function createSuggest ( node , option , withDefault ) {
124
149
if ( ! option . suggest ) {
125
150
return [ ]
126
151
}
@@ -140,7 +165,17 @@ function createSuggest(node, option) {
140
165
return [
141
166
{
142
167
fix ( fixer ) {
143
- return fixer . replaceText ( node , replaceText )
168
+ const fixes = [ fixer . replaceText ( node , replaceText ) ]
169
+ if ( withDefault ) {
170
+ if ( withDefault . shorthand ) {
171
+ fixes . push (
172
+ fixer . insertTextBefore ( withDefault . value , `${ replaceText } :` )
173
+ )
174
+ } else {
175
+ fixes . push ( fixer . replaceText ( withDefault . key , replaceText ) )
176
+ }
177
+ }
178
+ return fixes . sort ( ( a , b ) => a . range [ 0 ] - b . range [ 0 ] )
144
179
} ,
145
180
messageId : 'instead' ,
146
181
data : { suggest : option . suggest }
0 commit comments