babel-plugin-formatjs 10.3.25 → 10.3.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/BUILD +90 -0
  2. package/CHANGELOG.md +1139 -0
  3. package/LICENSE.md +0 -0
  4. package/README.md +0 -0
  5. package/global.d.ts +0 -0
  6. package/index.ts +88 -0
  7. package/integration-tests/BUILD +21 -0
  8. package/integration-tests/package.json +5 -0
  9. package/integration-tests/vue/fixtures/App.vue +19 -0
  10. package/integration-tests/vue/fixtures/app.js +6 -0
  11. package/integration-tests/vue/integration.test.ts +62 -0
  12. package/package.json +5 -4
  13. package/tests/__snapshots__/index.test.ts.snap +1246 -0
  14. package/tests/fixtures/2663.js +3 -0
  15. package/tests/fixtures/FormattedMessage.js +14 -0
  16. package/tests/fixtures/additionalComponentNames.js +15 -0
  17. package/tests/fixtures/additionalFunctionNames.js +23 -0
  18. package/tests/fixtures/ast.js +45 -0
  19. package/tests/fixtures/defineMessage.js +57 -0
  20. package/tests/fixtures/defineMessages.js +49 -0
  21. package/tests/fixtures/descriptionsAsObjects.js +18 -0
  22. package/tests/fixtures/empty.js +8 -0
  23. package/tests/fixtures/extractFromFormatMessageCall.js +47 -0
  24. package/tests/fixtures/extractFromFormatMessageCallStateless.js +46 -0
  25. package/tests/fixtures/extractSourceLocation.js +8 -0
  26. package/tests/fixtures/formatMessageCall.js +38 -0
  27. package/tests/fixtures/icuSyntax.js +18 -0
  28. package/tests/fixtures/idInterpolationPattern.js +40 -0
  29. package/tests/fixtures/inline.js +26 -0
  30. package/tests/fixtures/overrideIdFn.js +48 -0
  31. package/tests/fixtures/preserveWhitespace.js +79 -0
  32. package/tests/fixtures/removeDefaultMessage.js +36 -0
  33. package/tests/fixtures/skipExtractionFormattedMessage.js +12 -0
  34. package/tests/fixtures/templateLiteral.js +21 -0
  35. package/tests/index.test.ts +221 -0
  36. package/tsconfig.json +5 -0
  37. package/types.ts +46 -0
  38. package/utils.ts +226 -0
  39. package/visitors/call-expression.ts +208 -0
  40. package/visitors/jsx-opening-element.ts +147 -0
  41. package/index.d.ts +0 -10
  42. package/index.d.ts.map +0 -1
  43. package/index.js +0 -74
  44. package/types.d.ts +0 -31
  45. package/types.d.ts.map +0 -1
  46. package/types.js +0 -2
  47. package/utils.d.ts +0 -34
  48. package/utils.d.ts.map +0 -1
  49. package/utils.js +0 -155
  50. package/visitors/call-expression.d.ts +0 -6
  51. package/visitors/call-expression.d.ts.map +0 -1
  52. package/visitors/call-expression.js +0 -127
  53. package/visitors/jsx-opening-element.d.ts +0 -6
  54. package/visitors/jsx-opening-element.d.ts.map +0 -1
  55. package/visitors/jsx-opening-element.js +0 -89
