From 7b1c18f15fb4706ea54dcbefbcbbcd43b0a7cb75 Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Tue, 19 Nov 2019 11:06:03 +0000 Subject: [PATCH 1/2] Add isMacrosName option --- src/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 34fad0a..4f3c6a6 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const resolve = require('resolve') // const printAST = require('ast-pretty-print') const macrosRegex = /[./]macro(\.js)?$/ +const testMacrosRegex = v => macrosRegex.test(v) // https://stackoverflow.com/a/32749533/971592 class MacroError extends Error { @@ -67,7 +68,12 @@ function nodeResolvePath(source, basedir) { function macrosPlugin( babel, - {require: _require = require, resolvePath = nodeResolvePath, ...options} = {}, + { + require: _require = require, + resolvePath = nodeResolvePath, + isMacrosName = testMacrosRegex, + ...options + } = {}, ) { function interopRequire(path) { // eslint-disable-next-line import/no-dynamic-require @@ -84,7 +90,7 @@ function macrosPlugin( const isMacros = looksLike(path, { node: { source: { - value: v => macrosRegex.test(v), + value: v => isMacrosName(v), }, }, }) @@ -124,7 +130,7 @@ function macrosPlugin( name: 'require', }, arguments: args => - args.length === 1 && macrosRegex.test(args[0].value), + args.length === 1 && isMacrosName(args[0].value), }, }, }) From 7e6e0d6302e1011b030839fa6c3bab79afc699ce Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Tue, 19 Nov 2019 23:53:11 +0000 Subject: [PATCH 2/2] Add tests to check isMacrosName option works --- src/__tests__/__snapshots__/index.js.snap | 22 +++++++++++++++++++++ src/__tests__/fixtures/eval-macro.js | 1 + src/__tests__/index.js | 24 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/__tests__/fixtures/eval-macro.js diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 90eec11..0e4ff69 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -214,6 +214,28 @@ Error: The macro imported from "./fixtures/non-wrapped.macro" must be wrapped in `; +exports[`macros when a custom isMacrosName option is used on a import: when a custom isMacrosName option is used on a import 1`] = ` + +import myEval from './fixtures/eval-macro.js' +const x = myEval\`34 + 45\` + + ↓ ↓ ↓ ↓ ↓ ↓ + +const x = 79; + +`; + +exports[`macros when a custom isMacrosName option is used on a require: when a custom isMacrosName option is used on a require 1`] = ` + +const evaler = require('./fixtures/eval-macro.js') +const x = evaler\`34 + 45\` + + ↓ ↓ ↓ ↓ ↓ ↓ + +const x = 79; + +`; + exports[`macros when a plugin that replaces paths is used, macros still work properly: when a plugin that replaces paths is used, macros still work properly 1`] = ` import myEval from '../eval.macro' diff --git a/src/__tests__/fixtures/eval-macro.js b/src/__tests__/fixtures/eval-macro.js new file mode 100644 index 0000000..559c0f7 --- /dev/null +++ b/src/__tests__/fixtures/eval-macro.js @@ -0,0 +1 @@ +module.exports = require('./eval.macro.js') diff --git a/src/__tests__/index.js b/src/__tests__/index.js index d477b2c..9c4222d 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -410,6 +410,30 @@ pluginTester({ } }, }, + { + title: 'when a custom isMacrosName option is used on a import', + pluginOptions: { + isMacrosName(v) { + return v.endsWith('-macro.js') + }, + }, + code: ` + import myEval from './fixtures/eval-macro.js' + const x = myEval\`34 + 45\` + `, + }, + { + title: 'when a custom isMacrosName option is used on a require', + pluginOptions: { + isMacrosName(v) { + return v.endsWith('-macro.js') + }, + }, + code: ` + const evaler = require('./fixtures/eval-macro.js') + const x = evaler\`34 + 45\` + `, + }, { title: 'when plugin options configuration cannot be merged with file configuration',