Skip to content

Commit ea6569f

Browse files
authored
fix: support #import with whitespace between # and import (#629)
This PR updates the parsing logic for `#import` lines to support whitespace between the `#` and the `import`. Related to #510.
1 parent e8eca16 commit ea6569f

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

swiftpkg/internal/objc_files.bzl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,33 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
55
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")
66
load(":apple_builtin_frameworks.bzl", "apple_builtin_frameworks")
77

8-
_pound_import = "#import "
9-
_pound_import_len = len(_pound_import)
108
_at_import = "@import "
119
_at_import_len = len(_at_import)
1210

1311
def _parse_pound_import(line):
14-
import_idx = line.find(_pound_import)
15-
if import_idx < 0:
16-
return None
17-
start_idx = import_idx + _pound_import_len
1812
line_len = len(line)
1913

14+
# Find the pound sign
15+
pound_idx = line.find("#")
16+
if pound_idx < 0:
17+
return None
18+
start_idx = pound_idx + 1
19+
20+
# Find import
21+
begin_of_import_idx = -1
22+
for idx in range(start_idx, line_len):
23+
char = line[idx]
24+
if char == " " or char == "\t":
25+
continue
26+
elif char == "i":
27+
begin_of_import_idx = idx
28+
break
29+
else:
30+
return None
31+
if not line[begin_of_import_idx:].startswith("import"):
32+
return None
33+
start_idx = begin_of_import_idx + len("import")
34+
2035
# Find the opening bracket
2136
open_bracket_idx = -1
2237
for idx in range(start_idx, line_len):

swiftpkg/tests/objc_files_tests.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def _parse_for_imported_framework_test(ctx):
3232
""",
3333
exp = "CoreTelephony",
3434
),
35+
struct(
36+
msg = "# import, the pound is not adjacent to import ",
37+
line = """\
38+
# import <SystemConfiguration/SystemConfiguration.h>
39+
""",
40+
exp = "SystemConfiguration",
41+
),
3542
struct(
3643
msg = "#import dir/header with brackets, is not framework",
3744
line = """\

0 commit comments

Comments
 (0)