forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor visitors merging (babel#15702)
Co-authored-by: liuxingbaoyu <[email protected]> Co-authored-by: Nicolò Ribaudo <[email protected]>
- Loading branch information
1 parent
4986833
commit b1de75f
Showing
4 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { parse } from "@babel/parser"; | ||
|
||
import _traverse, { visitors } from "../lib/index.js"; | ||
const traverse = _traverse.default || _traverse; | ||
|
||
describe("visitors", () => { | ||
describe("merge", () => { | ||
(process.env.BABEL_8_BREAKING ? it : it.skip)( | ||
"should set `_verified` and `_exploded` to `true` if merging catch-all visitors", | ||
() => { | ||
const visitor = visitors.merge([{ enter() {} }, { enter() {} }]); | ||
expect(visitor._verified).toBe(true); | ||
expect(visitor._exploded).toBe(true); | ||
}, | ||
); | ||
|
||
it("should work when merging node type visitors", () => { | ||
const ast = parse("1"); | ||
const visitor = visitors.merge([ | ||
{ ArrayExpression() {} }, | ||
{ ArrayExpression() {} }, | ||
]); | ||
traverse(ast, visitor); | ||
expect(visitor).toMatchInlineSnapshot(` | ||
Object { | ||
"ArrayExpression": Object { | ||
"enter": Array [ | ||
[Function], | ||
[Function], | ||
], | ||
}, | ||
"_exploded": true, | ||
"_verified": true, | ||
} | ||
`); | ||
}); | ||
|
||
it("enter", () => { | ||
const ast = parse("1"); | ||
const visitor = visitors.merge([{ enter() {} }, { enter() {} }]); | ||
traverse(ast, visitor); | ||
expect(visitor).toMatchInlineSnapshot(` | ||
Object { | ||
"_exploded": true, | ||
"_verified": true, | ||
"enter": Array [ | ||
[Function], | ||
[Function], | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it("enter with states", () => { | ||
const ast = parse("1"); | ||
const visitor = visitors.merge( | ||
[{ enter() {} }, { enter() {} }], | ||
[{}, {}], | ||
); | ||
traverse(ast, visitor); | ||
expect(visitor).toMatchInlineSnapshot(` | ||
Object { | ||
"_exploded": true, | ||
"_verified": true, | ||
"enter": Array [ | ||
[Function], | ||
[Function], | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it("enter with wrapper", () => { | ||
const ast = parse("1"); | ||
const visitor = visitors.merge( | ||
[{ enter() {} }, { enter() {} }], | ||
[{}, {}], | ||
(stateKey, key, fn) => fn, | ||
); | ||
traverse(ast, visitor); | ||
expect(visitor).toMatchInlineSnapshot(` | ||
Object { | ||
"_exploded": true, | ||
"_verified": true, | ||
"enter": Array [ | ||
[Function], | ||
[Function], | ||
], | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |