Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(extractor/babel)!: sealed, non-fragile babel extractor options #1367

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@typescript-eslint/parser": "^5.50.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 @@ -18,7 +18,6 @@ const transformCode = (
filename = "test-case.js",
rootDir = "."
) => {
process.env.LINGUI_EXTRACT = "1"
process.env.LINGUI_CONFIG = path.join(
__dirname,
"fixtures",
Expand All @@ -45,6 +44,7 @@ const transformCode = (
[
"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 @@ -56,7 +56,6 @@ const transformCode = (
],
})
} finally {
process.env.LINGUI_EXTRACT = null
process.env.LINGUI_CONFIG = null
}

Expand Down
9 changes: 2 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@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.17.1",
"@lingui/conf": "3.17.1",
"@lingui/core": "3.17.1",
Expand All @@ -71,7 +72,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.4",
"pseudolocale": "^1.1.0",
Expand All @@ -83,11 +83,6 @@
"@types/papaparse": "^5.2.3",
"@types/plurals-cldr": "^1.0.1",
"mockdate": "^3.0.2",
"typescript": "^4.9.5"
},
"peerDependencies": {
"@babel/core": "^7.0.0",
"babel-plugin-macros": "2 || 3",
"typescript": "2 || 3 || 4"
"fs-extra": "^9.0.1"
}
}
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
49 changes: 45 additions & 4 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,49 @@ 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 @@ -209,7 +252,7 @@ describe("Catalog", () => {
mockConfig()
)

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

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

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

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

Expand All @@ -271,7 +312,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
18 changes: 6 additions & 12 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 @@ -54,17 +54,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
files?: string[]
Expand Down Expand Up @@ -115,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 @@ -145,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 @@ -156,7 +152,7 @@ export class Catalog {
* Collect messages from source paths. Return a raw message catalog as JSON.
*/
async collect(
options: CollectOptions
options: { files?: string[] } = {}
): Promise<ExtractedCatalogType | undefined> {
const messages: ExtractedCatalogType = {}

Expand Down Expand Up @@ -207,10 +203,8 @@ export class Catalog {
}
},
{
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.

60 changes: 0 additions & 60 deletions packages/cli/src/api/detect.ts

This file was deleted.

Loading