Skip to content

Commit

Permalink
feat: remove babel traverse and helper from deps
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Apr 15, 2021
1 parent b0c7ebc commit b0de12f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,4 @@ $RECYCLE.BIN/

patronum.cjs.js
patronum.cjs.js.map
.idea
8 changes: 4 additions & 4 deletions __snapshots__/macro.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ const last = debounce({ source: wow, timeout: 10 });
↓ ↓ ↓ ↓ ↓ ↓
import { withFactory } from 'effector';
import { debounce as _debounce } from '@effector/patronum';
import { status as _status } from '@effector/patronum';
import { withFactory } from 'effector';
const $status = withFactory({
sid: 'j4rpgh',
fn: () =>
Expand Down Expand Up @@ -121,9 +121,9 @@ const last = debounce({ source: wow, timeout: 10 });
↓ ↓ ↓ ↓ ↓ ↓
import { withFactory } from 'effector';
import { debounce as _debounce } from 'patronum';
import { status as _status } from 'patronum';
import { withFactory } from 'effector';
const $status = withFactory({
sid: 'j4rpgh',
fn: () =>
Expand Down Expand Up @@ -151,9 +151,9 @@ const last = debounce({ source: wow, timeout: 10 });
↓ ↓ ↓ ↓ ↓ ↓
import { withFactory } from 'effector';
import { debounce as _debounce } from '@effector/patronum/debounce';
import { status as _status } from '@effector/patronum/status';
import { withFactory } from 'effector';
const $status = withFactory({
sid: 'j4rpgh',
fn: () =>
Expand Down Expand Up @@ -181,9 +181,9 @@ const last = debounce({ source: wow, timeout: 10 });
↓ ↓ ↓ ↓ ↓ ↓
import { withFactory } from 'effector';
import { debounce as _debounce } from 'patronum/debounce';
import { status as _status } from 'patronum/status';
import { withFactory } from 'effector';
const $status = withFactory({
sid: 'j4rpgh',
fn: () =>
Expand Down
36 changes: 29 additions & 7 deletions macro.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
const { createMacro } = require('babel-plugin-macros');
const { addNamed } = require('@babel/helper-module-imports');
const { default: traverse } = require('@babel/traverse');
const babelPlugin = require('effector/babel-plugin');
const { factories, mapping } = require('./babel-plugin-factories.json');

Expand All @@ -21,12 +19,10 @@ function patronum({
const methodImportPath = importFromRoot
? importModuleName
: importModuleName + '/' + mapping[referenceName];
const id = addNamed(program, referenceName, methodImportPath, {
nameHint: referenceName,
});
const id = addImport(babel.types, program, referenceName, methodImportPath);

references[referenceName].forEach((referencePath) => {
referencePath.node.name = id.name;
referencePath.node.name = id;
});
});

Expand All @@ -38,9 +34,35 @@ function patronum({
});

instance.pre();
traverse(program.parent, instance.visitor, undefined, {
babel.traverse(program.parent, instance.visitor, undefined, {
...state,
...instance,
});
instance.post();
}

function addImport(t, programPath, specifierName, importPath) {
const [newPath] = programPath.unshiftContainer(
'body',
t.importDeclaration(
[
t.importSpecifier(
programPath.scope.generateUidIdentifier(specifierName),
t.identifier(specifierName),
),
],
t.stringLiteral(importPath),
),
);

let found;

newPath.get('specifiers').forEach((specifier) => {
if (specifier.node.imported.name === specifierName) {
found = specifier;
}
});

programPath.scope.registerBinding('module', found);
return found.node.local.name;
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,5 @@
"path": "cz-conventional-changelog"
}
},
"dependencies": {
"@babel/helper-module-imports": "^7.12.13",
"@babel/traverse": "^7.13.0"
}
"dependencies": {}
}

0 comments on commit b0de12f

Please sign in to comment.