Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isMacrosName option #133

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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