From 11570fc48ff9e44dc8a44d77248bf1d4d4a53228 Mon Sep 17 00:00:00 2001 From: Andreas Pelme Date: Tue, 12 May 2026 07:54:45 +0200 Subject: [PATCH] Support TC39 decorators This commit adapts the babel configuration to support the syntax propsed by the TC39 decorator proposal: https://github.com/tc39/proposal-decorators This proposal is now stage 3 (recommended to be implemented). It is likely that this is the syntax that will be used for a decorators for a long time. The syntax is similar (using the @decorator) to the "legacy" decorators so this should be backward compatible with code using the old decorators. Fixes #168. --- src/defaults.ts | 2 +- tests/commands/__snapshots__/test_extract.ts.snap | 12 ++++++++++++ tests/commands/test_extract.ts | 10 ++++++++++ tests/fixtures/testDecoratorParse.js | 12 ++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/testDecoratorParse.js diff --git a/src/defaults.ts b/src/defaults.ts index 6cc98e6..85784c7 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -13,7 +13,7 @@ import * as babelTtagPlugin from "babel-plugin-ttag"; import * as babelPluginDecorators from "@babel/plugin-proposal-decorators"; export const defaultPlugins: ConfigItem[] = [ - [babelPluginDecorators, { legacy: true }], + [babelPluginDecorators, { version: "2023-11" }], exportDefaultFromPlugin, ]; diff --git a/tests/commands/__snapshots__/test_extract.ts.snap b/tests/commands/__snapshots__/test_extract.ts.snap index aa3cd5b..5da9561 100644 --- a/tests/commands/__snapshots__/test_extract.ts.snap +++ b/tests/commands/__snapshots__/test_extract.ts.snap @@ -192,6 +192,18 @@ msgstr \\"\\" " `; +exports[`extract with decorator 1`] = ` +"msgid \\"\\" +msgstr \\"\\" +\\"Content-Type: text/plain; charset=utf-8\\\\n\\" +\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\" + +#: tests/fixtures/testDecoratorParse.js:3 +msgid \\"hi from a module with decorators\\" +msgstr \\"\\" +" +`; + exports[`should extract in the alphabetical order (sortByMsgid) 1`] = ` "msgid \\"\\" msgstr \\"\\" diff --git a/tests/commands/test_extract.ts b/tests/commands/test_extract.ts index 4af1828..63cf662 100644 --- a/tests/commands/test_extract.ts +++ b/tests/commands/test_extract.ts @@ -8,6 +8,10 @@ const baseTestPath = path.resolve(__dirname, "../fixtures/baseTest"); const sortByMsgidPath = path.resolve(__dirname, "../fixtures/sortByMsgidTest"); const ukTestPath = path.resolve(__dirname, "../fixtures/ukLocaleTest"); const jsxPath = path.resolve(__dirname, "../fixtures/testJSXParse.jsx"); +const decoratorPath = path.resolve( + __dirname, + "../fixtures/testDecoratorParse.js" +); const vuePath = path.resolve(__dirname, "../fixtures/vueTest/testVueParse.vue"); const vuePath2 = path.resolve( __dirname, @@ -48,6 +52,12 @@ test("extract from jsx", () => { expect(result).toMatchSnapshot(); }); +test("extract with decorator", () => { + execSync(`ts-node src/index.ts extract -o ${potPath} ${decoratorPath}`); + const result = fs.readFileSync(potPath).toString(); + expect(result).toMatchSnapshot(); +}); + test("extract from vue", () => { execSync(`ts-node src/index.ts extract -o ${potPath} ${vuePath}`); const result = fs.readFileSync(potPath).toString(); diff --git a/tests/fixtures/testDecoratorParse.js b/tests/fixtures/testDecoratorParse.js new file mode 100644 index 0000000..425f347 --- /dev/null +++ b/tests/fixtures/testDecoratorParse.js @@ -0,0 +1,12 @@ +import { t } from 'ttag'; + +const x = t`hi from a module with decorators` + +@myClassDecorator +class MyClass { + @myFieldDecorator myField; + @myAccessorDecortor accessor myAccessor; + @myGetterDecorator get myGetter() { } + @mySetterDecorator set mySetter(x) { } + @methodDecorator hi() { } +}