Skip to content

Commit

Permalink
chore: expose insert_after_imports predicate (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Aug 30, 2024
1 parent 90f73e0 commit b3808a4
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions .grit/patterns/js/imports.grit
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,28 @@ private pattern process_one_source($p, $all_imports) {
}
}

predicate insert_after_imports($p, $insert) {
or {
// Try to find a shebang and insert after that
$p <: program(hash_bang=$h) where {
$h <: hash_bang_line() += `\n$insert`
},
// Find an import statement to anchor on
$p <: program($statements) where {
$statements <: some $anchor where { $anchor <: import_statement() },
$anchor += `\n$insert`
},
// Fall back to inserting at the start of the whole program
$p => `$insert\n$p`
}
}

private pattern insert_imports() {
$p where {
$all_imports = [],
$GLOBAL_IMPORTED_SOURCES <: some process_one_source($p, $all_imports),
if ($all_imports <: not []) {
or {
// Try to find a shebang and insert after that
$p <: program(hash_bang=$h) where {
$h <: hash_bang_line() += `\n$all_imports`
},
// Find an import statement to anchor on
$p <: program($statements) where {
$statements <: some $anchor where { $anchor <: import_statement() },
$anchor += `\n$all_imports`
},
// Fall back to inserting the whole program
$p => `$all_imports\n$p`
}
insert_after_imports($p, insert=$all_imports)
} else {
true
}
Expand Down

0 comments on commit b3808a4

Please sign in to comment.