Bug 1907846 - Change how our eslint-plugin-* modules determine the root source directory. r=frontend-codestyle-reviewers,mossop

This switches to checking for the package.json whose name is mozilla-central, which should be more
reliable than checking for .eslintignore, and ensures that we're compatible with flat config which
will remove .eslintignore.

Differential Revision: https://phabricator.services.mozilla.com/D216538
This commit is contained in:
Mark Banner 2024-07-16 12:53:08 +00:00
Родитель 4d2370c264
Коммит 7ea3270091
2 изменённых файлов: 31 добавлений и 26 удалений

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

@ -634,17 +634,23 @@ module.exports = {
/**
* Gets the root directory of the repository by walking up directories from
* this file until a .eslintignore file is found. If this fails, the same
* procedure will be attempted from the current working dir.
* this file until the top-level mozilla-central package.json file is found.
* If this fails, the same procedure will be attempted from the current
* working dir.
*
* @return {String} The absolute path of the repository directory
*/
get rootDir() {
if (!gRootDir) {
function searchUpForIgnore(dirName, filename) {
function searchUpForPackage(dirName) {
let parsed = path.parse(dirName);
while (parsed.root !== dirName) {
if (fs.existsSync(path.join(dirName, filename))) {
return dirName;
let possibleFile = path.join(dirName, "package.json");
if (fs.existsSync(possibleFile)) {
let packageData = require(possibleFile);
if (packageData.name == "mozilla-central") {
return dirName;
}
}
// Move up a level
dirName = parsed.dir;
@ -653,15 +659,9 @@ module.exports = {
return null;
}
let possibleRoot = searchUpForIgnore(
path.dirname(module.filename),
".eslintignore"
);
let possibleRoot = searchUpForPackage(path.dirname(module.filename));
if (!possibleRoot) {
possibleRoot = searchUpForIgnore(path.resolve(), ".eslintignore");
}
if (!possibleRoot) {
possibleRoot = searchUpForIgnore(path.resolve(), "package.json");
possibleRoot = searchUpForPackage(path.resolve());
}
if (!possibleRoot) {
// We've couldn't find a root from the module or CWD, so lets just go

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

@ -13,14 +13,26 @@ const fs = require("fs");
let gRootDir = null;
// Copied from `tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js`.
/**
* Gets the root directory of the repository by walking up directories from
* this file until the top-level mozilla-central package.json file is found.
* If this fails, the same procedure will be attempted from the current
* working dir.
* Copied from `tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js`.
*
* @return {String} The absolute path of the repository directory
*/
function getRootDir() {
if (!gRootDir) {
function searchUpForIgnore(dirName, filename) {
function searchUpForPackage(dirName) {
let parsed = path.parse(dirName);
while (parsed.root !== dirName) {
if (fs.existsSync(path.join(dirName, filename))) {
return dirName;
let possibleFile = path.join(dirName, "package.json");
if (fs.existsSync(possibleFile)) {
let packageData = require(possibleFile);
if (packageData.name == "mozilla-central") {
return dirName;
}
}
// Move up a level
dirName = parsed.dir;
@ -28,16 +40,9 @@ function getRootDir() {
}
return null;
}
let possibleRoot = searchUpForIgnore(
path.dirname(module.filename),
".eslintignore"
);
let possibleRoot = searchUpForPackage(path.dirname(module.filename));
if (!possibleRoot) {
possibleRoot = searchUpForIgnore(path.resolve(), ".eslintignore");
}
if (!possibleRoot) {
possibleRoot = searchUpForIgnore(path.resolve(), "package.json");
possibleRoot = searchUpForPackage(path.resolve());
}
if (!possibleRoot) {
// We've couldn't find a root from the module or CWD, so lets just go