Skip to content

Commit 4a1bd8a

Browse files
committed
Add presubmit to check for exact imports.
1 parent 48430ba commit 4a1bd8a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

build/commands/PRESUBMIT.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2026 The Brave Authors. All rights reserved.
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
# You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
import os
7+
import re
8+
9+
PRESUBMIT_VERSION = '2.0.0'
10+
11+
12+
def CheckImportSpecifierMatchesFile(input_api, output_api):
13+
"""Checks that relative import specifier matches the actual file on disk.
14+
15+
When running TypeScript natively via Node.js (no transpilation), imports
16+
must use the real file extension. For example, if a file is named config.ts,
17+
importing it as './config.js' is an error.
18+
"""
19+
20+
IMPORT_RE = re.compile(
21+
r'''
22+
(?:
23+
\bfrom \s+ # static: import/export ... from
24+
| import \s* \( # dynamic: import(
25+
)
26+
\s* ['"]
27+
( \. [^'"]+ \. (?:[cm]?[jt]s) ) # relative specifier with extension
28+
['"]
29+
''', re.VERBOSE)
30+
31+
files_to_check = (r'.+\.[cm]?[jt]s$', )
32+
file_filter = lambda f: input_api.FilterSourceFile(
33+
f, files_to_check=files_to_check)
34+
35+
items = []
36+
for f in input_api.AffectedSourceFiles(file_filter):
37+
file_dir = os.path.dirname(f.AbsoluteLocalPath())
38+
for lineno, line in enumerate(f.NewContents(), 1):
39+
for match in IMPORT_RE.finditer(line):
40+
specifier = match.group(1)
41+
resolved = os.path.normpath(os.path.join(file_dir, specifier))
42+
43+
if not os.path.isfile(resolved):
44+
items.append(f'{f.LocalPath()}:{lineno}: '
45+
f'file not found: {specifier}')
46+
47+
if not items:
48+
return []
49+
50+
return [
51+
output_api.PresubmitError(
52+
'Import specifier must match the actual file on disk.', items)
53+
]

0 commit comments

Comments
 (0)