Skip to content

Commit

Permalink
fix: support #import with whitespace between # and import (#629)
Browse files Browse the repository at this point in the history
This PR updates the parsing logic for `#import` lines to support
whitespace between the `#` and the `import`.

Related to #510.
  • Loading branch information
cgrindel authored Sep 30, 2023
1 parent e8eca16 commit ea6569f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
27 changes: 21 additions & 6 deletions swiftpkg/internal/objc_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")
load(":apple_builtin_frameworks.bzl", "apple_builtin_frameworks")

_pound_import = "#import "
_pound_import_len = len(_pound_import)
_at_import = "@import "
_at_import_len = len(_at_import)

def _parse_pound_import(line):
import_idx = line.find(_pound_import)
if import_idx < 0:
return None
start_idx = import_idx + _pound_import_len
line_len = len(line)

# Find the pound sign
pound_idx = line.find("#")
if pound_idx < 0:
return None
start_idx = pound_idx + 1

# Find import
begin_of_import_idx = -1
for idx in range(start_idx, line_len):
char = line[idx]
if char == " " or char == "\t":
continue
elif char == "i":
begin_of_import_idx = idx
break
else:
return None
if not line[begin_of_import_idx:].startswith("import"):
return None
start_idx = begin_of_import_idx + len("import")

# Find the opening bracket
open_bracket_idx = -1
for idx in range(start_idx, line_len):
Expand Down
7 changes: 7 additions & 0 deletions swiftpkg/tests/objc_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def _parse_for_imported_framework_test(ctx):
""",
exp = "CoreTelephony",
),
struct(
msg = "# import, the pound is not adjacent to import ",
line = """\
# import <SystemConfiguration/SystemConfiguration.h>
""",
exp = "SystemConfiguration",
),
struct(
msg = "#import dir/header with brackets, is not framework",
line = """\
Expand Down

0 comments on commit ea6569f

Please sign in to comment.