forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10n-no-uistrings-export.js
68 lines (60 loc) · 2.2 KB
/
l10n-no-uistrings-export.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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 MODULE_UI_STRINGS_FILENAME_REGEX = /ModuleUIStrings\.(js|ts)$/;
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'UIStrings object literals are only allowed to be exported from ModuleUIStrings.(js|ts)',
category: 'Possible Errors',
},
fixable: 'code',
schema: [] // no options
},
create: function(context) {
const filename = context.filename ?? context.getFilename();
const sourceCode = context.sourceCode ?? context.getSourceCode();
function removeExportKeywordFromUIStrings(fixer, exportNamedDeclaration) {
const exportToken = sourceCode.getFirstToken(exportNamedDeclaration);
if (exportToken.type === 'Keyword' && exportToken.value === 'export') {
return fixer.remove(exportToken);
}
return [];
}
return {
ExportNamedDeclaration(exportNamedDeclaration) {
if (MODULE_UI_STRINGS_FILENAME_REGEX.test(filename)) {
return;
}
const declaration = exportNamedDeclaration.declaration;
if (declaration?.type !== 'VariableDeclaration') {
return;
}
if (declaration.declarations.length === 0 || declaration.declarations[0].type !== 'VariableDeclarator') {
return;
}
if (l10nHelper.isUIStringsIdentifier(declaration.declarations[0].id)) {
context.report({
node: declaration.declarations[0],
message: 'Exporting the UIStrings object is only allowed in ModuleUIStrings.(js|ts)',
fix: fixer => removeExportKeywordFromUIStrings(fixer, exportNamedDeclaration),
});
}
},
ExportSpecifier(specifier) {
if (specifier.local?.type === 'Identifier' && specifier.local.name === 'UIStrings') {
context.report({
node: specifier,
message: 'Exporting the UIStrings object is only allowed in ModuleUIStrings.(js|ts)',
});
}
}
};
}
};