Skip to content

Commit 5fa5ec8

Browse files
committed
fix: java import
* inline unnecessary second call * add short-circuit
1 parent bffbd07 commit 5fa5ec8

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

.grit/patterns/java/_test_java_imports.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tag: [java]
88
```grit
99
language java
1010
file($body) where {
11-
$delete_import = "import a.b.Text;",
11+
$delete_import = `import a.b.Text;`,
1212
$body<: maybe contains remove_import_statement($delete_import)
1313
}
1414
```
@@ -90,7 +90,7 @@ public class AClass {
9090
```grit
9191
language java
9292
file($body) where {
93-
$new_import = "import a.b.Text;",
93+
$new_import = `import a.b.Text;`,
9494
$body<: maybe contains ensure_import_statement($new_import)
9595
}
9696
```
@@ -171,7 +171,7 @@ public class AClass {
171171

172172
```grit
173173
language java
174-
replace_import_statement("import a.b.Text;", "import a.c.Text;")
174+
replace_import_statement(`import a.b.Text;`, `import a.c.Text;`)
175175
```
176176

177177
## test `replace_import_statement` when have

.grit/patterns/java/java.grit

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ language java
44
private pattern before_each_file_stdlib() {
55
file($body) where {
66
$body <: init_vars(),
7-
$body <: init_import()
7+
$body <: maybe contains bubble import_declaration() as $this_import where {
8+
$GLOBAL_EXIST_IMPORTS += $this_import,
9+
}
810
}
911
}
1012

@@ -26,7 +28,6 @@ private pattern after_rewrite() {
2628
}
2729
}
2830

29-
3031
private pattern init_vars() {
3132
$_ where {
3233
$GLOBAL_EXIST_IMPORTS = [],
@@ -36,60 +37,47 @@ private pattern init_vars() {
3637
}
3738
}
3839

39-
private pattern init_import() {
40-
$_ where {
41-
$program <: accumulate_all_imports(),
42-
}
43-
}
44-
45-
private pattern accumulate_all_imports() {
46-
$_ where {
47-
$program <: maybe contains bubble() import_declaration() as $this_import where {
48-
$GLOBAL_EXIST_IMPORTS += text(`$this_import`),
49-
},
50-
}
51-
}
5240

5341
private pattern process_all_import() {
5442
$body where {
43+
// short-circuit if no changes
44+
or {
45+
$GLOBAL_NEW_FROM_IMPORTS <: not [],
46+
$GLOBAL_REMOVE_IMPORTS <: not [],
47+
$GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS <: true,
48+
},
5549
$last_import = "",
5650
$exists_imports = [],
51+
$body <: contains package_declaration() as $pkg_decl,
52+
// no existing imports, just add all new imports
53+
$all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
5754
if ($body <: not contains bubble() import_declaration()) {
58-
$body <: contains bubble() package_declaration() as $pkg_decl where {
59-
$all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
60-
if ($all_import <: not ``) {
61-
$pkg_decl => `$pkg_decl\n\n$all_import`
62-
}
55+
if ($all_import <: not ``) {
56+
$pkg_decl => `$pkg_decl\n\n$all_import`
6357
}
6458
}
6559
else {
66-
$body <: contains bubble($last_import, $exists_imports) import_declaration() as $bef where {
67-
$last_import = $bef,
68-
if ($exists_imports <: some $bef) {
60+
$body <: contains bubble($last_import, $exists_imports) import_declaration() as $this_import where {
61+
$last_import = $this_import,
62+
if ($exists_imports <: some $this_import) {
6963
// duplicate import, remove it
7064
if ($GLOBAL_REMOVE_DUPLICATE_EXIST_IMPORTS <: true ) {
71-
$bef => .
65+
$this_import => .
7266
}
7367
} else {
74-
if ($GLOBAL_REMOVE_IMPORTS <: some $bef) {
75-
$bef => .
68+
if ($GLOBAL_REMOVE_IMPORTS <: some $this_import) {
69+
$this_import => .
7670
},
77-
$exists_imports += text($bef)
71+
$exists_imports += text($this_import)
7872
}
7973
},
80-
$all_import = join(distinct($GLOBAL_NEW_FROM_IMPORTS), `\n`),
8174
// last_import need to be deleted if in remove imports
8275
if ($GLOBAL_REMOVE_IMPORTS <: some $last_import) {
8376
$last_import => `$all_import`
8477
} else if ($last_import <: not ""){
8578
if ($all_import <: not ``) {
8679
$last_import => `$last_import\n$all_import`
8780
}
88-
} else {
89-
$body <: contains bubble($last_import) package_declaration() as $pkg_decl,
90-
if ($all_import <: not ``) {
91-
$pkg_decl => `$pkg_decl\n$all_import`
92-
}
9381
}
9482
}
9583
}

0 commit comments

Comments
 (0)