Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 4b88970

Browse files
armano2JamesHenry
authored andcommitted
test: refactor omitDeep function to support name changes (#70)
1 parent 5a1a5ca commit 4b88970

File tree

7 files changed

+559
-132
lines changed

7 files changed

+559
-132
lines changed

tests/ast-alignment/fixtures-to-test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,6 @@ let fixturePatternConfigsToTest = [
377377
* tsep: TSBigIntKeyword
378378
*/
379379
'typed-keyword-bigint',
380-
/**
381-
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9228
382-
* Babel: BooleanLiteral
383-
* tsep: Literal
384-
*/
385-
'typed-keyword-true',
386-
/**
387-
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
388-
* Babel: BooleanLiteral
389-
* tsep: Literal
390-
*/
391-
'typed-keyword-false',
392380
/**
393381
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
394382
* Directive field is not added to module and namespace

tests/ast-alignment/utils.ts

Lines changed: 102 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,56 @@ export function normalizeNodeTypes(ast: any): any {
1414

1515
/**
1616
* Removes the given keys from the given AST object recursively
17-
* @param {Object} obj A JavaScript object to remove keys from
18-
* @param {Object[]} keysToOmit Names and predicate functions use to determine what keys to omit from the final object
17+
* @param root A JavaScript object to remove keys from
18+
* @param keysToOmit Names and predicate functions use to determine what keys to omit from the final object
19+
* @param nodes advance ast modifications
1920
* @returns {Object} formatted object
2021
*/
2122
export function omitDeep(
22-
obj: any,
23-
keysToOmit: { key: string; predicate: Function }[]
24-
): any {
25-
keysToOmit = keysToOmit || [];
26-
function shouldOmit(keyName: string, val: any) {
27-
if (!keysToOmit || !keysToOmit.length) {
28-
return false;
29-
}
30-
for (const keyConfig of keysToOmit) {
31-
if (keyConfig.key !== keyName) {
32-
continue;
33-
}
34-
return keyConfig.predicate(val);
23+
root: any,
24+
keysToOmit: { key: string; predicate: Function }[],
25+
nodes: Record<string, (node: any, parent: any) => void> = {}
26+
) {
27+
function shouldOmit(keyName: string, val: any): boolean {
28+
if (keysToOmit && keysToOmit.length) {
29+
return keysToOmit.some(
30+
keyConfig => keyConfig.key === keyName && keyConfig.predicate(val)
31+
);
3532
}
3633
return false;
3734
}
3835

39-
for (const key in obj) {
40-
if (!obj.hasOwnProperty(key)) {
41-
continue;
36+
function visit(node: any, parent: any) {
37+
if (!node) {
38+
return;
4239
}
43-
const val = (obj as any)[key];
44-
if (isPlainObject(val)) {
45-
if (shouldOmit(key, val)) {
46-
delete (obj as any)[key];
47-
// re-run with the same arguments
48-
// in case the object has multiple keys to omit
49-
return omitDeep(obj, keysToOmit);
50-
}
51-
omitDeep(val, keysToOmit);
52-
} else if (Array.isArray(val)) {
53-
if (shouldOmit(key, val)) {
54-
delete (obj as any)[key];
55-
// re-run with the same arguments
56-
// in case the object has multiple keys to omit
57-
return omitDeep(obj, keysToOmit);
58-
}
59-
for (const i of val) {
60-
omitDeep(i, keysToOmit);
40+
41+
for (const prop in node) {
42+
if (node.hasOwnProperty(prop)) {
43+
if (shouldOmit(prop, node[prop])) {
44+
delete node[prop];
45+
continue;
46+
}
47+
48+
const child = node[prop];
49+
50+
if (Array.isArray(child)) {
51+
for (const el of child) {
52+
visit(el, node);
53+
}
54+
} else if (isPlainObject(child)) {
55+
visit(child, node);
56+
}
6157
}
62-
} else if (shouldOmit(key, val)) {
63-
delete (obj as any)[key];
64-
// re-run with the same arguments
65-
// in case the object has multiple keys to omit
66-
return omitDeep(obj, keysToOmit);
58+
}
59+
60+
if (typeof node.type === 'string' && node.type in nodes) {
61+
nodes[node.type](node, parent);
6762
}
6863
}
69-
return obj;
64+
65+
visit(root, null);
66+
return root;
7067
}
7168

7269
/**
@@ -84,58 +81,78 @@ const ifNumber = (val: any) => typeof val === 'number';
8481
* @returns {Object} processed babylon AST
8582
*/
8683
export function preprocessBabylonAST(ast: any): any {
87-
return omitDeep(ast.program, [
88-
{
89-
key: 'start',
90-
// only remove the "start" number (not the "start" object within loc)
91-
predicate: ifNumber
92-
},
93-
{
94-
key: 'end',
95-
// only remove the "end" number (not the "end" object within loc)
96-
predicate: ifNumber
97-
},
98-
{
99-
key: 'identifierName',
100-
predicate: always
101-
},
102-
{
103-
key: 'extra',
104-
predicate: always
105-
},
106-
{
107-
key: 'directives',
108-
predicate: always
109-
},
110-
{
111-
key: 'innerComments',
112-
predicate: always
113-
},
114-
{
115-
key: 'leadingComments',
116-
predicate: always
117-
},
118-
{
119-
key: 'trailingComments',
120-
predicate: always
121-
},
122-
{
123-
key: 'guardedHandlers',
124-
predicate: always
125-
},
84+
return omitDeep(
85+
ast.program,
86+
[
87+
{
88+
key: 'start',
89+
// only remove the "start" number (not the "start" object within loc)
90+
predicate: ifNumber
91+
},
92+
{
93+
key: 'end',
94+
// only remove the "end" number (not the "end" object within loc)
95+
predicate: ifNumber
96+
},
97+
{
98+
key: 'identifierName',
99+
predicate: always
100+
},
101+
{
102+
key: 'extra',
103+
predicate: always
104+
},
105+
{
106+
key: 'innerComments',
107+
predicate: always
108+
},
109+
{
110+
key: 'leadingComments',
111+
predicate: always
112+
},
113+
{
114+
key: 'trailingComments',
115+
predicate: always
116+
},
117+
{
118+
key: 'guardedHandlers',
119+
predicate: always
120+
},
121+
{
122+
key: 'interpreter',
123+
predicate: always
124+
}
125+
],
126126
{
127-
key: 'interpreter',
128-
predicate: always
127+
/**
128+
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
129+
*/
130+
StringLiteral(node: any) {
131+
node.type = 'Literal';
132+
},
133+
/**
134+
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
135+
*/
136+
NumericLiteral(node: any) {
137+
node.type = 'Literal';
138+
},
139+
/**
140+
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
141+
*/
142+
BooleanLiteral(node: any) {
143+
node.type = 'Literal';
144+
node.raw = String(node.value);
145+
}
129146
}
130-
]);
147+
);
131148
}
132149

133150
/**
134151
* There is currently a really awkward difference in location data for Program nodes
135152
* between different parsers in the ecosystem. Hack around this by removing the data
136153
* before comparing the ASTs.
137154
*
138-
* See: https://github.com/babel/babylon/issues/673
155+
* See: https://github.com/babel/babel/issues/6681
139156
*
140157
* @param {Object} ast the raw AST with a Program node at its top level
141158
* @param {boolean} ignoreSourceType fix for issues with unambiguous type detection
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x: 0;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x: "foo";

tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" en
18961896

18971897
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/types/intersection-type.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
18981898

1899+
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/types/literal-number.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1900+
1901+
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/types/literal-string.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1902+
18991903
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/types/mapped.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
19001904

19011905
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/types/mapped-readonly.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

0 commit comments

Comments
 (0)