18
18
* @typedef {import('vue-eslint-parser').AST.ESLintFunctionExpression } FunctionExpression
19
19
* @typedef {import('vue-eslint-parser').AST.ESLintBlockStatement } BlockStatement
20
20
* @typedef {import('vue-eslint-parser').AST.ESLintNode } ESLintNode
21
+ *
22
+ * @typedef {import('vue-eslint-parser').AST.ESLintArrowFunctionExpression | { type: 'ArrowFunctionExpression', body: BlockStatement | Expression } } ArrowFunctionExpression
21
23
*/
22
24
23
25
/**
37
39
/**
38
40
* @typedef { {key: string, value: BlockStatement} } ComponentComputedProperty
39
41
*/
42
+ /**
43
+ * @typedef { { name: string, groupName: string, node: Literal | TemplateLiteral } } ComponentArrayPropertyData
44
+ * @typedef { { name: string, groupName: string, node: Identifier | Literal | TemplateLiteral } } ComponentObjectPropertyData
45
+ * @typedef { ComponentArrayPropertyData | ComponentObjectPropertyData } ComponentPropertyData
46
+ */
40
47
41
48
// ------------------------------------------------------------------------------
42
49
// Helpers
@@ -799,14 +806,17 @@ module.exports = {
799
806
} ,
800
807
/**
801
808
* Return generator with all properties
802
- * @param {ASTNode } node Node to check
803
- * @param {Set } groups Name of parent group
809
+ * @param {ObjectExpression } node Node to check
810
+ * @param {Set<string> } groups Name of parent group
811
+ * @returns {IterableIterator<ComponentPropertyData> }
804
812
*/
805
813
* iterateProperties ( node , groups ) {
806
- const nodes = node . properties . filter ( p => p . type === 'Property' && groups . has ( getStaticPropertyName ( p . key ) ) )
807
- for ( const item of nodes ) {
808
- const name = getStaticPropertyName ( item . key )
809
- if ( ! name ) continue
814
+ for ( const item of node . properties ) {
815
+ if ( item . type !== 'Property' ) {
816
+ continue
817
+ }
818
+ const name = getStaticPropertyName ( item )
819
+ if ( ! name || ! groups . has ( name ) ) continue
810
820
811
821
if ( item . value . type === 'ArrayExpression' ) {
812
822
yield * this . iterateArrayExpression ( item . value , name )
@@ -822,40 +832,66 @@ module.exports = {
822
832
823
833
/**
824
834
* Return generator with all elements inside ArrayExpression
825
- * @param {ASTNode } node Node to check
835
+ * @param {ArrayExpression } node Node to check
826
836
* @param {string } groupName Name of parent group
837
+ * @returns {IterableIterator<ComponentArrayPropertyData> }
827
838
*/
828
839
* iterateArrayExpression ( node , groupName ) {
829
840
assert ( node . type === 'ArrayExpression' )
830
841
for ( const item of node . elements ) {
831
- const name = getStaticPropertyName ( item )
832
- if ( name ) {
833
- const obj = { name, groupName, node : item }
834
- yield obj
842
+ if ( item . type === 'Literal' || item . type === 'TemplateLiteral' ) {
843
+ const name = getStaticPropertyName ( item )
844
+ if ( name ) {
845
+ yield { name, groupName, node : item }
846
+ }
835
847
}
836
848
}
837
849
} ,
838
850
839
851
/**
840
852
* Return generator with all elements inside ObjectExpression
841
- * @param {ASTNode } node Node to check
853
+ * @param {ObjectExpression } node Node to check
842
854
* @param {string } groupName Name of parent group
855
+ * @returns {IterableIterator<ComponentObjectPropertyData> }
843
856
*/
844
857
* iterateObjectExpression ( node , groupName ) {
845
858
assert ( node . type === 'ObjectExpression' )
859
+ let usedGetter
846
860
for ( const item of node . properties ) {
847
- const name = getStaticPropertyName ( item )
848
- if ( name ) {
849
- const obj = { name, groupName, node : item . key }
850
- yield obj
861
+ if ( item . type === 'Property' ) {
862
+ const key = item . key
863
+ if ( key . type === 'Identifier' || key . type === 'Literal' || key . type === 'TemplateLiteral' ) {
864
+ const name = getStaticPropertyName ( item )
865
+ if ( name ) {
866
+ if ( item . kind === 'set' ) {
867
+ // find getter pair
868
+ if ( ! usedGetter ) { usedGetter = new Set ( ) }
869
+ if ( node . properties . some ( item2 => {
870
+ if ( item2 . type === 'Property' && item2 . kind === 'get' && ! usedGetter . has ( item2 ) ) {
871
+ const getterName = getStaticPropertyName ( item2 )
872
+ if ( getterName === name ) {
873
+ usedGetter . add ( item2 )
874
+ return true
875
+ }
876
+ }
877
+ return false
878
+ } ) ) {
879
+ // has getter pair
880
+ continue
881
+ }
882
+ }
883
+ yield { name, groupName, node : key }
884
+ }
885
+ }
851
886
}
852
887
}
853
888
} ,
854
889
855
890
/**
856
891
* Return generator with all elements inside FunctionExpression
857
- * @param {ASTNode } node Node to check
892
+ * @param {FunctionExpression } node Node to check
858
893
* @param {string } groupName Name of parent group
894
+ * @returns {IterableIterator<ComponentObjectPropertyData> }
859
895
*/
860
896
* iterateFunctionExpression ( node , groupName ) {
861
897
assert ( node . type === 'FunctionExpression' )
@@ -870,19 +906,21 @@ module.exports = {
870
906
871
907
/**
872
908
* Return generator with all elements inside ArrowFunctionExpression
873
- * @param {ASTNode } node Node to check
909
+ * @param {ArrowFunctionExpression } node Node to check
874
910
* @param {string } groupName Name of parent group
911
+ * @returns {IterableIterator<ComponentObjectPropertyData> }
875
912
*/
876
913
* iterateArrowFunctionExpression ( node , groupName ) {
877
914
assert ( node . type === 'ArrowFunctionExpression' )
878
- if ( node . body . type === 'BlockStatement' ) {
879
- for ( const item of node . body . body ) {
915
+ const body = node . body
916
+ if ( body . type === 'BlockStatement' ) {
917
+ for ( const item of body . body ) {
880
918
if ( item . type === 'ReturnStatement' && item . argument && item . argument . type === 'ObjectExpression' ) {
881
919
yield * this . iterateObjectExpression ( item . argument , groupName )
882
920
}
883
921
}
884
- } else if ( node . body . type === 'ObjectExpression' ) {
885
- yield * this . iterateObjectExpression ( node . body , groupName )
922
+ } else if ( body . type === 'ObjectExpression' ) {
923
+ yield * this . iterateObjectExpression ( body , groupName )
886
924
}
887
925
} ,
888
926
0 commit comments