1 | const { MacroError, createMacro } = require("babel-plugin-macros");
|
2 | const dedent = require("./dist/dedent.js").default;
|
3 |
|
4 | module.exports = createMacro(prevalMacros);
|
5 |
|
6 | function prevalMacros({ babel, references, state }) {
|
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 ${referencePath.parentPath.type}.`,
|
15 | );
|
16 | }
|
17 | });
|
18 | }
|
19 |
|
20 | function asTag(quasiPath, _, babel) {
|
21 | const string = quasiPath.parentPath.get("quasi").evaluate().value;
|
22 | const { types: t } = babel;
|
23 |
|
24 | quasiPath.parentPath.replaceWith(t.stringLiteral(dedent(string)));
|
25 | }
|
26 |
|
27 | function asFunction(argumentsPaths, _, babel) {
|
28 | const string = argumentsPaths[0].evaluate().value;
|
29 | const { types: t } = babel;
|
30 |
|
31 | argumentsPaths[0].parentPath.replaceWith(t.stringLiteral(dedent(string)));
|
32 | }
|