-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathl10n-filename-matches.ts
More file actions
136 lines (120 loc) · 3.91 KB
/
l10n-filename-matches.ts
File metadata and controls
136 lines (120 loc) · 3.91 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/utils';
import * as path from 'node:path';
import {isModuleScope} from './utils/l10n-helper.ts';
import {createRule} from './utils/ruleCreator.ts';
type CallExpression = TSESTree.CallExpression;
/** True iff the callExpression is `i18n.i18n.registerUIStrings`. **/
function isRegisterUIStringsCall(callExpression: CallExpression): boolean {
if (callExpression.callee?.type !== 'MemberExpression') {
return false;
}
const callee = callExpression.callee;
if (callee.property?.type !== 'Identifier' || callee.property?.name !== 'registerUIStrings') {
return false;
}
if (callee.object?.type !== 'MemberExpression') {
return false;
}
const calleeObject = callee.object;
if (calleeObject.property?.type !== 'Identifier' || calleeObject.property?.name !== 'i18n') {
return false;
}
if (calleeObject.object?.type !== 'Identifier' || calleeObject.object?.name !== 'i18n') {
return false;
}
return true;
}
type Options = [
{
rootFrontendDirectory?: string,
},
];
type MessageIds = 'pathMismatch';
export default createRule<Options, MessageIds>({
name: 'l10n-filename-matches',
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,
},
],
messages: {
// Using a generic message ID as the message itself is dynamic
pathMismatch:
'First argument to \'registerUIStrings\' call must be \'{{expectedPath}}\' or the ModuleUIStrings.(js|ts)',
},
},
defaultOptions: [{}],
create: function(context) {
const filename = context.filename;
let frontEndDirectory = '';
if (context.options?.[0]?.rootFrontendDirectory) {
frontEndDirectory = context.options[0].rootFrontendDirectory;
}
if (!frontEndDirectory) {
throw new Error(
'\'rootFrontendDirectory\' option must be provided for the l10n-filename-matches rule.',
);
}
return {
CallExpression(node) {
if (!isModuleScope(context, node) || !isRegisterUIStringsCall(node)) {
return;
}
// Do nothing if there are no arguments or the first argument is not a string literal we
// can check.
const firstArgument = node.arguments[0];
if (node.arguments.length === 0 || firstArgument?.type !== 'Literal' ||
typeof firstArgument.value !== 'string') {
return;
}
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 actualPath = path.join(
frontEndDirectory,
`${firstArgument.value}`,
);
if (!allowedPathArguments.includes(actualPath)) {
const newFileName = currentFileRelativeToFrontEnd.replace(/\\/g, '/');
context.report({
node,
messageId: 'pathMismatch',
data: {
expectedPath: newFileName,
},
fix(fixer) {
return fixer.replaceText(
firstArgument,
`'${newFileName}'`,
);
},
});
}
},
};
},
});