Skip to content

Commit 81f0f06

Browse files
committed
feature: @putout/plugin-declare-before-reference: improve support of template literals
1 parent c7884ba commit 81f0f06

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export const fix = ({path, referencePath}) => {
3232
const {node} = path.parentPath;
3333
const programPath = path.scope.getProgramParent().path;
3434

35-
delete node.loc;
36-
3735
const exportNode = path.parentPath.parentPath.node;
3836
remove(path.parentPath);
3937

@@ -48,6 +46,13 @@ export const fix = ({path, referencePath}) => {
4846
const [first] = body;
4947

5048
if (compare(first, 'const __a = require(__b)')) {
49+
if (!compare(node, 'const __a = require(__b)')) {
50+
const topPath = referencePath.find(isStatement);
51+
insertBefore(topPath, node);
52+
53+
return;
54+
}
55+
5156
const latest = getPathAfterRequires(body.slice(1));
5257
insertBefore(latest, node);
5358

@@ -110,6 +115,9 @@ export const traverse = ({push}) => ({
110115
const declarationLine = pathLoc.start.line;
111116
const referenceLine = referenceLoc.start.line;
112117

118+
if (!path.parentPath.node)
119+
continue;
120+
113121
if (own && declarationLine > referenceLine)
114122
push({
115123
name,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ test('putout: plugin-declare-before-reference: transform: merge-properties', (t)
216216
t.end();
217217
});
218218

219+
test('putout: plugin-declare-before-reference: transform: template', (t) => {
220+
t.transform('template');
221+
t.end();
222+
});
223+
219224
test('plugin-merge-destructuring-properties: no report after transform: apply-declarations-order', (t) => {
220225
t.noReportAfterTransform('apply-declarations-order', {
221226
'variables/apply-declarations-order': applyDeclarationsOrder,

packages/plugin-declare-before-reference/test/fixture/arg-fix.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const isCallInsideExpression = createTypeChecker([
22
'-: -> !ExpressionStatement',
33
'+: node.expression -> CallExpression',
44
]);
5+
56
const isNextToAssignmentCall = createTypeChecker([
67
'-: node.expression -> AssignmentExpression',
78
['+', getNext(isCallInsideExpression)],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
const {types, operator} = require('putout');
2+
23
const {remove} = operator;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const REACT = '../react';
2+
const render = require(`${REACT}/render`);
3+
const getComponent = require(`${REACT}/get`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const render = require(`${REACT}/render`);
2+
const getComponent = require(`${REACT}/get`);
3+
4+
const REACT = '../react';

0 commit comments

Comments
 (0)