forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10n-filename-matches.js
112 lines (101 loc) · 3.22 KB
/
l10n-filename-matches.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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 path = require('path');
const {isModuleScope} = require('./l10n-helper.js');
// True iff the callExpression is `i18n.i18n.registerUIStrings`.
function isRegisterUIStringsCall(callExpression) {
if (callExpression.callee?.property?.name !== 'registerUIStrings') {
return false;
}
if (callExpression.callee?.object?.property?.name !== 'i18n') {
return false;
}
if (callExpression.callee?.object?.object?.name !== 'i18n') {
return false;
}
return true;
}
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'i18n.i18n.registerUIStrings must receive the current file\'s path as the first argument',
category: 'Possible Errors',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
rootFrontendDirectory: {
type: 'string',
},
},
additionalProperties: false,
},
],
},
create: function (context) {
const filename = context.filename ?? context.getFilename();
return {
CallExpression(callExpression) {
if (
!isModuleScope(context, callExpression) ||
!isRegisterUIStringsCall(callExpression)
) {
return;
}
// Do nothing if there are no arguments or the first argument is not a string literal we
// can check.
if (
callExpression.arguments.length === 0 ||
callExpression.arguments[0].type !== 'Literal'
) {
return;
}
let frontEndDirectory = '';
if (context.options && context.options[0]?.rootFrontendDirectory) {
frontEndDirectory = context.options[0].rootFrontendDirectory;
}
if (!frontEndDirectory) {
throw new Error('rootFrontendDirectory must be provided.');
}
const currentSourceFile = path.resolve(filename);
const currentFileRelativeToFrontEnd = path.relative(
frontEndDirectory,
currentSourceFile,
);
const currentModuleDirectory = path.dirname(currentSourceFile);
const allowedPathArguments = [
currentSourceFile,
path.join(currentModuleDirectory, 'ModuleUIStrings.js'),
path.join(currentModuleDirectory, 'ModuleUIStrings.ts'),
];
const previousFileLocationArgument = callExpression.arguments[0];
const actualPath = path.join(
frontEndDirectory,
previousFileLocationArgument.value,
);
if (!allowedPathArguments.includes(actualPath)) {
const newFileName = currentFileRelativeToFrontEnd.replace(/\\/g, '/');
context.report({
node: callExpression,
message: `First argument to 'registerUIStrings' call must be '${newFileName}' or the ModuleUIStrings.(js|ts)`,
fix(fixer) {
return fixer.replaceText(
previousFileLocationArgument,
`'${newFileName}'`,
);
},
});
}
},
};
},
};