Skip to content

Commit cf66ec5

Browse files
committed
feature: @putout/plugin-putout: apply-vars: add support of CommonJS
1 parent 2379f31 commit cf66ec5

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports.match = () => ({
2+
'(__a) => typeof __a === "__b"': (vars, path) => !path.node.returnType,
3+
});
4+
5+
module.exports.replace = () => ({
6+
'(__a) => typeof __a === "__b"': (vars, path) => !path.node.returnType,
7+
});
8+
9+
const a = {
10+
'hello': ({}, path) => !path.node.returnType,
11+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports.match = () => ({
2+
'(__a) => typeof __a === "__b"': ({}, path) => !path.node.returnType,
3+
});
4+
5+
module.exports.replace = () => ({
6+
'(__a) => typeof __a === "__b"': ({}, path) => !path.node.returnType,
7+
});
8+
9+
10+
const a = {
11+
'hello': ({}, path) => !path.node.returnType,
12+
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
'use strict';
22

3-
module.exports.report = () => `Use 'var' instead of '{}'`;
3+
const {operator} = require('putout');
4+
const {compareAny} = operator;
5+
6+
module.exports.report = () => `Use 'vars' instead of '{}'`;
7+
8+
module.exports.match = () => ({
9+
'({}, path) => __a': isInsideReplacer,
10+
});
411

512
module.exports.replace = () => ({
6-
'({}, path) => __body': '(vars, path) => __body',
13+
'({}, path) => __a': '(vars, path) => __a',
714
});
15+
16+
const isInsideReplacer = (vars, path) => {
17+
return compareAny(path, [
18+
'module.exports.match = __',
19+
'module.exports.replace = __',
20+
'export const match = __',
21+
'export const replace = __',
22+
]);
23+
};

packages/plugin-putout/lib/apply-vars/index.spec.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ const plugin = require('.');
55

66
const test = createTest(__dirname, {
77
plugins: [
8-
['apply-vars', plugin],
8+
['putout/apply-vars', plugin],
99
],
1010
});
1111

12-
test('putout: apply-vars: report', (t) => {
13-
t.report('apply-vars', `Use 'var' instead of '{}'`);
12+
test('plugin-putout: apply-vars: report', (t) => {
13+
t.report('apply-vars', `Use 'vars' instead of '{}'`);
1414
t.end();
1515
});
1616

17-
test('putout: apply-vars: transform', (t) => {
17+
test('plugin-putout: apply-vars: transform', (t) => {
1818
t.transform('apply-vars');
1919
t.end();
2020
});
21+
22+
test('plugin-putout: apply-vars: transform: no-body', (t) => {
23+
t.transform('no-body');
24+
t.end();
25+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('..');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['apply-vars', plugin],
9+
],
10+
});
11+
12+
test('putout: apply-vars: report', (t) => {
13+
t.report('apply-vars', `Use 'vars' instead of '{}'`);
14+
t.end();
15+
});
16+
17+
test('putout: apply-vars: transform', (t) => {
18+
t.transform('apply-vars');
19+
t.end();
20+
});

packages/plugin-typescript/lib/apply-type-guards/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const create = template('(__a): __a is __c => typeof __a === "__b"', {
1616
module.exports.report = () => `Use 'type guards'`;
1717

1818
module.exports.match = () => ({
19-
'(__a) => typeof __a === "__b"': ({}, path) => !path.node.returnType,
19+
'(__a) => typeof __a === "__b"': (vars, path) => !path.node.returnType,
2020
});
2121

2222
module.exports.replace = () => ({

0 commit comments

Comments
 (0)