Skip to content

Commit

Permalink
Merge branch 'next' into refactor/expose-js-macro-internals
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/babel-plugin-lingui-macro/src/macroJs.ts
  • Loading branch information
timofei-iatsenko committed Sep 22, 2024
2 parents fbcff6a + 7936155 commit b961091
Show file tree
Hide file tree
Showing 91 changed files with 1,279 additions and 580 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [4.11.4](https:/lingui/js-lingui/compare/v4.11.3...v4.11.4) (2024-09-02)

### Bug Fixes

- **cli:** use caret range for `micromatch` dependency ([#2020](https:/lingui/js-lingui/issues/2020)) ([be441e3](https:/lingui/js-lingui/commit/be441e31ea1c5a0325f77402602f61c20a4aff4e))
- escape nested brackets ([#2001](https:/lingui/js-lingui/issues/2001)) ([6d00301](https:/lingui/js-lingui/commit/6d0030146cc73f457e4cdcd1837f3d8f060d16fc))
- return a single node when applicable ([#2016](https:/lingui/js-lingui/issues/2016)) ([68d8358](https:/lingui/js-lingui/commit/68d8358ff7bbb09de8953db9c7faf0a9a4e99d80))
- run type tests in test:all ([#2017](https:/lingui/js-lingui/issues/2017)) ([b9e89c1](https:/lingui/js-lingui/commit/b9e89c17de2bdaaf64d3d40bd308777285ed2b1a))

## [4.11.3](https:/lingui/js-lingui/compare/v4.11.2...v4.11.3) (2024-08-09)

### Bug Fixes

- **cli:** add pnpm detection to command suggestions ([#1989](https:/lingui/js-lingui/issues/1989)) ([287a688](https:/lingui/js-lingui/commit/287a68848d84134db10fbd373138ec8bbbf2889e))
- **extract:** escape special regex characters used in file-based routing systems when files are passed in options ([#1984](https:/lingui/js-lingui/issues/1984)) ([fd92d20](https:/lingui/js-lingui/commit/fd92d203ba299816150556aee46620d2e3f7794a))

## [4.11.2](https:/lingui/js-lingui/compare/v4.11.1...v4.11.2) (2024-07-03)

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs-swc/src/app/[lang]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import linguiConfig from '../../../lingui.config'
import { allI18nInstances, allMessages } from '../../appRouterI18n'
import { allMessages, getI18nInstance } from '../../appRouterI18n'
import { LinguiClientProvider } from '../../components/LinguiClientProvider'
import { PageLangParam, withLinguiLayout } from '../../withLingui'
import React from 'react'
Expand All @@ -10,7 +10,7 @@ export async function generateStaticParams() {
}

export function generateMetadata({ params }: PageLangParam) {
const i18n = allI18nInstances[params.lang]!
const i18n = getI18nInstance(params.lang)

return {
title: t(i18n)`Translation Demo`
Expand Down
7 changes: 7 additions & 0 deletions examples/nextjs-swc/src/appRouterI18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ export const allI18nInstances: AllI18nInstances = locales.reduce(
},
{}
)

export const getI18nInstance = (locale: SupportedLocales): I18n => {
if (!allI18nInstances[locale]) {
console.warn(`No i18n instance found for locale "${locale}"`)
}
return allI18nInstances[locale]! || allI18nInstances['en']!
}
6 changes: 3 additions & 3 deletions examples/nextjs-swc/src/withLingui.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactNode } from 'react'
import { allI18nInstances } from './appRouterI18n'
import { getI18nInstance } from './appRouterI18n'
import { setI18n } from '@lingui/react/server'

export type PageLangParam = {
Expand All @@ -21,7 +21,7 @@ export const withLinguiPage = <Props extends PageProps>(
): PageExposedToNextJS<Props> => {
return function WithLingui(props) {
const lang = props.params.lang
const i18n = allI18nInstances[lang]!
const i18n = getI18nInstance(lang)
setI18n(i18n)

return <AppRouterPage {...props} lang={lang} />
Expand All @@ -37,7 +37,7 @@ export const withLinguiLayout = <Props extends LayoutProps>(
): LayoutExposedToNextJS<Props> => {
return function WithLingui(props) {
const lang = props.params.lang
const i18n = allI18nInstances[lang]!
const i18n = getI18nInstance(lang)
setI18n(i18n)

return <AppRouterPage {...props} lang={lang} />
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "5.0.0-next.0",
"version": "5.0.0-next.1",
"packages": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:integration": "jest -c jest.config.integration.js",
"test:e2e": "yarn workspaces foreach -p run test:e2e",
"test:tsd": "jest -c jest.config.types.js",
"test:all": "yarn test && yarn test:integration && yarn test:e2e && test:tsd",
"test:all": "yarn test && yarn test:integration && yarn test:e2e && yarn test:tsd",
"lint:types": "tsc",
"lint:eslint": "eslint './packages/**/*.{ts,tsx,js,jsx}'",
"lint:all": "yarn lint:eslint && yarn lint:types",
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-plugin-extract-messages/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [4.11.4](https:/lingui/js-lingui/compare/v4.11.3...v4.11.4) (2024-09-02)

**Note:** Version bump only for package @lingui/babel-plugin-extract-messages

## [4.11.3](https:/lingui/js-lingui/compare/v4.11.2...v4.11.3) (2024-08-09)

**Note:** Version bump only for package @lingui/babel-plugin-extract-messages

## [4.11.2](https:/lingui/js-lingui/compare/v4.11.1...v4.11.2) (2024-07-03)

**Note:** Version bump only for package @lingui/babel-plugin-extract-messages
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-extract-messages/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lingui/babel-plugin-extract-messages",
"version": "5.0.0-next.0",
"version": "5.0.0-next.1",
"description": "Babel plugin for collecting messages from source code for internationalization",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-plugin-extract-messages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ export default function ({ types: t }: { types: BabelTypes }): PluginObj {
const attrs = node.openingElement.attributes || []

if (
t.isJSXSpreadAttribute(attrs[0]) &&
hasI18nComment(attrs[0].argument)
attrs.find(
(attr) =>
t.isJSXSpreadAttribute(attr) && hasI18nComment(attr.argument)
)
) {
return
}
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-plugin-extract-messages/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ import { Trans } from "@lingui/react";
})
})

it("Should not rise warning when `key` used with macro", () => {
const code = `
import { Trans } from "@lingui/react/macro";
<Trans context="Context2" key={1}>
Some message
</Trans>
`
expectNoConsole(() => {
const messages = transformCode(code)
expect(messages.length).toBe(1)
})
})

it("Should log error when no ID provided", () => {
const code = `
import { Trans } from "@lingui/react";
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-plugin-lingui-macro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lingui/babel-plugin-lingui-macro",
"version": "5.0.0-next.0",
"version": "5.0.0-next.1",
"description": "Babel plugin for transforming Lingui Macros",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -84,9 +84,9 @@
"@babel/core": "^7.20.12",
"@babel/runtime": "^7.20.13",
"@babel/types": "^7.20.7",
"@lingui/conf": "5.0.0-next.0",
"@lingui/core": "5.0.0-next.0",
"@lingui/message-utils": "5.0.0-next.0"
"@lingui/conf": "5.0.0-next.1",
"@lingui/core": "5.0.0-next.1",
"@lingui/message-utils": "5.0.0-next.1"
},
"peerDependencies": {
"babel-plugin-macros": "2 || 3"
Expand Down
23 changes: 15 additions & 8 deletions packages/babel-plugin-lingui-macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ export class MacroJs {
// looking for `t` property in left side assigment
// in the declarator `const { t } = useLingui()`
const varDec = path.parentPath.node

if (!t.isObjectPattern(varDec.id)) {
// Enforce destructuring `t` from `useLingui` macro to prevent misuse
throw new Error(
`You have to destructure \`t\` when using the \`useLingui\` macro, i.e:
const { t } = useLingui()
or
const { t: _ } = useLingui()
`
)
}

const _property = t.isObjectPattern(varDec.id)
? varDec.id.properties.find(
(
Expand All @@ -224,15 +236,10 @@ export class MacroJs {
)
: null

// Enforce destructuring `t` from `useLingui` macro to prevent misuse
const newNode = t.callExpression(t.identifier(this.useLinguiImportName), [])

if (!_property) {
throw new Error(
`You have to destructure \`t\` when using the \`useLingui\` macro, i.e:
const { t } = useLingui()
or
const { t: _ } = useLingui()
`
)
return newNode
}

const uniqTIdentifier = path.scope.generateUidIdentifier("t")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ function MyComponent() {
`;
exports[`support i18n export 1`] = `
import { useLingui } from "@lingui/react/macro";
function MyComponent() {
const { i18n } = useLingui();
console.log(i18n);
}
↓ ↓ ↓ ↓ ↓ ↓
import { useLingui as _useLingui } from "@lingui/react";
function MyComponent() {
const { i18n } = _useLingui();
console.log(i18n);
}
`;
exports[`support message descriptor 1`] = `
import { useLingui } from "@lingui/react/macro";
function MyComponent() {
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ function MyComponent() {
console.log(i18n);
const a = t\`Text\`;
}
`,
},
{
name: "support i18n export",
code: `
import { useLingui } from '@lingui/react/macro';
function MyComponent() {
const { i18n } = useLingui();
console.log(i18n);
}
`,
},
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [4.11.4](https:/lingui/js-lingui/compare/v4.11.3...v4.11.4) (2024-09-02)

### Bug Fixes

- **cli:** use caret range for `micromatch` dependency ([#2020](https:/lingui/js-lingui/issues/2020)) ([be441e3](https:/lingui/js-lingui/commit/be441e31ea1c5a0325f77402602f61c20a4aff4e))
- escape nested brackets ([#2001](https:/lingui/js-lingui/issues/2001)) ([6d00301](https:/lingui/js-lingui/commit/6d0030146cc73f457e4cdcd1837f3d8f060d16fc))

## [4.11.3](https:/lingui/js-lingui/compare/v4.11.2...v4.11.3) (2024-08-09)

### Bug Fixes

- **cli:** add pnpm detection to command suggestions ([#1989](https:/lingui/js-lingui/issues/1989)) ([287a688](https:/lingui/js-lingui/commit/287a68848d84134db10fbd373138ec8bbbf2889e))
- **extract:** escape special regex characters used in file-based routing systems when files are passed in options ([#1984](https:/lingui/js-lingui/issues/1984)) ([fd92d20](https:/lingui/js-lingui/commit/fd92d203ba299816150556aee46620d2e3f7794a))

## [4.11.2](https:/lingui/js-lingui/compare/v4.11.1...v4.11.2) (2024-07-03)

### Bug Fixes
Expand Down
21 changes: 10 additions & 11 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lingui/cli",
"version": "5.0.0-next.0",
"version": "5.0.0-next.1",
"description": "CLI for working wit message catalogs",
"keywords": [
"cli",
Expand Down Expand Up @@ -53,23 +53,23 @@
"@babel/parser": "^7.21.2",
"@babel/runtime": "^7.21.0",
"@babel/types": "^7.21.2",
"@lingui/babel-plugin-extract-messages": "5.0.0-next.0",
"@lingui/babel-plugin-lingui-macro": "5.0.0-next.0",
"@lingui/conf": "5.0.0-next.0",
"@lingui/core": "5.0.0-next.0",
"@lingui/format-po": "5.0.0-next.0",
"@lingui/message-utils": "5.0.0-next.0",
"@lingui/babel-plugin-extract-messages": "5.0.0-next.1",
"@lingui/babel-plugin-lingui-macro": "5.0.0-next.1",
"@lingui/conf": "5.0.0-next.1",
"@lingui/core": "5.0.0-next.1",
"@lingui/format-po": "5.0.0-next.1",
"@lingui/message-utils": "5.0.0-next.1",
"babel-plugin-macros": "^3.0.1",
"chalk": "^4.1.0",
"chokidar": "3.5.1",
"cli-table": "0.3.6",
"cli-table": "^0.3.11",
"commander": "^10.0.0",
"convert-source-map": "^2.0.0",
"date-fns": "^3.6.0",
"esbuild": "^0.21.5",
"glob": "^7.1.4",
"glob": "^11.0.0",
"inquirer": "^7.3.3",
"micromatch": "4.0.2",
"micromatch": "^4.0.7",
"normalize-path": "^3.0.0",
"ora": "^5.1.0",
"pathe": "^1.1.0",
Expand All @@ -82,7 +82,6 @@
"devDependencies": {
"@lingui/jest-mocks": "*",
"@types/convert-source-map": "^2.0.0",
"@types/glob": "^8.1.0",
"@types/micromatch": "^4.0.1",
"@types/normalize-path": "^3.0.0",
"mock-fs": "^5.2.0",
Expand Down
Loading

0 comments on commit b961091

Please sign in to comment.