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
|
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
"use strict";
const globals = require("../globals");
const fs = require("fs");
module.exports = {
meta: {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/no-more-globals.html",
},
messages: {
newGlobal:
"The global {{ name }} was not previously in this file, where new global variables are not permitted.",
removedGlobal:
"The global {{ name }} was expected to be defined in this file but isn't. Please remove it from the .globals file.",
missingGlobalsFile:
"This file has mozilla/no-more-globals enabled but has no .globals sibling file. Please create one.",
},
schema: [],
type: "problem",
},
create(context) {
return {
Program(node) {
let filename = context.filename;
let code = context.sourceCode.getText();
let currentGlobals = globals.getGlobalsForCode(code, {}, false);
let knownGlobals;
try {
knownGlobals = new Set(
JSON.parse(fs.readFileSync(filename + ".globals"))
);
} catch (ex) {
context.report({
node,
messageId: "missingGlobalsFile",
});
return;
}
for (let { name } of currentGlobals) {
if (!knownGlobals.has(name)) {
context.report({
node,
messageId: "newGlobal",
data: { name },
});
}
}
for (let known of knownGlobals) {
if (!currentGlobals.some(n => n.name == known)) {
context.report({
node,
messageId: "removedGlobal",
data: { name: known },
});
}
}
},
};
},
};
|