From b0de12fae2fbb1f2207f6a51de9f54d7dcfebb3f Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Thu, 15 Apr 2021 10:33:53 +0300 Subject: [PATCH] feat: remove babel traverse and helper from deps --- .gitignore | 1 + __snapshots__/macro.test.ts.snap | 8 +++---- macro.js | 36 +++++++++++++++++++++++++------- package.json | 5 +---- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 6e7901c8..efcd0327 100644 --- a/.gitignore +++ b/.gitignore @@ -181,3 +181,4 @@ $RECYCLE.BIN/ patronum.cjs.js patronum.cjs.js.map +.idea diff --git a/__snapshots__/macro.test.ts.snap b/__snapshots__/macro.test.ts.snap index af45b1df..a8e1beab 100644 --- a/__snapshots__/macro.test.ts.snap +++ b/__snapshots__/macro.test.ts.snap @@ -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: () => @@ -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: () => @@ -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: () => @@ -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: () => diff --git a/macro.js b/macro.js index 6037157b..811c0ace 100644 --- a/macro.js +++ b/macro.js @@ -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'); @@ -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; }); }); @@ -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; +} diff --git a/package.json b/package.json index f590cea8..55511558 100644 --- a/package.json +++ b/package.json @@ -88,8 +88,5 @@ "path": "cz-conventional-changelog" } }, - "dependencies": { - "@babel/helper-module-imports": "^7.12.13", - "@babel/traverse": "^7.13.0" - } + "dependencies": {} }