Skip to content
Open
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
191 changes: 2 additions & 189 deletions lib/rules/template-require-lang-attribute.js
Original file line number Diff line number Diff line change
@@ -1,189 +1,4 @@
// Common valid BCP 47 language tags (not exhaustive, but covers the most common)
const COMMON_LANG_CODES = new Set([
'aa',
'ab',
'af',
'ak',
'am',
'an',
'ar',
'as',
'av',
'ay',
'az',
'ba',
'be',
'bg',
'bh',
'bi',
'bm',
'bn',
'bo',
'br',
'bs',
'ca',
'ce',
'ch',
'co',
'cr',
'cs',
'cu',
'cv',
'cy',
'da',
'de',
'dv',
'dz',
'ee',
'el',
'en',
'eo',
'es',
'et',
'eu',
'fa',
'ff',
'fi',
'fj',
'fo',
'fr',
'fy',
'ga',
'gd',
'gl',
'gn',
'gu',
'gv',
'ha',
'he',
'hi',
'ho',
'hr',
'ht',
'hu',
'hy',
'hz',
'ia',
'id',
'ie',
'ig',
'ii',
'ik',
'io',
'is',
'it',
'iu',
'ja',
'jv',
'ka',
'kg',
'ki',
'kj',
'kk',
'kl',
'km',
'kn',
'ko',
'kr',
'ks',
'ku',
'kv',
'kw',
'ky',
'la',
'lb',
'lg',
'li',
'ln',
'lo',
'lt',
'lu',
'lv',
'mg',
'mh',
'mi',
'mk',
'ml',
'mn',
'mr',
'ms',
'mt',
'my',
'na',
'nb',
'nd',
'ne',
'ng',
'nl',
'nn',
'no',
'nr',
'nv',
'ny',
'oc',
'oj',
'om',
'or',
'os',
'pa',
'pi',
'pl',
'ps',
'pt',
'qu',
'rm',
'rn',
'ro',
'ru',
'rw',
'sa',
'sc',
'sd',
'se',
'sg',
'si',
'sk',
'sl',
'sm',
'sn',
'so',
'sq',
'sr',
'ss',
'st',
'su',
'sv',
'sw',
'ta',
'te',
'tg',
'th',
'ti',
'tk',
'tl',
'tn',
'to',
'tr',
'ts',
'tt',
'tw',
'ty',
'ug',
'uk',
'ur',
'uz',
've',
'vi',
'vo',
'wa',
'wo',
'xh',
'yi',
'yo',
'za',
'zh',
'zu',
]);
const tags = require('language-tags');

const DEFAULT_CONFIG = {
validateValues: true,
Expand All @@ -193,9 +8,7 @@ function isValidLangTag(value) {
if (!value || !value.trim()) {
return false;
}
const parts = value.trim().toLowerCase().split('-');

return COMMON_LANG_CODES.has(parts[0]);
return tags(value.trim()).valid();
}

function parseConfig(config) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"eslint-utils": "^3.0.0",
"estraverse": "^5.3.0",
"html-tags": "^3.3.1",
"language-tags": "^1.0.9",
"lodash.camelcase": "^4.3.0",
"lodash.kebabcase": "^4.1.1",
"requireindex": "^1.2.0",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/lib/rules/template-require-lang-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ruleTester.run('template-require-lang-attribute', rule, {
'<template><html lang="en"></html></template>',
'<template><html lang="en-US"></html></template>',
'<template><html lang="DE-BW"></html></template>',
'<template><html lang="zh-Hant-HK"></html></template>',
'<template><html lang="yue-Hans"></html></template>',
'<template><html lang={{lang}}></html></template>',
{
code: '<template><html lang="de"></html></template>',
Expand Down Expand Up @@ -66,6 +68,14 @@ ruleTester.run('template-require-lang-attribute', rule, {
options: [{ validateValues: true }],
errors: [{ message: ERROR_MESSAGE }],
},
{
// Invalid region subtag: "xx" is not a registered ISO 3166 / BCP 47
// region code. Prior to the country-codes port, the rule only
// validated the primary subtag and incorrectly accepted this value.
code: '<template><html lang="en-XX"></html></template>',
output: null,
errors: [{ message: ERROR_MESSAGE }],
},
{
code: '<template><html></html></template>',
output: null,
Expand Down Expand Up @@ -94,6 +104,8 @@ hbsRuleTester.run('template-require-lang-attribute', rule, {
'<html lang="en"></html>',
'<html lang="en-US"></html>',
'<html lang="DE-BW"></html>',
'<html lang="zh-Hant-HK"></html>',
'<html lang="yue-Hans"></html>',
'<html lang={{lang}}></html>',
{
code: '<html lang="de"></html>',
Expand Down Expand Up @@ -146,6 +158,14 @@ hbsRuleTester.run('template-require-lang-attribute', rule, {
options: [{ validateValues: true }],
errors: [{ message: ERROR_MESSAGE }],
},
{
// Invalid region subtag: "xx" is not a registered ISO 3166 / BCP 47
// region code. Prior to the country-codes port, the rule only
// validated the primary subtag and incorrectly accepted this value.
code: '<html lang="en-XX"></html>',
output: null,
errors: [{ message: ERROR_MESSAGE }],
},
{
code: '<html></html>',
output: null,
Expand Down
Loading