Skip to content

Commit

Permalink
babel-traverse: prefer clearer, reduced-bias option naming (#11791)
Browse files Browse the repository at this point in the history
* babel-traverse: prefer clearer, reduced-bias option naming

* Apply suggestions from code review

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
jayaddison and nicolo-ribaudo committed Jul 29, 2020
1 parent cf425a0 commit 9daa50e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
12 changes: 6 additions & 6 deletions packages/babel-traverse/src/index.js
Expand Up @@ -76,7 +76,7 @@ traverse.removeProperties = function (tree, opts) {
return tree;
};

function hasBlacklistedType(path, state) {
function hasDenylistedType(path, state) {
if (path.node.type === state.type) {
state.has = true;
path.stop();
Expand All @@ -86,10 +86,10 @@ function hasBlacklistedType(path, state) {
traverse.hasType = function (
tree: Object,
type: Object,
blacklistTypes?: Array<string>,
denylistTypes?: Array<string>,
): boolean {
// the node we're searching in is blacklisted
if (blacklistTypes?.includes(tree.type)) return false;
// the node we're searching in is denylisted
if (denylistTypes?.includes(tree.type)) return false;

// the type we're looking for is the same as the passed node
if (tree.type === type) return true;
Expand All @@ -103,8 +103,8 @@ traverse.hasType = function (
tree,
{
noScope: true,
blacklist: blacklistTypes,
enter: hasBlacklistedType,
denylist: denylistTypes,
enter: hasDenylistedType,
},
null,
state,
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-traverse/src/path/context.js
Expand Up @@ -51,17 +51,20 @@ export function _call(fns?: Array<Function>): boolean {
return false;
}

export function isBlacklisted(): boolean {
const blacklist = this.opts.blacklist;
return blacklist && blacklist.indexOf(this.node.type) > -1;
export function isDenylisted(): boolean {
const denylist = this.opts.denylist ?? this.opts.blacklist;
return denylist && denylist.indexOf(this.node.type) > -1;
}

// TODO: Remove in Babel 8
export { isDenylisted as isBlacklisted };

export function visit(): boolean {
if (!this.node) {
return false;
}

if (this.isBlacklisted()) {
if (this.isDenylisted()) {
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion packages/babel-traverse/src/visitors.js
Expand Up @@ -274,7 +274,13 @@ function shouldIgnoreKey(key) {
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;

// ignore other options
if (key === "blacklist" || key === "noScope" || key === "skipKeys") {
if (
key === "denylist" ||
key === "noScope" ||
key === "skipKeys" ||
// TODO: Remove in Babel 8
key === "blacklist"
) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-traverse/test/traverse.js
Expand Up @@ -61,7 +61,7 @@ describe("traverse", function () {
});
});

it("traverse blacklistTypes", function () {
it("traverse denylistTypes", function () {
const expected = [
body[0],
body[0].declarations[0],
Expand All @@ -75,7 +75,7 @@ describe("traverse", function () {
const actual = [];

traverse(program, {
blacklist: ["MemberExpression"],
denylist: ["MemberExpression"],
enter: function (path) {
actual.push(path.node);
},
Expand Down

0 comments on commit 9daa50e

Please sign in to comment.