Bug 1779989 - Explicitly disallow ES modules in ESLint import-globals-from statements. r=arai

Differential Revision: https://phabricator.services.mozilla.com/D152002
This commit is contained in:
Mark Banner 2022-07-19 05:57:15 +00:00
Родитель f724fd0ca0
Коммит a869ce033b
2 изменённых файлов: 22 добавлений и 1 удалений

Просмотреть файл

@ -12,3 +12,7 @@ on each other's globals.
If <path> is a relative path, then it must be relative to the file being
checked by the rule.
Note: ``import-globals-from`` does not support loading globals from ES modules.
These should be imported as variable definitions directly, or the file where
they are imported should be referenced.

Просмотреть файл

@ -81,8 +81,9 @@ var lastHTMLGlobals = {};
* @param {String} filePath
* The absolute path of the file being parsed.
*/
function GlobalsForNode(filePath) {
function GlobalsForNode(filePath, context) {
this.path = filePath;
this.context = context;
if (this.path) {
this.dirname = path.dirname(this.path);
@ -128,6 +129,22 @@ GlobalsForNode.prototype = {
let filePath = match[1].trim();
if (filePath.endsWith(".mjs")) {
if (this.context) {
this.context.report(
comment,
"import-globals-from does not support module files - use a direct import instead"
);
} else {
// Fall back to throwing an error, as we do not have a context in all situations,
// e.g. when loading the environment.
throw new Error(
"import-globals-from does not support module files - use a direct import instead"
);
}
continue;
}
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(this.dirname, filePath);
} else {