-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Migrate build/commands to ESM #34222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+397
−359
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d2c3e3
Migrate build/commands to esm.
goodov 0620269
Fix fs-extra usages.
goodov d35759c
Replace require() calls.
goodov 258818a
Keep rootDir as commonjs.
goodov 89f5fbf
Add presubmit to check for exact imports.
goodov e42e197
Replace imports check presubmit with a working eslint rule.
goodov 0ff3562
Use spawnSync to run tests.
goodov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright (c) 2026 The Brave Authors. All rights reserved. | ||
| # 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/. | ||
|
|
||
| import os | ||
| import re | ||
|
|
||
| PRESUBMIT_VERSION = '2.0.0' | ||
|
|
||
|
|
||
| def CheckImportSpecifierMatchesFile(input_api, output_api): | ||
| """Checks that relative import specifier matches the actual file on disk. | ||
|
|
||
| When running TypeScript natively via Node.js (no transpilation), imports | ||
| must use the real file extension. For example, if a file is named config.ts, | ||
| importing it as './config.js' is an error. | ||
| """ | ||
|
|
||
| IMPORT_RE = re.compile( | ||
| r''' | ||
| (?: | ||
| \bfrom \s+ # static: import/export ... from | ||
| | \bimport \s* \( # dynamic: import( | ||
| | \bimport \s+ # side-effect: import './foo.js' | ||
| ) | ||
| \s* ['"] | ||
| ( \.\.?/ # ./ or ../ | ||
| [^'"]* # path | ||
| \. [cm]?[jt]s # file extension | ||
| ) | ||
| ['"] | ||
| ''', re.VERBOSE) | ||
|
|
||
| files_to_check = (r'.+\.[cm]?[jt]s$', ) | ||
| file_filter = lambda f: input_api.FilterSourceFile( | ||
| f, files_to_check=files_to_check) | ||
|
|
||
| items = [] | ||
| for f in input_api.AffectedSourceFiles(file_filter): | ||
| file_dir = os.path.dirname(f.AbsoluteLocalPath()) | ||
| for lineno, line in enumerate(f.NewContents(), 1): | ||
| for match in IMPORT_RE.finditer(line): | ||
| specifier = match.group(1) | ||
| resolved = os.path.normpath(os.path.join(file_dir, specifier)) | ||
|
|
||
| if not os.path.isfile(resolved): | ||
| items.append(f'{f.LocalPath()}:{lineno}: ' | ||
| f'file not found: {specifier}') | ||
|
|
||
| if not items: | ||
| return [] | ||
|
|
||
| return [ | ||
| output_api.PresubmitError( | ||
| 'Import path references a file that doesn\'t exist. ' | ||
| 'Check the file extension.', items) | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| # Copyright (c) 2026 The Brave Authors. All rights reserved. | ||
| # 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/. | ||
|
|
||
| import os | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| import brave_chromium_utils | ||
|
|
||
| # pylint: disable=import-error,no-member | ||
| import PRESUBMIT | ||
|
|
||
| with brave_chromium_utils.sys_path("//"): | ||
| from PRESUBMIT_test_mocks import MockAffectedFile | ||
| from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi | ||
|
|
||
|
|
||
| class ImportSpecifierTest(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self._tmpdir = tempfile.mkdtemp() | ||
|
|
||
| def _run_check(self, files): | ||
| """Run the check with a files map. | ||
|
|
||
| Args: | ||
| files: dict mapping path -> lines list (affected file) or None | ||
| (exists on disk only, not in the changelist). | ||
| """ | ||
| input_api = MockInputApi() | ||
| input_api.files = [] | ||
| for path, contents in files.items(): | ||
| full = os.path.join(self._tmpdir, path) | ||
| os.makedirs(os.path.dirname(full), exist_ok=True) | ||
| if contents is not None: | ||
| affected_file = MockAffectedFile(path, contents) | ||
| affected_file.AbsoluteLocalPath = lambda p=full: p | ||
| input_api.files.append(affected_file) | ||
| else: | ||
| open(full, 'w').close() | ||
| return PRESUBMIT.CheckImportSpecifierMatchesFile( | ||
| input_api, MockOutputApi()) | ||
|
|
||
| def testImportSpecifiers(self): | ||
| # files: path -> lines (affected source) or None (on-disk only). | ||
| cases = [ | ||
| { | ||
| 'name': 'correct .js extension', | ||
| 'files': { | ||
| 'lib/build.js': ["import config from './config.js'"], | ||
| 'lib/config.js': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'wrong extension: .js import but .ts file', | ||
| 'files': { | ||
| 'lib/build.js': ["import config from './config.js'"], | ||
| 'lib/config.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'multiline import', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "import {", | ||
| " getTestBinary,", | ||
| " getTestsToRun,", | ||
| "} from './utils.js'", | ||
| ], | ||
| 'lib/utils.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'dynamic import()', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "const config = await import('./config.js')", | ||
| ], | ||
| 'lib/config.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'side-effect import, wrong extension', | ||
| 'files': { | ||
| 'lib/build.js': ["import './setup.js'"], | ||
| 'lib/setup.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'side-effect import, correct extension', | ||
| 'files': { | ||
| 'lib/build.js': ["import './setup.js'"], | ||
| 'lib/setup.js': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'export { } from', | ||
| 'files': { | ||
| 'lib/index.js': ["export { foo } from './utils.js'"], | ||
| 'lib/utils.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'export * from, correct extension', | ||
| 'files': { | ||
| 'lib/index.js': ["export * from './utils.js'"], | ||
| 'lib/utils.js': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'correct .ts extension', | ||
| 'files': { | ||
| 'lib/build.ts': ["import config from './config.ts'"], | ||
| 'lib/config.ts': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'parent directory import', | ||
| 'files': { | ||
| 'lib/build.js': ["import config from '../config.js'"], | ||
| 'config.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'non-relative imports are ignored', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "import fs from 'fs-extra'", | ||
| "import path from 'path'", | ||
| ], | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'multiple errors in one file', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "import config from './config.js'", | ||
| "import util from './util.js'", | ||
| ], | ||
| 'lib/config.ts': None, | ||
| 'lib/util.ts': None, | ||
| }, | ||
| 'expected_errors': 2, | ||
| }, | ||
| { | ||
| 'name': 'wrong .mjs extension', | ||
| 'files': { | ||
| 'lib/build.mjs': ["import config from './config.mjs'"], | ||
| 'lib/config.mts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'require() is not matched', | ||
| 'files': { | ||
| 'lib/build.cjs': [ | ||
| "const config = require('./config.cjs')", | ||
| ], | ||
| 'lib/config.cts': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| { | ||
| 'name': 'dotted filename, wrong extension', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "import config from './config.base.js'", | ||
| ], | ||
| 'lib/config.base.ts': None, | ||
| }, | ||
| 'expected_errors': 1, | ||
| }, | ||
| { | ||
| 'name': 'dotted filename, correct extension', | ||
| 'files': { | ||
| 'lib/build.js': [ | ||
| "import config from './config.base.js'", | ||
| ], | ||
| 'lib/config.base.js': None, | ||
| }, | ||
| 'expected_errors': 0, | ||
| }, | ||
| ] | ||
|
|
||
| for case in cases: | ||
| with self.subTest(name=case['name']): | ||
| self._tmpdir = tempfile.mkdtemp() | ||
| results = self._run_check(case['files']) | ||
|
|
||
| items = [i for r in results for i in r.items] | ||
| self.assertEqual( | ||
| case['expected_errors'], len(items), | ||
| f"Expected {case['expected_errors']} errors, " | ||
| f"got {len(items)}: {items}") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.