@@ -18,97 +18,76 @@ module.exports = {
18
18
schema : [ ]
19
19
} ,
20
20
create : function ( context ) {
21
- function extractKeysFromObjectExpression ( node ) {
21
+ function extractKeysFromObjectExpression ( node , parentKey = "" ) {
22
22
const keys = [ ] ;
23
23
24
- function traverseProperties ( properties ) {
25
- if ( ! properties ) return ; // Handle undefined or null properties
26
-
27
- for ( const property of properties ) {
24
+ function traverseObjectProperties ( properties , parentKey ) {
25
+ properties . forEach ( ( property ) => {
28
26
if (
29
- property . type === "Property" &&
30
- property . value . type === "ObjectExpression"
31
- ) {
32
- traverseProperties ( property . value . properties ) ;
33
- } else if (
34
- property . type === "Property" &&
35
- property . value . type === "ArrayExpression"
36
- ) {
37
- traverseArrayElements ( property . value . elements ) ;
38
- } else if (
39
27
property . type === "Property" &&
40
28
property . key . type === "Identifier"
41
29
) {
42
- keys . push ( property . key . name ) ;
43
- }
44
- }
45
- }
46
-
47
- function traverseArrayElements ( elements ) {
48
- if ( ! elements ) return ; // Handle undefined or null elements
49
-
50
- for ( const element of elements ) {
51
- if ( element . type === "ObjectExpression" ) {
52
- traverseProperties ( element . properties ) ;
30
+ const currentKey = parentKey
31
+ ? `${ parentKey } .${ property . key . name } `
32
+ : property . key . name ;
33
+ keys . push ( currentKey ) ;
34
+ if ( property . value . type === "ObjectExpression" ) {
35
+ traverseObjectProperties (
36
+ property . value . properties ,
37
+ currentKey
38
+ ) ;
39
+ }
53
40
}
54
- }
41
+ } ) ;
55
42
}
56
43
57
- traverseProperties ( node . properties ) ;
44
+ traverseObjectProperties ( node . properties , parentKey ) ;
58
45
59
46
return keys ;
60
47
}
61
48
62
- function extractKeysFromFile ( filePath ) {
49
+ function extractKeysFromFile ( filePath , parentKey = "" ) {
63
50
const fileContent = fs . readFileSync ( filePath , "utf8" ) ;
64
51
const ast = parse ( fileContent , {
65
- sourceType : "module" , // or 'script' depending on your file
52
+ sourceType : "module" ,
66
53
plugins : [ "typescript" , "jsx" ]
67
54
} ) ;
68
55
const keys = [ ] ;
69
56
70
57
const properties = ast . program . body [ 0 ] . declaration . properties ;
71
58
72
- function traverseProperties ( properties ) {
73
- for ( const property of properties ) {
59
+ function traverseFileProperties ( properties , parentKey ) {
60
+ properties . forEach ( ( property ) => {
74
61
if (
75
- property . type === "ObjectProperty" &&
76
- property . value . type === "ObjectExpression"
77
- ) {
78
- traverseProperties ( property . value . properties ) ;
79
- } else if (
80
- property . type === "ObjectProperty" &&
81
- property . value . type === "ArrayExpression"
82
- ) {
83
- traverseArrayElements ( property . value . elements ) ;
84
- } else if (
85
62
property . type === "ObjectProperty" &&
86
63
property . key . type === "Identifier"
87
64
) {
88
- keys . push ( property . key . name ) ;
89
- }
90
- }
91
- }
92
-
93
- function traverseArrayElements ( elements ) {
94
- if ( ! elements ) return ; // Handle undefined or null elements
95
-
96
- for ( const element of elements ) {
97
- if ( element . type === "ObjectExpression" ) {
98
- traverseProperties ( element . properties ) ;
65
+ const currentKey = parentKey
66
+ ? `${ parentKey } .${ property . key . name } `
67
+ : property . key . name ;
68
+ keys . push ( currentKey ) ;
69
+ if ( property . value . type === "ObjectExpression" ) {
70
+ traverseFileProperties (
71
+ property . value . properties ,
72
+ currentKey
73
+ ) ;
74
+ }
99
75
}
100
- }
76
+ } ) ;
101
77
}
102
78
103
- traverseProperties ( properties ) ;
79
+ traverseFileProperties ( properties , parentKey ) ;
104
80
105
81
return keys ;
106
82
}
107
83
108
84
return {
109
85
Program ( node ) {
110
86
for ( const statement of node . body ) {
111
- const relativePath = path . relative ( process . cwd ( ) , context . getFilename ( ) )
87
+ const relativePath = path . relative (
88
+ process . cwd ( ) ,
89
+ context . getFilename ( )
90
+ ) ;
112
91
const fallbackFilePath = path
113
92
. relative ( process . cwd ( ) , context . getFilename ( ) )
114
93
. replace (
@@ -121,27 +100,23 @@ module.exports = {
121
100
) ;
122
101
123
102
const enKeys = extractKeysFromFile ( fallbackFilePath ) ;
124
- // Report missing keys and incorrect order
125
- enKeys . forEach ( ( enKey , index ) => {
103
+
104
+ // Report missing keys
105
+ enKeys . forEach ( ( enKey ) => {
126
106
if ( ! keys . includes ( enKey ) ) {
127
107
context . report ( {
128
108
node : node ,
129
- message : `missing key '${ enKey } '`
130
- } ) ;
131
- } else if ( keys . indexOf ( enKey ) !== index ) {
132
- context . report ( {
133
- node : node ,
134
- message : `incorrect key location '${ enKey } '`
109
+ message : `missing key '${ enKey } ' ${ relativePath } `
135
110
} ) ;
136
111
}
137
112
} ) ;
138
113
139
114
// Report extra keys
140
- keys . forEach ( key => {
115
+ keys . forEach ( ( key ) => {
141
116
if ( ! enKeys . includes ( key ) ) {
142
117
context . report ( {
143
118
node : node ,
144
- message : `extra key '${ key } '`
119
+ message : `extra key '${ key } ' ${ relativePath } `
145
120
} ) ;
146
121
}
147
122
} ) ;
0 commit comments