From 1f501407fe7cac915d4bfb360df5a951cae10000 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Mon, 3 Mar 2025 12:47:47 +0000 Subject: [PATCH] Bug 1945566 - Add eslint and vscode module resolvers for moz-src, r=firefox-desktop-core-reviewers ,frontend-codestyle-reviewers,mconley,Standard8 Differential Revision: https://phabricator.services.mozilla.com/D236570 --- .eslintrc.js | 6 ++++ moz.build | 2 +- .../mozbuild/mozbuild/test/test_jarmaker.py | 3 +- srcdir-resolver.js | 29 +++++++++++++++++++ tools/@types/tsconfig.json | 6 +++- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 srcdir-resolver.js diff --git a/.eslintrc.js b/.eslintrc.js index bedcdc7fca56..12079efc68c8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -50,6 +50,10 @@ const httpTestingPaths = [ module.exports = { settings: { "import/extensions": [".mjs"], + "import/resolver": { + [path.resolve(__dirname, "srcdir-resolver.js")]: {}, + node: {}, + }, }, ignorePatterns, // Ignore eslint configurations in parent directories. @@ -108,6 +112,8 @@ module.exports = { // *.config.js files are generally assumed to be configuration files // based for node. "*.config.?(m)js", + // The resolver for moz-src for eslint, vscode etc. + "srcdir-resolver.js", ], env: { node: true, diff --git a/moz.build b/moz.build index 559182bb0a67..51949748e662 100644 --- a/moz.build +++ b/moz.build @@ -35,7 +35,7 @@ with Files("docs/**"): with Files("mach*"): BUG_COMPONENT = ("Firefox Build System", "Mach Core") -with Files("pyproject.toml"): +with Files("pyproject.toml", "srcdir-resolver.js"): BUG_COMPONENT = ("Developer Infrastructure", "Lint and Formatting") with Files("*moz*"): diff --git a/python/mozbuild/mozbuild/test/test_jarmaker.py b/python/mozbuild/mozbuild/test/test_jarmaker.py index 24a8c7694a20..57ce1bd2ec5a 100644 --- a/python/mozbuild/mozbuild/test/test_jarmaker.py +++ b/python/mozbuild/mozbuild/test/test_jarmaker.py @@ -204,10 +204,11 @@ class TestJarMaker(unittest.TestCase): for attr in ("topsourcedir", "sourcedirs"): if attr in kwargs: setattr(jm, attr, kwargs[attr]) - jm.makeJar(infile, self.builddir) cwd = os.getcwd() os.chdir(self.builddir) try: + jm.makeJar(infile, self.builddir) + # expand build to stage for path, dirs, files in os.walk("."): stagedir = os.path.join(self.stagedir, path) diff --git a/srcdir-resolver.js b/srcdir-resolver.js new file mode 100644 index 000000000000..31301900105e --- /dev/null +++ b/srcdir-resolver.js @@ -0,0 +1,29 @@ +/* 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 https://mozilla.org/MPL/2.0/. */ + +const path = require("path"); +const fs = require("fs"); + +const PREFIX = "moz-src:///"; + +exports.interfaceVersion = 2; +exports.resolve = source => { + if (!source.startsWith(PREFIX)) { + return { found: false }; + } + + let result = path.resolve( + __dirname, + ...source.substring(PREFIX.length).split("/") + ); + let stats = fs.statSync(result, { throwIfNoEntry: false }); + if (!stats || !stats.isFile()) { + return { found: false }; + } + + return { + found: true, + path: result, + }; +}; diff --git a/tools/@types/tsconfig.json b/tools/@types/tsconfig.json index 9e9a8930d68d..8d92ac0437a8 100644 --- a/tools/@types/tsconfig.json +++ b/tools/@types/tsconfig.json @@ -2,6 +2,10 @@ "compilerOptions": { "noEmit": true, "target": "es2024", - "types": ["gecko"] + "types": ["gecko"], + "baseUrl": "../../", + "paths": { + "moz-src:///*": ["./*"] + } } }