package/LICENSE.md CHANGED
File without changes
package/README.md CHANGED
File without changes
package/global.d.ts CHANGED
File without changes
package/index.ts ADDED
@@ -0,0 +1,88 @@
1
+ import {declare} from '@babel/helper-plugin-utils'
2
+ import {PluginObj, PluginPass} from '@babel/core'
3
+ import babelPluginSyntaxJsx from '@babel/plugin-syntax-jsx'
4
+ import {ExtractedMessageDescriptor, Options, State} from './types'
5
+ import {visitor as JSXOpeningElement} from './visitors/jsx-opening-element'
6
+ import {visitor as CallExpression} from './visitors/call-expression'
7
+
8
+ export type ExtractionResult<M = Record<string, string>> = {
9
+ messages: ExtractedMessageDescriptor[]
10
+ meta: M
11
+ }
12
+
13
+ export const DEFAULT_ID_INTERPOLATION_PATTERN = '[sha512:contenthash:base64:6]'
14
+
15
+ // @ts-expect-error PluginPass doesn't allow custom state but it actually does
16
+ export default declare<Options, PluginObj>((api, options) => {
17
+ api.assertVersion(7)
18
+ if (!options.idInterpolationPattern) {
19
+ options.idInterpolationPattern = DEFAULT_ID_INTERPOLATION_PATTERN
20
+ }
21
+
22
+ const {pragma} = options
23
+ const componentNames = new Set<string>(options.additionalComponentNames)
24
+ componentNames.add('FormattedMessage')
25
+ const functionNames = new Set<string>(options.additionalFunctionNames)
26
+ functionNames.add('formatMessage')
27
+ // Vue
28
+ functionNames.add('$formatMessage')
29
+ return {
30
+ inherits: babelPluginSyntaxJsx,
31
+ pre() {
32
+ this.componentNames = Array.from(componentNames)
33
+ this.functionNames = Array.from(functionNames)
34
+ },
35
+
36
+ visitor: {
37
+ Program: {
38
+ enter(this: PluginPass & State, path) {
39
+ this.messages = []
40
+ this.meta = {}
41
+ if (!pragma) {
42
+ return
43
+ }
44
+ for (const {leadingComments} of path.node.body) {
45
+ if (!leadingComments) {
46
+ continue
47
+ }
48
+ const pragmaLineNode = leadingComments.find(c =>
49
+ c.value.includes(pragma)
50
+ )
51
+ if (!pragmaLineNode) {
52
+ continue
53
+ }
54
+
55
+ pragmaLineNode.value
56
+ .split(pragma)[1]
57
+ .trim()
58
+ .split(/\s+/g)
59
+ .forEach(kv => {
60
+ const [k, v] = kv.split(':')
61
+ this.meta[k] = v
62
+ })
63
+ }
64
+ },
65
+ exit(
66
+ this: PluginPass & State,
67
+ _,
68
+ {
69
+ opts: _opts,
70
+ file: {
71
+ opts: {filename},
72
+ },
73
+ }
74
+ ) {
75
+ const opts = _opts as Options
76
+ if (typeof opts?.onMetaExtracted === 'function') {
77
+ opts.onMetaExtracted(filename || '', this.meta)
78
+ }
79
+ if (typeof opts?.onMsgExtracted === 'function') {
80
+ opts.onMsgExtracted(filename || '', this.messages)
81
+ }
82
+ },
83
+ },
84
+ JSXOpeningElement,
85
+ CallExpression,
86
+ },
87
+ }
88
+ })
@@ -0,0 +1,21 @@
1
+ load("@npm//:defs.bzl", "npm_link_all_packages")
2
+ load("//tools:jest.bzl", "jest_test")
3
+
4
+ npm_link_all_packages(name = "node_modules")
5
+
6
+ jest_test(
7
+ name = "integration-vue",
8
+ srcs = ["vue/integration.test.ts"] + glob([
9
+ "vue/fixtures/*",
10
+ ]),
11
+ # flaky = True,
12
+ deps = [
13
+ ":node_modules/babel-plugin-formatjs",
14
+ "//:node_modules/@types/node",
15
+ "//:node_modules/@types/webpack",
16
+ "//:node_modules/babel-loader",
17
+ "//:node_modules/vue",
18
+ "//:node_modules/vue-loader",
19
+ "//:node_modules/webpack",
20
+ ],
21
+ )
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "babel-plugin-formatjs": "workspace:*"
4
+ }
5
+ }
@@ -0,0 +1,19 @@
1
+ <template>
2
+ <p>
3
+ {{
4
+ _ctx.formatMessage(
5
+ {id: 'myMessage', defaultMessage: 'Today is {ts, date,::yyyyMMdd}'},
6
+ {ts: Date.now()}
7
+ )
8
+ }}
9
+ <br />
10
+ {{ $formatNumber(19, {style: 'currency', currency: 'EUR'}) }}
11
+ </p>
12
+ </template>
13
+
14
+ <script>
15
+ intl.formatMessage({defaultMessage: 'blah'})
16
+ export default {
17
+ name: 'App',
18
+ }
19
+ </script>
@@ -0,0 +1,6 @@
1
+ import {createApp} from 'vue'
2
+ import App from './App.vue'
3
+
4
+ const app = createApp(App)
5
+
6
+ app.mount('#app')
@@ -0,0 +1,62 @@
1
+ import webpack from 'webpack'
2
+ import {VueLoaderPlugin} from 'vue-loader'
3
+ import {readFileSync} from 'fs'
4
+ import {join} from 'path'
5
+ test('dummy', function (done) {
6
+ console.log(require.resolve('babel-plugin-formatjs'))
7
+ webpack(
8
+ {
9
+ entry: require.resolve('./fixtures/app.js'),
10
+ output: {
11
+ filename: 'out.js',
12
+ },
13
+ module: {
14
+ rules: [
15
+ {
16
+ test: /\.vue$/,
17
+ loader: 'vue-loader',
18
+ },
19
+ // this will apply to both plain `.js` files
20
+ // AND `<script>` blocks in `.vue` files
21
+ {
22
+ test: /\.js$/,
23
+ loader: 'babel-loader',
24
+ options: {
25
+ plugins: [
26
+ [
27
+ require.resolve('babel-plugin-formatjs'),
28
+ {
29
+ idInterpolationPattern: '[sha512:contenthash:base64:6]',
30
+ ast: true,
31
+ },
32
+ ],
33
+ ],
34
+ },
35
+ },
36
+ ],
37
+ },
38
+ plugins: [new VueLoaderPlugin()],
39
+ },
40
+ function (err, stats) {
41
+ const statsJson = stats?.toJson()
42
+ if (!statsJson) {
43
+ throw new Error('missing stats')
44
+ }
45
+ if (err) {
46
+ throw err
47
+ }
48
+ if (stats?.hasErrors()) {
49
+ console.error(statsJson.errors)
50
+ throw new Error('err compiling')
51
+ }
52
+ const outFile = join(
53
+ statsJson.outputPath || __dirname,
54
+ statsJson.assets?.[0].name || 'out.js'
55
+ )
56
+ expect(readFileSync(outFile, 'utf-8')).toContain(
57
+ '[{type:0,value:"Today is "},{type:3,value:"ts",style:{type:1,pattern:"yyyyMMdd",parsedOptions:{year:"numeric",month:"2-digit",day:"2-digit"}}}]'
58
+ )
59
+ done()
60
+ }
61
+ )
62
+ }, 30000)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-formatjs",
3
- "version": "10.3.25",
3
+ "version": "10.3.26",
4
4
  "description": "Extracts string messages for translation from modules that use formatjs.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,10 +16,11 @@
16
16
  "@babel/plugin-syntax-jsx": "7",
17
17
  "@babel/traverse": "7",
18
18
  "@babel/types": "^7.12.11",
19
- "@formatjs/icu-messageformat-parser": "2.1.4",
20
- "@formatjs/ts-transformer": "3.9.9",
19
+ "@formatjs/icu-messageformat-parser": "2.1.5",
20
+ "@formatjs/ts-transformer": "3.9.10",
21
21
  "@types/babel__core": "^7.1.7",
22
22
  "@types/babel__helper-plugin-utils": "^7.10.0",
23
+ "@types/babel__traverse": "^7.1.7",
23
24
  "tslib": "2.4.0"
24
25
  },
25
26
  "keywords": [
@@ -31,4 +32,4 @@
31
32
  },
32
33
  "homepage": "https://github.com/formatjs/formatjs#readme",
33
34
  "gitHead": "8b0baec8eda5002715cf893274fe59782fc2d371"
34
- }
35
+ }