-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathjslog-context-list.ts
More file actions
189 lines (169 loc) · 6.11 KB
/
jslog-context-list.ts
File metadata and controls
189 lines (169 loc) · 6.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2024 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 fs from 'node:fs';
import path from 'node:path';
import {createRule} from './utils/ruleCreator.ts';
type Node = TSESTree.Node;
const FILE = 'front_end/ui/visual_logging/KnownContextValues.ts';
const FRONT_END_PARENT_FOLDER = path.join(import.meta.filename, '..', '..', '..', '..');
const ABSOLUTE_FILE_PATH = path.join(FRONT_END_PARENT_FOLDER, FILE);
const LICENSE_HEADER = `// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
`;
let formattedValues = new Set();
const statsCache = {
mtimeMs: Infinity,
size: Infinity,
};
function writeToFile() {
const finalContents = LICENSE_HEADER + 'export const knownContextValues = new Set([\n' +
[...formattedValues].sort().join('\n') + '\n]);\n';
fs.writeFileSync(ABSOLUTE_FILE_PATH, finalContents, 'utf-8');
const stats = fs.statSync(ABSOLUTE_FILE_PATH);
statsCache.mtimeMs = stats.mtimeMs;
statsCache.size = stats.size;
}
function updateLocalCacheIfNeeded() {
const stats = fs.statSync(ABSOLUTE_FILE_PATH);
let needsUpdate = false;
if (stats.mtimeMs !== statsCache.mtimeMs) {
statsCache.mtimeMs = stats.mtimeMs;
needsUpdate = true;
}
if (stats.size !== statsCache.size) {
statsCache.size = stats.size;
needsUpdate = true;
}
if (needsUpdate) {
formattedValues = new Set(
fs.readFileSync(ABSOLUTE_FILE_PATH, 'utf-8').split('\n').filter(l => l.startsWith(' \'')),
);
}
}
export default createRule({
name: 'jslog-context-list',
meta: {
type: 'problem',
docs: {
description: 'Puts jslog context values into KnownContextValues.ts file',
category: 'Possible Errors',
},
messages: {
unknownJslogContextValue: 'Found jslog context value \'{{ value }}\' that is not listed in ' + FILE,
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
let valuesAdded = false;
const checkValue = (value: unknown, node: Node) => {
if (typeof value !== 'string') {
return;
}
if (!value.length) {
return;
}
const formattedValue = ' ' + JSON.stringify(value).replaceAll('"', '\'') + ',';
if (formattedValues.has(formattedValue)) {
return;
}
formattedValues.add(formattedValue);
valuesAdded = true;
if (process.env.ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE) {
context.report({
node,
messageId: 'unknownJslogContextValue',
data: {value},
});
}
};
const checkPropertyValue = (propertyName: string, node: TSESTree.CallExpressionArgument) => {
if (!('properties' in node)) {
return;
}
for (const property of node.properties) {
if (property.type === 'RestElement' || property.type === 'SpreadElement' || !('value' in property.value)) {
continue;
}
if ('name' in property.key && property.key.name === propertyName) {
checkValue(property.value?.value, node);
} else if ('value' in property.key && property.key.value === propertyName) {
checkValue(property.value?.value, node);
}
}
};
updateLocalCacheIfNeeded();
return {
CallExpression(node) {
const firstArg = node.arguments[0];
if (!firstArg) {
return;
}
if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'VisualLogging') {
if (firstArg.type === 'Literal') {
checkValue(firstArg.value, node);
}
} else if (node.callee.type === 'MemberExpression' && node.callee.property.type === 'Identifier') {
const propertyName = node.callee.property.name;
if (propertyName === 'registerActionExtension') {
checkPropertyValue('actionId', firstArg);
} else if (propertyName === 'registerViewExtension') {
checkPropertyValue('id', firstArg);
} else if (propertyName === 'registerSettingExtension') {
checkPropertyValue('settingName', firstArg);
} else if (propertyName === 'createSetting') {
if (firstArg.type === 'Literal') {
checkValue(firstArg.value, node);
}
}
}
},
ObjectExpression(node) {
checkPropertyValue('jslogContext', node);
},
VariableDeclarator(node) {
if (node.id.type === 'Identifier' && node.id.name === 'generatedProperties' &&
node.init?.type === 'ArrayExpression') {
for (const element of node.init.elements) {
if (element) {
checkPropertyValue('name', element);
}
}
}
if (node.id.type === 'Identifier' && node.id.name === 'generatedAliasesFor' &&
node.init?.type === 'NewExpression') {
const firstArg = node.init?.arguments?.[0];
const elements = firstArg.type === 'ArrayExpression' ? firstArg.elements : [];
for (const outerElement of elements) {
const innerElements = outerElement?.type === 'ArrayExpression' ? outerElement.elements : [];
for (const innerElement of innerElements) {
if (innerElement && 'value' in innerElement) {
checkValue(innerElement.value, innerElement);
}
}
}
}
},
'Program:exit'() {
// Don't write to file if fail is enabled
if (process.env.ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE) {
return;
}
if (
// If a new value is added write the update to file
valuesAdded ||
// If we are lint the KnownContextValues.ts file
// unconditionally write to it
// that ensures that manually added values are sorted
context.filename === ABSOLUTE_FILE_PATH) {
writeToFile();
}
},
};
},
});