@@ -24,10 +24,56 @@ module.exports = (
2424) => {
2525 const r = RegExp ( getTokenPattern ( onlyEnclosedInQuotes ) , 'g' ) ;
2626 let tokens = [ ] ;
27+ let definedDataElements = [ ] ;
2728
28- while ( ( res = r . exec ( containerString ) ) !== null ) {
29- tokens . push ( res [ 1 ] ) ;
30- }
29+ const ast = parse ( containerString ) ;
30+
31+ // The following code is used to find tokens.
32+ traverse ( ast , {
33+ StringLiteral : ( path ) => {
34+ const stringValue = path . node . value ;
35+
36+ while ( ( res = r . exec ( stringValue ) ) !== null ) {
37+ tokens . push ( res [ 1 ] ) ;
38+ }
39+ } ,
40+ CallExpression : ( path ) => {
41+ if ( path . node . callee . name === 'getDataElementValue' ) {
42+ const tokenNameNode = path . node . arguments [ 0 ] ;
43+ tokens . push ( tokenNameNode . value ) ;
44+ }
45+ }
46+ } ) ;
47+
48+ // Here we search for the data elements keys that are defined in the container.
49+ // ATTENTION: This code transforms the ast, so generate a new ast if you need to
50+ // do something new.
51+ traverse ( ast , {
52+ ObjectProperty : ( path ) => {
53+ const keyName = path . node . key . name || path . node . key . value ;
54+
55+ if ( keyName === 'dataElements' ) {
56+ // Remove any function since they cannont be JSON.parsed.
57+ traverse (
58+ path . node . value ,
59+ {
60+ // Replace identifiers with strings key names.
61+ Identifier : ( path ) => {
62+ path . replaceWith ( t . stringLiteral ( path . node . name ) ) ;
63+ } ,
64+ // Remove the data element object so we can keep only the names.
65+ ObjectExpression : ( path ) => {
66+ path . replaceWith ( t . stringLiteral ( '' ) ) ;
67+ }
68+ } ,
69+ path . scope
70+ ) ;
71+
72+ const dataElementsObject = JSON . parse ( generate ( path . node . value ) . code ) ;
73+ definedDataElements = Object . keys ( dataElementsObject ) ;
74+ }
75+ }
76+ } ) ;
3177
3278 if ( unique ) {
3379 tokens = tokens . filter (
@@ -36,35 +82,7 @@ module.exports = (
3682 }
3783
3884 if ( onlyUndefined ) {
39- const ast = parse ( containerString ) ;
40-
41- traverse ( ast , {
42- ObjectProperty : ( path ) => {
43- const keyName = path . node . key . name || path . node . key . value ;
44-
45- if ( keyName === 'dataElements' ) {
46- // Remove any function since they cannont be JSON.parsed.
47- traverse (
48- path . node . value ,
49- {
50- // Replace identifiers with strings key names.
51- Identifier : ( path ) => {
52- path . replaceWith ( t . stringLiteral ( path . node . name ) ) ;
53- } ,
54- // Remove the data element object so we can keep only the names.
55- ObjectExpression : ( path ) => {
56- path . replaceWith ( t . stringLiteral ( '' ) ) ;
57- }
58- } ,
59- path . scope
60- ) ;
61-
62- const dataElementsObject = JSON . parse ( generate ( path . node . value ) . code ) ;
63- const definedDataElements = Object . keys ( dataElementsObject ) ;
64- tokens = difference ( tokens , definedDataElements ) ;
65- }
66- }
67- } ) ;
85+ tokens = difference ( tokens , definedDataElements ) ;
6886 }
6987
7088 return tokens ;
0 commit comments