babel-traverse: prefer clearer, reduced-bias option naming (#11791)

* 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>
This commit is contained in:
James Addison 2020-07-29 21:41:20 +01:00 committed by GitHub
parent cf425a0bde
commit 9daa50e005
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 13 deletions

View File

@ -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();
@ -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;
@ -103,8 +103,8 @@ traverse.hasType = function (
tree,
{
noScope: true,
blacklist: blacklistTypes,
enter: hasBlacklistedType,
denylist: denylistTypes,
enter: hasDenylistedType,
},
null,
state,

View File

@ -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;
}

View File

@ -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;
}

View File

@ -61,7 +61,7 @@ describe("traverse", function () {
});
});
it("traverse blacklistTypes", function () {
it("traverse denylistTypes", function () {
const expected = [
body[0],
body[0].declarations[0],
@ -75,7 +75,7 @@ describe("traverse", function () {
const actual = [];
traverse(program, {
blacklist: ["MemberExpression"],
denylist: ["MemberExpression"],
enter: function (path) {
actual.push(path.node);
},