forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10n-no-locked-or-placeholder-only-phrase.js
52 lines (46 loc) · 1.64 KB
/
l10n-no-locked-or-placeholder-only-phrase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
const l10nHelper = require('./l10n-helper.js');
const FULLY_LOCKED_PHRASE_REGEX = /^`[^`]*`$/;
const SINGLE_PLACEHOLDER_REGEX = /^\{\w+\}$/; // Matches the PH regex in `collect-strings.js`.
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'UIStrings object literals are not allowed to have phrases that are fully locked, or consist only of a single placeholder.',
category: 'Possible Errors',
},
schema: [] // no options
},
create: function(context) {
return {
VariableDeclarator(variableDeclarator) {
if (!l10nHelper.isUIStringsVariableDeclarator(context, variableDeclarator)) {
return;
}
for (const property of variableDeclarator.init.properties) {
if (property.type !== 'Property' || property.value.type !== 'Literal') {
continue;
}
if (FULLY_LOCKED_PHRASE_REGEX.test(property.value.value)) {
context.report({
node: property.value,
message: 'Locking whole phrases is not allowed. Use i18n.i18n.lockedString instead.',
});
} else if (SINGLE_PLACEHOLDER_REGEX.test(property.value.value)) {
context.report({
node: property.value,
message: 'Single placeholder-only phrases are not allowed. Use i18n.i18n.lockedString instead.',
});
}
}
},
};
}
};