Repository: lingui/js-lingui
Pull request #1369 (2023-01-25 11:17:10)

docs(): replace defaults -> message

vercel[bot]: [vc]: #KrLaOZSYmY5iJUmCwg+aHLJ+cUsT8lH33TDm54s1/HE=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJqcy1saW5ndWkiLCJyb290RGlyZWN0b3J5IjpudWxsLCJsaXZlRmVlZGJhY2siOnsicmVzb2x2ZWQiOjAsInVucmVzb2x2ZWQiOjAsInRvdGFsIjowLCJsaW5rIjoiIn0sImluc3BlY3RvclVybCI6Imh0dHBzOi8vdmVyY2VsLmNvbS9saW5ndWkvanMtbGluZ3VpLzltd3RTVFNacDdqVDhyckR6dWlRRnpoNVZnQ2giLCJwcmV2aWV3VXJsIjoiIiwibmV4dENvbW1pdFN0YXR1cyI6IlBFTkRJTkcifV19
**The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more)

| Name | Status | Preview | Comments | Updated |
| :--- | :----- | :------ | :------- | :------ |
| **js-lingui** | 🔄 Building ([Inspect](https://vercel.com/lingui/js-lingui/9mwtSTSZp7jT8rrDzuiQFzh5VgCh)) |  |  | Jan 25, 2023 at 11:17AM (UTC) |


github-actions[bot]: ## size-limit report 📦 
| Path                                        | Size         |
| ------------------------------------------- | ------------ |
| ./packages/core/build/esm/index.js          | 2.89 KB (0%) |
| ./packages/detect-locale/build/esm/index.js | 804 B (0%)   |
| ./packages/react/build/esm/index.js         | 2.61 KB (0%) |
| ./packages/remote-loader/build/esm/index.js | 7.37 KB (0%) |

Patch:
diff --git a/packages/macro/src/index.ts b/packages/macro/src/index.ts
index 643436e5..83974660 100644
--- a/packages/macro/src/index.ts
+++ b/packages/macro/src/index.ts
@@ -27,6 +27,22 @@ const getSymbolSource = (name: 'i18n' | 'Trans'): [source: string, identifier?:
 const [i18nImportModule, i18nImportName = "i18n"] = getSymbolSource("i18n")
 const [TransImportModule, TransImportName = "Trans"] = getSymbolSource("Trans")
 
+const jsMacroTags = new Set([
+  'defineMessage',
+  'arg',
+  't',
+  'plural',
+  'select',
+  'selectOrdinal'
+])
+
+const jsxMacroTags = new Set([
+  'Trans',
+  'Plural',
+  'Select',
+  'SelectOrdinal',
+])
+
 function macro({ references, state, babel }: MacroParams) {
   const jsxNodes: NodePath[] = []
   const jsNodes: NodePath[] = []
@@ -34,20 +50,18 @@ function macro({ references, state, babel }: MacroParams) {
 
   Object.keys(references).forEach((tagName) => {
     const nodes = references[tagName]
-    const macroType = getMacroType(tagName)
-    if (macroType == null) {
-      throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`)
-    }
 
-    if (macroType === "js") {
+    if (jsMacroTags.has(tagName)) {
       nodes.forEach((node) => {
         jsNodes.push(node.parentPath)
       })
-    } else {
+    } else if(jsxMacroTags.has(tagName)) {
       nodes.forEach((node) => {
         // identifier.openingElement.jsxElement
         jsxNodes.push(node.parentPath.parentPath)
       })
+    } else {
+      throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`)
     }
   })
 
@@ -131,21 +145,15 @@ const alreadyVisited = (path: NodePath) => {
   }
 }
 
-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"
-  }
-}
+[...jsMacroTags, ...jsxMacroTags].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)
diff --git a/scripts/verdaccio-release.js b/scripts/verdaccio-release.js
index 5372a634..db02f6af 100644
--- a/scripts/verdaccio-release.js
+++ b/scripts/verdaccio-release.js
@@ -1,7 +1,6 @@
 const { exec: _exec } = require("child_process")
 const chalk = require("chalk")
 const ora = require("ora")
