Skip to content

Commit 5d52e84

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
[eslint] Fix issue with JsLog rule and VSCode
Remove the update if the Set didn't change during the rule run. This make the rule take so little time it's no longer in the top 10 for time consumed. Also conditionally reload the rule while if needed. This can happen if the user checkouts a separate branch or commit, this meant that the Set was out of sync and value could be deleted unintentionally. Bug: none Change-Id: I29ff66e4e9f979444e5a22f1f9c001af9769dfc5 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6439513 Reviewed-by: Danil Somsikov <dsv@chromium.org> Auto-Submit: Nikolay Vitkov <nvitkov@chromium.org> Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org>
1 parent 7f52031 commit 5d52e84

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

scripts/eslint_rules/lib/jslog-context-list.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,42 @@ const LICENSE_HEADER = `// Copyright 2024 The Chromium Authors. All rights reser
1818
// found in the LICENSE file.
1919
2020
`;
21+
let formattedValues = new Set();
2122

22-
const formattedValues = new Set(
23-
fs.readFileSync(ABSOLUTE_FILE_PATH, 'utf-8').split('\n').filter(l => l.startsWith(' \'')),
24-
);
23+
const statsCache = {
24+
mtimeMs: Infinity,
25+
size: Infinity,
26+
};
27+
28+
function writeToFile() {
29+
const finalContents = LICENSE_HEADER + 'export const knownContextValues = new Set([\n' +
30+
[...formattedValues].sort().join('\n') + '\n]);\n';
31+
fs.writeFileSync(ABSOLUTE_FILE_PATH, finalContents, 'utf-8');
32+
const stats = fs.statSync(ABSOLUTE_FILE_PATH);
33+
statsCache.mtimeMs = stats.mtimeMs;
34+
statsCache.size = stats.size;
35+
}
36+
37+
function updateLocalCacheIfNeeded() {
38+
const stats = fs.statSync(ABSOLUTE_FILE_PATH);
39+
let needsUpdate = false;
40+
41+
if (stats.mtimeMs !== statsCache.mtimeMs) {
42+
statsCache.mtimeMs = stats.mtimeMs;
43+
needsUpdate = true;
44+
}
45+
46+
if (stats.size !== statsCache.size) {
47+
statsCache.size = stats.size;
48+
needsUpdate = true;
49+
}
50+
51+
if (needsUpdate) {
52+
formattedValues = new Set(
53+
fs.readFileSync(ABSOLUTE_FILE_PATH, 'utf-8').split('\n').filter(l => l.startsWith(' \'')),
54+
);
55+
}
56+
}
2557

2658
export default createRule({
2759
name: 'jslog-context-list',
@@ -40,6 +72,7 @@ export default createRule({
4072
},
4173
defaultOptions: [],
4274
create: function(context) {
75+
let valuesAdded = false;
4376
const checkValue = (value, node) => {
4477
if (typeof value !== 'string') {
4578
return;
@@ -52,6 +85,7 @@ export default createRule({
5285
return;
5386
}
5487
formattedValues.add(formattedValue);
88+
valuesAdded = true;
5589
if (process.env.ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE) {
5690
context.report({
5791
node,
@@ -68,6 +102,8 @@ export default createRule({
68102
}
69103
}
70104
};
105+
106+
updateLocalCacheIfNeeded();
71107
return {
72108
CallExpression(node) {
73109
const firstArg = node.arguments[0];
@@ -121,12 +157,10 @@ export default createRule({
121157
}
122158
},
123159
'Program:exit'() {
124-
if (process.env.ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE) {
160+
if (process.env.ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE || !valuesAdded) {
125161
return;
126162
}
127-
const finalContents = LICENSE_HEADER + 'export const knownContextValues = new Set([\n' +
128-
[...formattedValues].sort().join('\n') + '\n]);\n';
129-
fs.writeFileSync(ABSOLUTE_FILE_PATH, finalContents, 'utf-8');
163+
writeToFile();
130164
},
131165
};
132166
},

0 commit comments

Comments
 (0)