77const utils = require ( '../utils' )
88const regexp = require ( '../utils/regexp' )
99
10+ /**
11+ * @typedef {import('../utils').ComponentArrayProp } ComponentArrayProp
12+ * @typedef {import('../utils').ComponentObjectProp } ComponentObjectProp
13+ * @typedef {import('../utils').ComponentTypeProp } ComponentTypeProp
14+ */
15+
1016/**
1117 * @typedef {object } ParsedOption
1218 * @property { (name: string) => boolean } test
@@ -88,39 +94,58 @@ module.exports = {
8894 /** @type {ParsedOption[] } */
8995 const options = context . options . map ( parseOption )
9096
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+ }
97106
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
111123 }
112124 }
113125 }
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+ )
115139 }
116140}
117141
118142/**
119143 * @param {Expression } node
120144 * @param {ParsedOption } option
145+ * @param {Property } [withDefault]
121146 * @returns {Rule.SuggestionReportDescriptor[] }
122147 */
123- function createSuggest ( node , option ) {
148+ function createSuggest ( node , option , withDefault ) {
124149 if ( ! option . suggest ) {
125150 return [ ]
126151 }
@@ -140,7 +165,17 @@ function createSuggest(node, option) {
140165 return [
141166 {
142167 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 ] )
144179 } ,
145180 messageId : 'instead' ,
146181 data : { suggest : option . suggest }
0 commit comments