-const os = require("os")
 
 async function releaseInVerdaccio() {
   const spinner = ora()
@@ -27,13 +26,8 @@ async function releaseInVerdaccio() {
   spinner.start(
     "Dont't mark files as changed to let publish, but not create a git history"
   )
-  if (isWindows()) {
-    await exec(
-      `for /f "" %f in ('git ls-files -m') do git update-index --assume-unchanged %f`
-    )
-  } else {
-    await exec("git ls-files -m | xargs git update-index --assume-unchanged")
-  }
+  await exec("git commit --all -m \"TEMP: verdaccio release\"")
+
   spinner.succeed()
 
   spinner.start("Publishing packages to local registry")
@@ -44,15 +38,7 @@ async function releaseInVerdaccio() {
   spinner.succeed()
 
   spinner.start("Reverting the changed files from index")
-  if (isWindows()) {
-    await exec(
-      `for /f "tokens=2" %f in ('git ls-files -v ^| findstr /R /C:"^[abcdefghijklmnopqrstuvwxyz]"') do git update-index --no-assume-unchanged %f --"`
-    )
-  } else {
-    await exec(
-      "git ls-files -v | grep '^[a-z]' | cut -c3- | xargs git update-index --no-assume-unchanged --"
-    )
-  }
+  await exec("git reset --soft HEAD~1")
   spinner.succeed()
 
   console.log()
@@ -63,10 +49,6 @@ async function releaseInVerdaccio() {
   )
 }
 
-function isWindows() {
-  return os.platform() === "win32"
-}
-
 function exec(cmd, options) {
   const _options = {
     env: {
diff --git a/website/docs/misc/react-intl.md b/website/docs/misc/react-intl.md
index e8dce522..a6a87607 100644
--- a/website/docs/misc/react-intl.md
+++ b/website/docs/misc/react-intl.md
@@ -20,7 +20,7 @@ Looking at the low-level API of [LinguiJS](https://github.com/lingui/js-lingui),
 ``` jsx
 <Trans
    id="welcome"
-   defaults={`Hello {name}, you have {unreadCount, number} {unreadCount, plural,
+   message={`Hello {name}, you have {unreadCount, number} {unreadCount, plural,
      one {message}
      other {messages}
    }`}
@@ -55,7 +55,7 @@ In [react-intl](https://github.com/yahoo/react-intl), this would be translated a
 ``` jsx
 <Trans
     id='msg.docs'
-    defaults='Read the <0>documentation</0>.'
+    message='Read the <0>documentation</0>.'
     components={[
         <a href="/docs" />
     ]}
@@ -86,7 +86,7 @@ All we need to do is to wrap the message in a [`Trans`](/docs/ref/macro.md#trans
 </p>
 ```
 
-The macro then parses the [`Trans`](/docs/ref/macro.md#trans) macro children and generates `defaults` and `components` props  automatically in the form described in the previous section.
+The macro then parses the [`Trans`](/docs/ref/macro.md#trans) macro children and generates `message` and `components` props  automatically in the form described in the previous section.
 
 This is extremely useful when adding i18n to an existing project. All we need is to wrap all messages in [`Trans`](/docs/ref/macro.md#trans) macro.
 
@@ -149,7 +149,7 @@ and the final message would be very similar:
 ``` jsx
 <Trans
    id="welcome"
-   defaults={`Hello <0>{name}</0>, you have {unreadCount, number} {unreadCount, plural,
+   message={`Hello <0>{name}</0>, you have {unreadCount, number} {unreadCount, plural,
      one {message}
      other {messages}
    }`}
diff --git a/website/docs/ref/catalog-formats.md b/website/docs/ref/catalog-formats.md
index 7200b96e..b236e721 100644
--- a/website/docs/ref/catalog-formats.md
+++ b/website/docs/ref/catalog-formats.md
@@ -91,7 +91,7 @@ This file format simply outputs all internal data in JSON format. It's the origi
 {
    "messageId": {
       "translation": "Translated message",
-      "defaults": "Default message",
+      "message": "Default message",
       "description": "Comment for translators",
       "origin": [["src/App.js", 3]]
    },
diff --git a/website/docs/ref/conf.md b/website/docs/ref/conf.md
index fea1d991..9353cafd 100644
--- a/website/docs/ref/conf.md
+++ b/website/docs/ref/conf.md
@@ -328,7 +328,7 @@ Format of message catalogs. Possible values are:
   {
     "MessageID": {
       "translation": "Translated Message",
-      "defaults": "Default string (from source code)",
+      "message": "Default string (from source code)",
       "origin": [
         ["path/to/src.js", 42]
       ]
diff --git a/website/docs/ref/core.md b/website/docs/ref/core.md
index 8754fbb1..fb911d9a 100644
--- a/website/docs/ref/core.md
+++ b/website/docs/ref/core.md
@@ -128,7 +128,7 @@ The core method for translating and formatting messages.
 
 `values` is an object of variables used in translated message.
 
-`options.defaults` is the default translation (optional). This is mostly used when application doesn't use message IDs in natural language (e.g.: `msg.id` or `Component.title`).
+`options.message` is the default translation (optional). This is mostly used when application doesn't use message IDs in natural language (e.g.: `msg.id` or `Component.title`).
 
 ``` js
 import { i18n } from "@lingui/core"
@@ -140,7 +140,7 @@ i18n._("Hello")
 i18n._("My name is {name}", { name: "Tom" })
 
 // Message with custom messageId
-i18n._("msg.id", { name: "Tom" }, { defaults: "My name is {name}" })
+i18n._("msg.id", { name: "Tom" }, { message: "My name is {name}" })
 ```
 
 ### `i18n.date(value: string | Date[, format: Intl.DateTimeFormatOptions])` {#i18n.date}
diff --git a/website/docs/tutorials/react.md b/website/docs/tutorials/react.md
index e1355744..c53ce0ca 100644
--- a/website/docs/tutorials/react.md
+++ b/website/docs/tutorials/react.md
@@ -418,7 +418,7 @@ By default, [LinguiJS](https://github.com/lingui/js-lingui) generates message ID
 This will generate:
 
 ```jsx
-<h1><Trans id="inbox.title" defaults="Message Inbox" /></h1>
+<h1><Trans id="inbox.title" message="Message Inbox" /></h1>
 ```
 
 In our message catalog, we'll see `inbox.title` as message ID, but we also get `Message Inbox` as default translation for English.


Test patch:
diff --git a/packages/macro/test/index.ts b/packages/macro/test/index.ts
index 7698d6bd..038b065d 100644
--- a/packages/macro/test/index.ts
+++ b/packages/macro/test/index.ts
@@ -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,
@@ -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
@@ -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(
@@ -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 () {