1 | const { createMacro, MacroError } = require("babel-plugin-macros");
|
2 | const dedent = require("./dist/dedent.js").default;
|
3 |
|
4 | module.exports = createMacro(prevalMacros);
|
5 |
|
6 | function prevalMacros({ references, state, babel }) {
|
7 | references.default.forEach(referencePath => {
|
8 | if (referencePath.parentPath.type === "TaggedTemplateExpression") {
|
9 | asTag(referencePath.parentPath.get("quasi"), state, babel);
|
10 | } else if (referencePath.parentPath.type === "CallExpression") {
|
11 | asFunction(referencePath.parentPath.get("arguments"), state, babel);
|
12 | } else {
|
13 | throw new MacroError(
|
14 | `dedent.macro can only be used as tagged template expression or function call. You tried ${
|
15 | referencePath.parentPath.type
|
16 | }.`
|
17 | );
|
18 | }
|
19 | });
|
20 | }
|
21 |
|
22 | function asTag(quasiPath, { file: { opts: { filename } } }, babel) {
|
23 | const string = quasiPath.parentPath.get("quasi").evaluate().value;
|
24 | const { types: t } = babel;
|
25 |
|
26 | quasiPath.parentPath.replaceWith(t.stringLiteral(dedent(string)));
|
27 | }
|
28 |
|
29 | function asFunction(argumentsPaths, { file: { opts: { filename } } }, babel) {
|
30 | const string = argumentsPaths[0].evaluate().value;
|
31 | const { types: t } = babel;
|
32 |
|
33 | argumentsPaths[0].parentPath.replaceWith(t.stringLiteral(dedent(string)));
|
34 | }
|