Skip to content

Commit

Permalink
feat(macro): throw useful error message if macro used without a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Jan 25, 2023
1 parent c51f2dd commit b54270e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
45 changes: 30 additions & 15 deletions packages/macro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function macro({ references, state, babel }: MacroParams) {
Object.keys(references).forEach((tagName) => {
const nodes = references[tagName]
const macroType = getMacroType(tagName)
if (macroType == null) {
if (!macroType) {
throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`)
}

Expand Down Expand Up @@ -131,21 +131,36 @@ const alreadyVisited = (path: NodePath) => {
}
}

const jsMacros = new Set([
'defineMessage',
'arg',
't',
'plural',
'select',
'selectOrdinal'
])

const jsxMacros = new Set([
'Trans',
'Plural',
'Select',
'SelectOrdinal',
])

function getMacroType(tagName: string): string {
switch (tagName) {
case "defineMessage":
case "arg":
case "t":
case "plural":
case "select":
case "selectOrdinal":
return "js"
case "Trans":
case "Plural":
case "Select":
case "SelectOrdinal":
return "jsx"
}
if (jsMacros.has(tagName)) return "js"
if (jsxMacros.has(tagName)) return "jsx"
}

[...jsMacros, ...jsxMacros].forEach((name) => {
Object.defineProperty(module.exports, name, {
get() {
throw new Error(`The macro you imported from "@lingui/macro" is being executed outside the context of compilation with babel-plugin-macros. `
+ `This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. `
+ `Please see the documentation for how to configure babel-plugin-macros properly: `
+ 'https://github.com/kentcdodds/babel-plugin-macros/blob/main/other/docs/user.md');
}
})
})

export default createMacro(macro)
26 changes: 23 additions & 3 deletions packages/macro/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const testCases: Record<string, TestCase[]> = {
}

describe("macro", function () {
process.env.LINGUI_CONFIG = path.join(__dirname, "lingui.config.js")

const babelOptions: TransformOptions = {
filename: "<filename>",
configFile: false,
Expand All @@ -54,7 +56,16 @@ describe("macro", function () {

cases.forEach(
(
{ name, input, expected, filename, production, useTypescriptPreset, only, skip },
{
name,
input,
expected,
filename,
production,
useTypescriptPreset,
only,
skip,
},
index
) => {
let run = it
Expand All @@ -73,8 +84,6 @@ describe("macro", function () {
babelOptions.presets.push("@babel/preset-typescript")
}

process.env.LINGUI_CONFIG = path.join(__dirname, "lingui.config.js")

try {
if (filename) {
const inputPath = path.relative(
Expand Down Expand Up @@ -111,6 +120,17 @@ describe("macro", function () {
})
})

it("Should throw error if used without babel-macro-plugin", async () => {
await expect(
async () => {
const mod = await import("../src/index");
return (mod as unknown as typeof import('@lingui/macro')).Trans
}
).rejects.toThrow(
'The macro you imported from "@lingui/macro"'
)
})

describe.skip("validation", function () {
describe("plural/select/selectordinal", function () {
it("value is missing", function () {
Expand Down

0 comments on commit b54270e

Please sign in to comment.