@@ -6,6 +6,7 @@ import { findVariable } from '../utils/ast-utils.js';
6
6
import { toRegExp } from '../utils/regexp.js' ;
7
7
8
8
type PropertyPathArray = string [ ] ;
9
+ type DeclaredPropertyNames = Set < { originalName : string ; aliasName : string } > ;
9
10
10
11
let isRemovedWarningShown = false ;
11
12
@@ -182,16 +183,27 @@ export default createRule('no-unused-props', {
182
183
return sourceFile . fileName . includes ( 'node_modules/typescript/lib/' ) ;
183
184
}
184
185
185
- function getUsedPropertyNamesFromPattern ( pattern : TSESTree . ObjectPattern ) : Set < string > {
186
- const usedProps = new Set < string > ( ) ;
186
+ function getUsedPropertyNamesFromPattern (
187
+ pattern : TSESTree . ObjectPattern
188
+ ) : DeclaredPropertyNames {
189
+ const usedProps : DeclaredPropertyNames = new Set ( ) ;
187
190
for ( const prop of pattern . properties ) {
188
- if ( prop . type === 'Property' && prop . key . type === 'Identifier' ) {
189
- usedProps . add ( prop . key . name ) ;
191
+ if ( prop . type === 'Property' ) {
192
+ if ( prop . key . type === 'Identifier' ) {
193
+ usedProps . add ( { originalName : prop . key . name , aliasName : prop . key . name } ) ;
194
+ } else if (
195
+ prop . key . type === 'Literal' &&
196
+ typeof prop . key . value === 'string' &&
197
+ prop . value . type === 'Identifier'
198
+ ) {
199
+ usedProps . add ( { originalName : prop . key . value , aliasName : prop . value . name } ) ;
200
+ }
190
201
} else if ( prop . type === 'RestElement' ) {
191
202
// If there's a rest element, all properties are potentially used
192
203
return new Set ( ) ;
193
204
}
194
205
}
206
+
195
207
return usedProps ;
196
208
}
197
209
@@ -229,7 +241,7 @@ export default createRule('no-unused-props', {
229
241
} : {
230
242
propsType : ts . Type ;
231
243
usedPropertyPaths : string [ ] ;
232
- declaredPropertyNames : Set < string > ;
244
+ declaredPropertyNames : DeclaredPropertyNames ;
233
245
reportNode : TSESTree . Node ;
234
246
parentPath : string [ ] ;
235
247
checkedPropsTypes : Set < string > ;
@@ -287,7 +299,9 @@ export default createRule('no-unused-props', {
287
299
continue ;
288
300
}
289
301
290
- const isUsedInProps = declaredPropertyNames . has ( propName ) ;
302
+ const isUsedInProps = Array . from ( declaredPropertyNames ) . some ( ( p ) => {
303
+ return p . originalName === propName ;
304
+ } ) ;
291
305
292
306
if ( ! isUsedInPath && ! isUsedInProps ) {
293
307
reportedPropertyPaths . add ( currentPathStr ) ;
@@ -338,8 +352,8 @@ export default createRule('no-unused-props', {
338
352
* Returns true if the destructuring pattern includes a rest element,
339
353
* which means all remaining properties are potentially used.
340
354
*/
341
- function hasRestElement ( usedProps : Set < string > ) : boolean {
342
- return usedProps . size === 0 ;
355
+ function hasRestElement ( declaredPropertyNames : DeclaredPropertyNames ) : boolean {
356
+ return declaredPropertyNames . size === 0 ;
343
357
}
344
358
345
359
function normalizeUsedPaths ( paths : PropertyPathArray [ ] ) : PropertyPathArray [ ] {
@@ -370,7 +384,7 @@ export default createRule('no-unused-props', {
370
384
371
385
const propsType = typeChecker . getTypeFromTypeNode ( tsNode . type ) ;
372
386
let usedPropertyPathsArray : PropertyPathArray [ ] = [ ] ;
373
- let declaredPropertyNames = new Set < string > ( ) ;
387
+ let declaredPropertyNames : DeclaredPropertyNames = new Set ( ) ;
374
388
375
389
if ( node . id . type === 'ObjectPattern' ) {
376
390
declaredPropertyNames = getUsedPropertyNamesFromPattern ( node . id ) ;
0 commit comments