Skip to content

Commit 6dd6b23

Browse files
committed
feature: @putout/plugin-declare-before-reference: improve uid check
1 parent 5446da7 commit 6dd6b23

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

packages/plugin-declare-before-reference/lib/declare-before-reference.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const {
1616
} = types;
1717

1818
const {entries} = Object;
19-
const isTopScope = (a) => isFunction(a) || isProgram(a) || isBlockStatement(a);
19+
const isTopScope = (a) => isFunction(a) || isProgram(a);
20+
const getTopUID = (a) => a.find(isTopScope).scope.uid;
21+
const getBlockUID = (a) => a.find(isBlockStatement)?.scope.uid;
2022

2123
module.exports.report = ({name}) => {
2224
return `Declare '${name}' before referencing to avoid 'ReferenceError'`;
@@ -72,9 +74,10 @@ module.exports.traverse = ({push}) => ({
7274
break;
7375

7476
for (const referencePath of referencePaths) {
75-
const referenceUid = referencePath.find(isTopScope).scope.uid;
77+
const referenceUid = getTopUID(referencePath);
78+
const blockUid = getBlockUID(referencePath);
7679

77-
if (uid !== referenceUid && (uid && referencePath.find(isFunction)))
80+
if (uid !== referenceUid && uid !== blockUid)
7881
continue;
7982

8083
if (referencePath.parentPath.isExportDefaultDeclaration())

packages/plugin-declare-before-reference/test/declare-before-reference.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ test('plugin-declare-before-reference: transform: destructuring', (t) => {
102102
t.end();
103103
});
104104

105+
test('plugin-declare-before-reference: no report: different-scopes', (t) => {
106+
t.noReport('different-scopes');
107+
t.end();
108+
});
109+
105110
test('plugin-declare-before-reference: no report: apply-types', (t) => {
106111
t.noReportAfterTransform('apply-types', {
107112
'apply-types': printer.rules['apply-types'],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const x = () => {
2+
run();
3+
};
4+
5+
const run = () => {};

0 commit comments

Comments
 (0)