Skip to content

Commit f871b53

Browse files
committed
fix: @putout/plugin-apply-arrow: find top scope
1 parent 5c8e07a commit f871b53

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

packages/plugin-apply-arrow/lib/apply-arrow.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

3-
const {print} = require('putout');
3+
const {print, types} = require('putout');
4+
const {isFunction, isProgram} = types;
5+
const isTopScope = (a) => isFunction(a) || isProgram(a);
46

57
module.exports.report = () => `Use 'Arrow Function' instead of 'Function Declaration`;
68

@@ -19,8 +21,9 @@ module.exports.match = () => ({
1921
return binding.references === 1;
2022

2123
const [ref] = binding.referencePaths;
24+
const {uid} = ref.find(isTopScope).scope;
2225

23-
return ref.scope.uid !== binding.scope.uid;
26+
return uid !== binding.scope.uid;
2427
},
2528
});
2629

packages/plugin-apply-arrow/test/apply-arrow.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ test('putout: apply-arrow: no report: long', (t) => {
3232
t.end();
3333
});
3434

35+
test('putout: apply-arrow: no report: for-of', (t) => {
36+
t.noReport('for-of');
37+
t.end();
38+
});
39+
3540
test('putout: apply-arrow: no report: no-loc', (t) => {
3641
const FN = 'function __a(__args) {return __b}';
3742

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
for (const name of names) {
2+
console.log(run(plugin, name));
3+
}
4+
5+
function run(plugin, rule) {
6+
return montag`
7+
"${plugin}/${rule}": {
8+
"$ref": "#/definitions/rule"
9+
},
10+
`;
11+
}
12+
13+
function x() {
14+
for (const name of names) {
15+
b();
16+
}
17+
18+
function b() {
19+
return c;
20+
}
21+
}

scripts/generate-schema.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import {readdir} from 'node:fs/promises';
22
import process from 'node:process';
33
import montag from 'montag';
44

5+
const run = (plugin, rule) => montag`
6+
"${plugin}/${rule}": {
7+
"$ref": "#/definitions/rule"
8+
},
9+
`;
10+
511
const [plugin] = process.argv.slice(2);
612

713
if (!plugin) {
@@ -15,8 +21,3 @@ for (const name of names) {
1521
console.log(run(plugin, name));
1622
}
1723

18-
const run = (plugin, rule) => montag`
19-
"${plugin}/${rule}": {
20-
"$ref": "#/definitions/rule"
21-
},
22-
`;

0 commit comments

Comments
 (0)