Skip to content

Commit eb589db

Browse files
committed
feat: change should-polyfill return value to 1 of the supported locales of the polyfill, fix formatjs#3255
1 parent e71a705 commit eb589db

File tree

24 files changed

+199
-24
lines changed

24 files changed

+199
-24
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packages/*/src/data/* linguist-generated
22
packages/*/test262-main.ts linguist-generated
3+
packages/*/supported-locales.ts linguist-generated
34
packages/*/tests/locale-data/* linguist-generated
45
website/* linguist-documentation
56
**/CHANGELOG.md linguist-documentation

BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ multirun(
148148
],
149149
)
150150

151+
multirun(
152+
name = "supported-locales-all.update",
153+
testonly = True,
154+
commands = [
155+
"//packages/intl-datetimeformat:supported-locales.update",
156+
"//packages/intl-displaynames:supported-locales.update",
157+
"//packages/intl-listformat:supported-locales.update",
158+
"//packages/intl-numberformat:supported-locales.update",
159+
"//packages/intl-pluralrules:supported-locales.update",
160+
"//packages/intl-relativetimeformat:supported-locales.update",
161+
],
162+
)
163+
151164
buildifier(
152165
name = "buildifier",
153166
exclude_patterns = ["./node_modules/*"],

packages/intl-datetimeformat/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ generate_src_file(
169169
],
170170
)
171171

172+
generate_src_file(
173+
name = "supported-locales",
174+
src = "supported-locales.ts",
175+
args = [
176+
"--cldrFolder",
177+
"$(location :cldr-raw)",
178+
],
179+
data = CLDR_DEPS,
180+
entry_point = "//tools:supported-locales-gen.ts",
181+
visibility = [
182+
"//:__pkg__",
183+
],
184+
)
185+
172186
# "ts-node scripts/link --input iana-data/backward --output src/links.ts"
173187
# links
174188
generate_src_file(
@@ -288,6 +302,7 @@ check_format(
288302
"tests/locale-data/*",
289303
"src/data/*",
290304
"test262-main.ts",
305+
"supported-locales.ts",
291306
],
292307
),
293308
)

packages/intl-datetimeformat/should-polyfill.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {match} from '@formatjs/intl-localematcher'
2+
import {supportedLocales} from './supported-locales'
3+
14
function supportsDateStyle() {
25
try {
36
return !!(
@@ -52,14 +55,16 @@ function supportedLocalesOf(locale?: string | string[]) {
5255
)
5356
}
5457

55-
export function shouldPolyfill(locale?: string | string[]) {
56-
return (
58+
export function shouldPolyfill(locale = 'en'): string | undefined {
59+
if (
5760
!('DateTimeFormat' in Intl) ||
5861
!('formatToParts' in Intl.DateTimeFormat.prototype) ||
5962
!('formatRange' in Intl.DateTimeFormat.prototype) ||
6063
hasChromeLt71Bug() ||
6164
hasUnthrownDateTimeStyleBug() ||
6265
!supportsDateStyle() ||
6366
!supportedLocalesOf(locale)
64-
)
67+
) {
68+
return locale ? match([locale], supportedLocales, 'en') : undefined
69+
}
6570
}

packages/intl-datetimeformat/supported-locales.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/intl-displaynames/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ generate_src_file(
156156
],
157157
)
158158

159+
generate_src_file(
160+
name = "supported-locales",
161+
src = "supported-locales.ts",
162+
args = [
163+
"--cldrFolder",
164+
"$(location :cldr-raw)",
165+
],
166+
data = CLDR_DEPS,
167+
entry_point = "//tools:supported-locales-gen.ts",
168+
visibility = [
169+
"//:__pkg__",
170+
],
171+
)
172+
159173
# Test262
160174
ts_project(
161175
name = "test262-main-bundle",
@@ -238,6 +252,7 @@ check_format(
238252
"CHANGELOG.md",
239253
"tests/locale-data/*",
240254
"test262-main.ts",
255+
"supported-locales.ts",
241256
],
242257
),
243258
)

packages/intl-displaynames/should-polyfill.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {match} from '@formatjs/intl-localematcher'
2+
import {supportedLocales} from './supported-locales'
3+
14
/**
25
* https://bugs.chromium.org/p/chromium/issues/detail?id=1097432
36
*/
@@ -39,11 +42,12 @@ function supportedLocalesOf(locale?: string | string[]) {
3942
)
4043
}
4144

42-
export function shouldPolyfill(locale?: string | string[]) {
43-
return (
44-
!(Intl as any).DisplayNames ||
45-
hasMissingICUBug() ||
46-
hasScriptBug() ||
47-
!supportedLocalesOf(locale)
48-
)
45+
export function _shouldPolyfillWithoutLocale() {
46+
return !(Intl as any).DisplayNames || hasMissingICUBug() || hasScriptBug()
47+
}
48+
49+
export function shouldPolyfill(locale = 'en') {
50+
if (_shouldPolyfillWithoutLocale() || !supportedLocalesOf(locale)) {
51+
return match([locale], supportedLocales, 'en')
52+
}
4953
}

packages/intl-displaynames/supported-locales.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/intl-displaynames/tests/should-polyfill.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {DisplayNames} from '..'
2-
import {shouldPolyfill} from '../should-polyfill'
2+
import {_shouldPolyfillWithoutLocale} from '../should-polyfill'
33

44
describe('before polyfill', function () {
55
it('should-polyfill should be true', function () {
66
// Node 14.9.0/browsers does have this bug
7-
expect(shouldPolyfill()).toBe(true)
7+
expect(_shouldPolyfillWithoutLocale()).toBeTruthy()
88
})
99
})
1010

@@ -18,6 +18,6 @@ describe('after polyfill', function () {
1818
;(Intl as any).DisplayNames = NativeDisplayNames
1919
})
2020
it('should fix the bug', function () {
21-
expect(shouldPolyfill()).toBe(false)
21+
expect(_shouldPolyfillWithoutLocale()).toBeFalsy()
2222
})
2323
})

packages/intl-listformat/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ ts_script(
135135
entry_point = "scripts/test-locale-data-gen.ts",
136136
) for locale in TEST_LOCALES]
137137

138+
generate_src_file(
139+
name = "supported-locales",
140+
src = "supported-locales.ts",
141+
args = [
142+
"--cldrFolder",
143+
"$(location :cldr-raw)",
144+
],
145+
data = CLDR_DEPS,
146+
entry_point = "//tools:supported-locales-gen.ts",
147+
visibility = [
148+
"//:__pkg__",
149+
],
150+
)
151+
138152
multirun(
139153
name = "tests-locale-data-all.update",
140154
testonly = True,
@@ -241,6 +255,7 @@ check_format(
241255
"CHANGELOG.md",
242256
"tests/locale-data/*",
243257
"test262-main.ts",
258+
"supported-locales.ts",
244259
],
245260
),
246261
)

0 commit comments

Comments
 (0)