From b3808a443b8dbe7fbb16b4789afd19512419dbe6 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Fri, 30 Aug 2024 10:41:00 -0700 Subject: [PATCH] chore: expose insert_after_imports predicate (#236) --- .grit/patterns/js/imports.grit | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/.grit/patterns/js/imports.grit b/.grit/patterns/js/imports.grit index 58f9223a..a9842fed 100644 --- a/.grit/patterns/js/imports.grit +++ b/.grit/patterns/js/imports.grit @@ -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 }