Skip to content

Commit fd67dd9

Browse files
Merge pull request #2683 from johanrd/day_fix/template-require-lang-attribute
Post-merge-review: Fix template-require-lang-attribute: validate every BCP47 subtag from 'language-tags'
2 parents 623705b + 4fab3be commit fd67dd9

File tree

4 files changed

+39
-189
lines changed

4 files changed

+39
-189
lines changed

lib/rules/template-require-lang-attribute.js

Lines changed: 2 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,4 @@
1-
// Common valid BCP 47 language tags (not exhaustive, but covers the most common)
2-
const COMMON_LANG_CODES = new Set([
3-
'aa',
4-
'ab',
5-
'af',
6-
'ak',
7-
'am',
8-
'an',
9-
'ar',
10-
'as',
11-
'av',
12-
'ay',
13-
'az',
14-
'ba',
15-
'be',
16-
'bg',
17-
'bh',
18-
'bi',
19-
'bm',
20-
'bn',
21-
'bo',
22-
'br',
23-
'bs',
24-
'ca',
25-
'ce',
26-
'ch',
27-
'co',
28-
'cr',
29-
'cs',
30-
'cu',
31-
'cv',
32-
'cy',
33-
'da',
34-
'de',
35-
'dv',
36-
'dz',
37-
'ee',
38-
'el',
39-
'en',
40-
'eo',
41-
'es',
42-
'et',
43-
'eu',
44-
'fa',
45-
'ff',
46-
'fi',
47-
'fj',
48-
'fo',
49-
'fr',
50-
'fy',
51-
'ga',
52-
'gd',
53-
'gl',
54-
'gn',
55-
'gu',
56-
'gv',
57-
'ha',
58-
'he',
59-
'hi',
60-
'ho',
61-
'hr',
62-
'ht',
63-
'hu',
64-
'hy',
65-
'hz',
66-
'ia',
67-
'id',
68-
'ie',
69-
'ig',
70-
'ii',
71-
'ik',
72-
'io',
73-
'is',
74-
'it',
75-
'iu',
76-
'ja',
77-
'jv',
78-
'ka',
79-
'kg',
80-
'ki',
81-
'kj',
82-
'kk',
83-
'kl',
84-
'km',
85-
'kn',
86-
'ko',
87-
'kr',
88-
'ks',
89-
'ku',
90-
'kv',
91-
'kw',
92-
'ky',
93-
'la',
94-
'lb',
95-
'lg',
96-
'li',
97-
'ln',
98-
'lo',
99-
'lt',
100-
'lu',
101-
'lv',
102-
'mg',
103-
'mh',
104-
'mi',
105-
'mk',
106-
'ml',
107-
'mn',
108-
'mr',
109-
'ms',
110-
'mt',
111-
'my',
112-
'na',
113-
'nb',
114-
'nd',
115-
'ne',
116-
'ng',
117-
'nl',
118-
'nn',
119-
'no',
120-
'nr',
121-
'nv',
122-
'ny',
123-
'oc',
124-
'oj',
125-
'om',
126-
'or',
127-
'os',
128-
'pa',
129-
'pi',
130-
'pl',
131-
'ps',
132-
'pt',
133-
'qu',
134-
'rm',
135-
'rn',
136-
'ro',
137-
'ru',
138-
'rw',
139-
'sa',
140-
'sc',
141-
'sd',
142-
'se',
143-
'sg',
144-
'si',
145-
'sk',
146-
'sl',
147-
'sm',
148-
'sn',
149-
'so',
150-
'sq',
151-
'sr',
152-
'ss',
153-
'st',
154-
'su',
155-
'sv',
156-
'sw',
157-
'ta',
158-
'te',
159-
'tg',
160-
'th',
161-
'ti',
162-
'tk',
163-
'tl',
164-
'tn',
165-
'to',
166-
'tr',
167-
'ts',
168-
'tt',
169-
'tw',
170-
'ty',
171-
'ug',
172-
'uk',
173-
'ur',
174-
'uz',
175-
've',
176-
'vi',
177-
'vo',
178-
'wa',
179-
'wo',
180-
'xh',
181-
'yi',
182-
'yo',
183-
'za',
184-
'zh',
185-
'zu',
186-
]);
1+
const tags = require('language-tags');
1872

1883
const DEFAULT_CONFIG = {
1894
validateValues: true,
@@ -193,9 +8,7 @@ function isValidLangTag(value) {
1938
if (!value || !value.trim()) {
1949
return false;
19510
}
196-
const parts = value.trim().toLowerCase().split('-');
197-
198-
return COMMON_LANG_CODES.has(parts[0]);
11+
return tags(value.trim()).valid();
19912
}
20013

20114
function parseConfig(config) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"eslint-utils": "^3.0.0",
7272
"estraverse": "^5.3.0",
7373
"html-tags": "^3.3.1",
74+
"language-tags": "^1.0.9",
7475
"lodash.camelcase": "^4.3.0",
7576
"lodash.kebabcase": "^4.1.1",
7677
"requireindex": "^1.2.0",

pnpm-lock.yaml

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

tests/lib/rules/template-require-lang-attribute.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ruleTester.run('template-require-lang-attribute', rule, {
1313
'<template><html lang="en"></html></template>',
1414
'<template><html lang="en-US"></html></template>',
1515
'<template><html lang="DE-BW"></html></template>',
16+
'<template><html lang="zh-Hant-HK"></html></template>',
17+
'<template><html lang="yue-Hans"></html></template>',
1618
'<template><html lang={{lang}}></html></template>',
1719
{
1820
code: '<template><html lang="de"></html></template>',
@@ -66,6 +68,14 @@ ruleTester.run('template-require-lang-attribute', rule, {
6668
options: [{ validateValues: true }],
6769
errors: [{ message: ERROR_MESSAGE }],
6870
},
71+
{
72+
// Invalid region subtag: "xx" is not a registered ISO 3166 / BCP 47
73+
// region code. Prior to the country-codes port, the rule only
74+
// validated the primary subtag and incorrectly accepted this value.
75+
code: '<template><html lang="en-XX"></html></template>',
76+
output: null,
77+
errors: [{ message: ERROR_MESSAGE }],
78+
},
6979
{
7080
code: '<template><html></html></template>',
7181
output: null,
@@ -94,6 +104,8 @@ hbsRuleTester.run('template-require-lang-attribute', rule, {
94104
'<html lang="en"></html>',
95105
'<html lang="en-US"></html>',
96106
'<html lang="DE-BW"></html>',
107+
'<html lang="zh-Hant-HK"></html>',
108+
'<html lang="yue-Hans"></html>',
97109
'<html lang={{lang}}></html>',
98110
{
99111
code: '<html lang="de"></html>',
@@ -146,6 +158,14 @@ hbsRuleTester.run('template-require-lang-attribute', rule, {
146158
options: [{ validateValues: true }],
147159
errors: [{ message: ERROR_MESSAGE }],
148160
},
161+
{
162+
// Invalid region subtag: "xx" is not a registered ISO 3166 / BCP 47
163+
// region code. Prior to the country-codes port, the rule only
164+
// validated the primary subtag and incorrectly accepted this value.
165+
code: '<html lang="en-XX"></html>',
166+
output: null,
167+
errors: [{ message: ERROR_MESSAGE }],
168+
},
149169
{
150170
code: '<html></html>',
151171
output: null,

0 commit comments

Comments
 (0)