Skip to content

Commit be3cbcd

Browse files
committed
feature: @putout/plugin-putout: add-traverse-args: options
1 parent 620e6fa commit be3cbcd

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const options = {
2+
blacklist,
3+
};
4+
5+
module.exports.traverse = ({push}) => ({
6+
RegExpLiteral(path) {
7+
const {pattern, flags} = path.node;
8+
const [error, result] = tryCatch(optimize, RegExp(pattern, flags), whitelist, options);
9+
}
10+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const report = () => `Use 'if condition' instead of 'ternary expression'`;
2+
3+
export const fix = () => {};
4+
5+
export const traverse = ({options}) => ({
6+
Program() {
7+
console.log(options.x);
8+
},
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const report = () => `Use 'if condition' instead of 'ternary expression'`;
2+
3+
export const fix = () => {
4+
};
5+
6+
export const traverse = () => ({
7+
Program() {
8+
console.log(options.x);
9+
}
10+
});
11+

packages/plugin-putout/lib/add-traverse-args/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const {
77
identifier,
88
} = types;
99

10-
const {traverse} = operator;
10+
const {traverse, getBinding} = operator;
1111

1212
const defaultNames = [
1313
'push',
1414
'store',
1515
'listStore',
1616
'pathStore',
17+
'options',
1718
];
1819

1920
module.exports.report = ({name}) => `Add '${name}' argument to 'traverse'`;
@@ -73,22 +74,23 @@ const checkArgs = (names, push) => (path) => {
7374
if (isArgExists(name, fn))
7475
return;
7576

77+
if (getBinding(path, name))
78+
return;
79+
7680
push({
7781
path,
7882
fn,
7983
name,
8084
});
8185
},
82-
[`__a(__args)`]: (currentPath) => {
83-
const {callee} = currentPath.node;
86+
[`__a(__args)`]: (path) => {
87+
const {callee} = path.node;
8488
const {name} = callee;
8589

8690
if (!names.includes(name))
8791
return;
8892

89-
const bindings = currentPath.scope.getAllBindings();
90-
91-
if (bindings[name])
93+
if (getBinding(path, name))
9294
return;
9395

9496
push({

packages/plugin-putout/lib/add-traverse-args/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ test('plugin-putout: add-traverse-args: transform: list-store', (t) => {
5353
t.transform('list-store');
5454
t.end();
5555
});
56+
57+
test('plugin-putout: add-traverse-args: transform: options', (t) => {
58+
t.transform('options');
59+
t.end();
60+
});
61+
62+
test('plugin-putout: add-traverse-args: no report: options-declared-upper', (t) => {
63+
t.noReport('options-declared-upper');
64+
t.end();
65+
});

0 commit comments

Comments
 (0)