Skip to content

Commit

Permalink
refactor(extractor/babel): make opinionated settings for babel extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Jan 27, 2023
1 parent d3cfad6 commit fd76a91
Show file tree
Hide file tree
Showing 46 changed files with 4,025 additions and 5,644 deletions.
6 changes: 3 additions & 3 deletions examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"@lingui/macro": "^3.5.1"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2",
"@lingui/cli": "^3.5.1",
"babel-jest": "^26.6.1",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-macros": "^3.1.0",
"jest": "^26.6.1"
}
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"node": ">=14.0.0"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-export-default-from": "^7.10.4",
"@babel/plugin-transform-runtime": "^7.11.5",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@babel/code-frame": "^7.10.4",
"@babel/core": "^7.20.12",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-export-default-from": "^7.18.10",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/code-frame": "^7.18.6",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
Expand All @@ -55,7 +55,7 @@
"@typescript-eslint/parser": "^4.4.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.5.2",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-macros": "^3.1.0",
"chalk": "^4.1.0",
"codecov": "^3.8.1",
"cross-env": "^7.0.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-plugin-extract-messages/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const transform = (filename: string) => {
}

const transformCode = (code: string, filename = 'test-case.js', rootDir=".") => {
process.env.LINGUI_EXTRACT = "1"
process.env.LINGUI_CONFIG = path.join(
__dirname,
"fixtures",
Expand All @@ -39,6 +38,7 @@ const transformCode = (code: string, filename = 'test-case.js', rootDir=".") =>
plugins: [
"@babel/plugin-syntax-jsx",
["macros", {
lingui: { extract: true },
// macro plugin uses package `resolve` to find a path of macro file
// this will not follow jest pathMapping and will resolve path from ./build
// instead of ./src which makes testing & developing hard.
Expand All @@ -52,7 +52,6 @@ const transformCode = (code: string, filename = 'test-case.js', rootDir=".") =>
]
})
} finally {
process.env.LINGUI_EXTRACT = null
process.env.LINGUI_CONFIG = null

}
Expand Down
19 changes: 7 additions & 12 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@
"build/"
],
"dependencies": {
"@babel/generator": "^7.11.6",
"@babel/parser": "^7.11.5",
"@babel/plugin-syntax-jsx": "^7.10.4",
"@babel/runtime": "^7.11.2",
"@babel/types": "^7.11.5",
"@babel/generator": "^7.20.7",
"@babel/parser": "^7.20.12",
"@babel/plugin-syntax-jsx": "^7.18.6",
"@babel/runtime": "^7.20.13",
"@babel/types": "^7.20.7",
"@babel/core": "^7.20.12",
"@lingui/babel-plugin-extract-messages": "3.16.1",
"@lingui/conf": "3.16.1",
"@lingui/core": "3.16.1",
"babel-plugin-macros": "^3.0.1",
"babel-plugin-macros": "^3.1.0",
"bcp-47": "^1.0.7",
"chalk": "^4.1.0",
"chokidar": "3.5.1",
Expand All @@ -70,7 +71,6 @@
"normalize-path": "^3.0.0",
"ora": "^5.1.0",
"papaparse": "^5.3.0",
"pkg-up": "^3.1.0",
"plurals-cldr": "^1.0.4",
"pofile": "^1.1.0",
"pseudolocale": "^1.1.0",
Expand All @@ -84,10 +84,5 @@
"mockdate": "^3.0.2",
"typescript": "^4.0.3",
"fs-extra": "^9.0.1"
},
"peerDependencies": {
"@babel/core": "^7.0.0",
"babel-plugin-macros": "2 || 3",
"typescript": "2 || 3 || 4"
}
}
79 changes: 78 additions & 1 deletion packages/cli/src/api/__snapshots__/catalog.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,84 @@ Object {
}
`;

exports[`Catalog collect should handle errors 1`] = `undefined`;
exports[`Catalog collect should support Flow syntax if enabled 1`] = `Object {}`;

exports[`Catalog collect should support JSX and Typescript 1`] = `
Object {
Description: Object {
extractedComments: Array [
description,
],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
6,
],
],
},
Hi, my name is {name}: Object {
extractedComments: Array [],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
17,
],
],
},
ID Some: Object {
extractedComments: Array [],
message: Message with id some,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
11,
],
],
},
Message: Object {
extractedComments: Array [],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
4,
],
],
},
Some message: Object {
extractedComments: Array [],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
18,
],
],
},
Title: Object {
extractedComments: Array [],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
19,
],
],
},
{count, plural, one {# book} other {# books}}: Object {
extractedComments: Array [],
message: undefined,
origin: Array [
Array [
collect-typescript-jsx/macro.tsx,
21,
],
],
},
}
`;

exports[`Catalog make should collect and write catalogs 1`] = `
Object {
Expand Down
43 changes: 39 additions & 4 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,43 @@ describe("Catalog", () => {
})

describe("collect", () => {
it("should support JSX and Typescript", async () => {
process.env.LINGUI_CONFIG = path.join(__dirname, 'fixtures/collect-typescript-jsx/lingui.config.js')
const catalog = new Catalog(
{
name: "messages",
path: "locales/{locale}",
include: [fixture("collect-typescript-jsx/")],
exclude: [],
},
mockConfig()
)

const messages = await catalog.collect()
expect(messages).toBeTruthy()
expect(messages).toMatchSnapshot()
})

it("should support Flow syntax if enabled", async () => {
process.env.LINGUI_CONFIG = path.join(__dirname, 'fixtures/collect-syntax-flow/lingui.config.js')
const catalog = new Catalog(
{
name: "messages",
path: "locales/{locale}",
include: [fixture("collect-syntax-flow/")],
exclude: [],
},
mockConfig({
extractorParserOptions: {
flow: true
}
})
)

const messages = await catalog.collect()
expect(messages).toBeTruthy()
expect(messages).toMatchSnapshot()
})
it("should extract messages from source files", async () => {
const catalog = new Catalog(
{
Expand All @@ -206,7 +243,7 @@ describe("Catalog", () => {
mockConfig()
)

const messages = await catalog.collect(defaultMakeOptions)
const messages = await catalog.collect()
expect(messages).toMatchSnapshot()
})

Expand All @@ -225,7 +262,6 @@ describe("Catalog", () => {
)

const messages = await catalog.collect({
...defaultMakeOptions,
files: [fixture("collect/componentA")],
})
expect(messages).toMatchSnapshot()
Expand All @@ -246,7 +282,6 @@ describe("Catalog", () => {

mockConsole(async (console) => {
await catalog.collect({
...defaultMakeOptions,
files: [fixture("duplicate-id.js")]
})

Expand All @@ -268,7 +303,7 @@ describe("Catalog", () => {
)

mockConsole(async (console) => {
const messages = await catalog.collect(defaultMakeOptions)
const messages = await catalog.collect()
expect(console.error).toBeCalledWith(
expect.stringContaining(`Cannot process file`)
)
Expand Down
17 changes: 6 additions & 11 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import normalize from "normalize-path"
import {LinguiConfig, OrderBy, FallbackLocales, LocaleObject} from "@lingui/conf"

import getFormat, {CatalogFormatOptionsInternal, CatalogFormatter} from "./formats"
import extract, {ExtractedMessage} from "./extractors"
import extract, {ExtractedMessage, ExtractorType} from "./extractors"
import { CliExtractOptions } from "../lingui-extract"
import { CliExtractTemplateOptions } from "../lingui-extract-template"
import { CompiledCatalogNamespace } from "./compile"
Expand Down Expand Up @@ -53,16 +53,13 @@ export type AllCatalogsType = {
}

export type MakeOptions = CliExtractOptions & {
projectType?: string
orderBy?: OrderBy
}

export type MakeTemplateOptions = CliExtractTemplateOptions & {
projectType?: string
orderBy?: OrderBy
}

type CollectOptions = MakeOptions | MakeTemplateOptions

export type MergeOptions = {
overwrite: boolean
Expand Down Expand Up @@ -114,7 +111,7 @@ export class Catalog {
}

async make(options: MakeOptions): Promise<boolean> {
const nextCatalog = await this.collect(options)
const nextCatalog = await this.collect({files: options.files})
if (!nextCatalog) return false
const prevCatalogs = this.readAll()

Expand Down Expand Up @@ -144,7 +141,7 @@ export class Catalog {
}

async makeTemplate(options: MakeTemplateOptions): Promise<boolean> {
const catalog = await this.collect(options)
const catalog = await this.collect({files: options.files})
if (!catalog) return false
const sort = order<CatalogType>(options.orderBy)
this.writeTemplate(sort(catalog as CatalogType))
Expand All @@ -154,7 +151,7 @@ export class Catalog {
/**
* Collect messages from source paths. Return a raw message catalog as JSON.
*/
async collect(options: CollectOptions): Promise<ExtractedCatalogType | undefined> {
async collect(options: { files?: string[] } = {}): Promise<ExtractedCatalogType | undefined> {
const messages: ExtractedCatalogType = {}

let paths = this.sourcePaths
Expand Down Expand Up @@ -199,10 +196,8 @@ export class Catalog {
origin: [...prev.origin, [filename, next.origin[1]]]
}
}, {
verbose: options.verbose,
babelOptions: this.config.extractBabelOptions,
extractors: options.extractors,
projectType: options.projectType
parserOptions: this.config.extractorParserOptions,
extractors: this.config.extractors as ExtractorType[],
})
catalogSuccess &&= fileSuccess
}
Expand Down
30 changes: 0 additions & 30 deletions packages/cli/src/api/detect.test.ts

This file was deleted.

Loading

0 comments on commit fd76a91

Please sign in to comment.