Skip to content

Commit 06a2539

Browse files
committed
Ignore for comments when searching for tokens.
1 parent 8ec9154 commit 06a2539

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

src/findTokens.js

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

test/fixtures/findTokens/container.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ module.exports = (getDataElementValues) => ({
142142
const y2 = "some getDataElementValue('core556')";
143143
const y3 = "some getDataElementValue('core557')";
144144
const y4 = 'some getDataElementValue("core558")';
145+
const y5 = "some getDataElementValue(\"core559\")";
146+
const y6 = 'some getDataElementValue(\'core560\')';
147+
getDataElementValue('core561');
148+
getDataElementValue('core562');
145149

146-
getDataElementValue('core559');
147-
getDataElementValue('core560');
150+
//getDataElementValue('commented');
148151

149152
const z = 5 + getDataElementValue('core');
150153
const z2 = 5 + getDataElementValue('core') + 5;

0 commit comments

Comments
 (0)