Skip to content

Commit

Permalink
feat: add isMacrosName option (#133)
Browse files Browse the repository at this point in the history
* Add isMacrosName option

* Add tests to check isMacrosName option works
  • Loading branch information
futagoza authored and Kent C. Dodds committed Nov 20, 2019
1 parent 1fcdc4f commit c2e2ec9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/fixtures/eval-macro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./eval.macro.js')
24 changes: 24 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -84,7 +90,7 @@ function macrosPlugin(
const isMacros = looksLike(path, {
node: {
source: {
value: v => macrosRegex.test(v),
value: v => isMacrosName(v),
},
},
})
Expand Down Expand Up @@ -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),
},
},
})
Expand Down

0 comments on commit c2e2ec9

Please sign in to comment.