Skip to content

Commit da63c56

Browse files
committed
fix: use strip_prefix for clippy compliance in import detection
1 parent f8f6895 commit da63c56

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/code-validator/guest/runtime/src/js_parser.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -510,26 +510,18 @@ pub fn extract_all_imports(source: &str) -> Vec<String> {
510510
}
511511
// Also check for `from "..."` pattern on continuation lines.
512512
// Only match when `from` is at the START of the trimmed line or
513-
// immediately after `}` at the start. Using `starts_with` prevents
513+
// immediately after `}` at the start. Using `strip_prefix` prevents
514514
// false positives from `from` appearing mid-line inside strings.
515-
if trimmed.starts_with("from ")
516-
|| trimmed.starts_with("} from ")
517-
|| trimmed.starts_with("}from ")
515+
let from_rest = trimmed
516+
.strip_prefix("from ")
517+
.or_else(|| trimmed.strip_prefix("} from "))
518+
.or_else(|| trimmed.strip_prefix("}from "));
519+
520+
if let Some(rest) = from_rest
521+
&& let Ok((_, specifier)) = string_literal(rest.trim())
522+
&& !imports.iter().any(|s: &String| s == specifier)
518523
{
519-
let rest = if trimmed.starts_with("from ") {
520-
&trimmed[5..]
521-
} else if trimmed.starts_with("} from ") {
522-
&trimmed[7..]
523-
} else {
524-
// "}from "
525-
&trimmed[6..]
526-
};
527-
528-
if let Ok((_, specifier)) = string_literal(rest.trim())
529-
&& !imports.iter().any(|s: &String| s == specifier)
530-
{
531-
imports.push(String::from(specifier));
532-
}
524+
imports.push(String::from(specifier));
533525
}
534526
}
535527

0 commit comments

Comments
 (0)