ast-grep-cli 0.26.2__tar.gz → 0.26.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/Cargo.lock +17 -17
  2. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/Cargo.toml +6 -6
  3. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/PKG-INFO +1 -1
  4. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/Cargo.toml +1 -1
  5. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/transform/mod.rs +26 -0
  6. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/transform/transformation.rs +37 -18
  7. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/Cargo.toml +1 -1
  8. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/match_tree/strictness.rs +4 -1
  9. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/meta_var.rs +14 -2
  10. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/replacer/indent.rs +21 -0
  11. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/replacer.rs +2 -0
  12. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/dynamic/Cargo.toml +1 -1
  13. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/Cargo.toml +1 -1
  14. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/lsp/Cargo.toml +1 -1
  15. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/pyproject.toml +1 -1
  16. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/LICENSE +0 -0
  17. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/README.md +0 -0
  18. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/Cargo.toml +0 -0
  19. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/bin/ast-grep.rs +0 -0
  20. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/completions.rs +0 -0
  21. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/config.rs +0 -0
  22. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/debug.rs +0 -0
  23. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/error.rs +0 -0
  24. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lang/custom_lang.rs +0 -0
  25. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lang/injection.rs +0 -0
  26. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lang/lang_globs.rs +0 -0
  27. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lang.rs +0 -0
  28. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lib.rs +0 -0
  29. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/lsp.rs +0 -0
  30. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/main.rs +0 -0
  31. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/new.rs +0 -0
  32. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/cloud_print.rs +0 -0
  33. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/colored_print/test.rs +0 -0
  34. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/colored_print.rs +0 -0
  35. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/interactive_print.rs +0 -0
  36. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/json_print.rs +0 -0
  37. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/print/mod.rs +0 -0
  38. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/run.rs +0 -0
  39. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/scan.rs +0 -0
  40. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/utils.rs +0 -0
  41. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/case_result.rs +0 -0
  42. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/find_file.rs +0 -0
  43. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/mod.rs +0 -0
  44. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/reporter.rs +0 -0
  45. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/snapshot.rs +0 -0
  46. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/src/verify/test_case.rs +0 -0
  47. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/tests/common/mod.rs +0 -0
  48. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/tests/run_test.rs +0 -0
  49. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/tests/scan_test.rs +0 -0
  50. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/cli/tests/verify_test.rs +0 -0
  51. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/check_var.rs +0 -0
  52. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/combined.rs +0 -0
  53. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/fixer.rs +0 -0
  54. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/lib.rs +0 -0
  55. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/maybe.rs +0 -0
  56. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/deserialize_env.rs +0 -0
  57. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/mod.rs +0 -0
  58. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/nth_child.rs +0 -0
  59. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/referent_rule.rs +0 -0
  60. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/relational_rule.rs +0 -0
  61. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule/stop_by.rs +0 -0
  62. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule_collection.rs +0 -0
  63. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule_config.rs +0 -0
  64. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/rule_core.rs +0 -0
  65. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/transform/rewrite.rs +0 -0
  66. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/config/src/transform/string_case.rs +0 -0
  67. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/language.rs +0 -0
  68. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/lib.rs +0 -0
  69. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/match_tree/match_node.rs +0 -0
  70. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/match_tree/mod.rs +0 -0
  71. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/matcher/kind.rs +0 -0
  72. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/matcher/node_match.rs +0 -0
  73. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/matcher/pattern.rs +0 -0
  74. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/matcher/text.rs +0 -0
  75. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/matcher.rs +0 -0
  76. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/node.rs +0 -0
  77. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/ops.rs +0 -0
  78. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/pinned.rs +0 -0
  79. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/replacer/structural.rs +0 -0
  80. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/replacer/template.rs +0 -0
  81. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/source.rs +0 -0
  82. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/core/src/traversal.rs +0 -0
  83. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/dynamic/src/lib.rs +0 -0
  84. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/bash.rs +0 -0
  85. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/cpp.rs +0 -0
  86. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/csharp.rs +0 -0
  87. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/css.rs +0 -0
  88. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/elixir.rs +0 -0
  89. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/go.rs +0 -0
  90. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/haskell.rs +0 -0
  91. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/html.rs +0 -0
  92. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/json.rs +0 -0
  93. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/kotlin.rs +0 -0
  94. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/lib.rs +0 -0
  95. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/lua.rs +0 -0
  96. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/parsers.rs +0 -0
  97. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/php.rs +0 -0
  98. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/python.rs +0 -0
  99. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/ruby.rs +0 -0
  100. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/rust.rs +0 -0
  101. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/scala.rs +0 -0
  102. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/language/src/swift.rs +0 -0
  103. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/lsp/src/lib.rs +0 -0
  104. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/lsp/src/utils.rs +0 -0
  105. {ast_grep_cli-0.26.2 → ast_grep_cli-0.26.3}/crates/lsp/tests/basic.rs +0 -0
@@ -114,7 +114,7 @@ dependencies = [
114
114
 
115
115
  [[package]]
116
116
  name = "ast-grep"
117
- version = "0.26.2"
117
+ version = "0.26.3"
118
118
  dependencies = [
119
119
  "ansi_term",
120
120
  "anyhow",
@@ -145,7 +145,7 @@ dependencies = [
145
145
 
146
146
  [[package]]
147
147
  name = "ast-grep-config"
148
- version = "0.26.2"
148
+ version = "0.26.3"
149
149
  dependencies = [
150
150
  "anyhow",
151
151
  "ast-grep-core",
@@ -161,7 +161,7 @@ dependencies = [
161
161
 
162
162
  [[package]]
163
163
  name = "ast-grep-core"
164
- version = "0.26.2"
164
+ version = "0.26.3"
165
165
  dependencies = [
166
166
  "bit-set",
167
167
  "regex",
@@ -172,7 +172,7 @@ dependencies = [
172
172
 
173
173
  [[package]]
174
174
  name = "ast-grep-dynamic"
175
- version = "0.26.2"
175
+ version = "0.26.3"
176
176
  dependencies = [
177
177
  "ast-grep-core",
178
178
  "ignore",
@@ -184,7 +184,7 @@ dependencies = [
184
184
 
185
185
  [[package]]
186
186
  name = "ast-grep-language"
187
- version = "0.26.2"
187
+ version = "0.26.3"
188
188
  dependencies = [
189
189
  "ast-grep-core",
190
190
  "ignore",
@@ -216,7 +216,7 @@ dependencies = [
216
216
 
217
217
  [[package]]
218
218
  name = "ast-grep-lsp"
219
- version = "0.26.2"
219
+ version = "0.26.3"
220
220
  dependencies = [
221
221
  "ast-grep-config",
222
222
  "ast-grep-core",
@@ -230,7 +230,7 @@ dependencies = [
230
230
 
231
231
  [[package]]
232
232
  name = "ast-grep-napi"
233
- version = "0.26.2"
233
+ version = "0.26.3"
234
234
  dependencies = [
235
235
  "ast-grep-config",
236
236
  "ast-grep-core",
@@ -249,7 +249,7 @@ dependencies = [
249
249
 
250
250
  [[package]]
251
251
  name = "ast-grep-py"
252
- version = "0.26.2"
252
+ version = "0.26.3"
253
253
  dependencies = [
254
254
  "anyhow",
255
255
  "ast-grep-config",
@@ -317,7 +317,7 @@ dependencies = [
317
317
 
318
318
  [[package]]
319
319
  name = "benches"
320
- version = "0.26.2"
320
+ version = "0.26.3"
321
321
  dependencies = [
322
322
  "ast-grep-config",
323
323
  "ast-grep-core",
@@ -450,9 +450,9 @@ dependencies = [
450
450
 
451
451
  [[package]]
452
452
  name = "clap_complete"
453
- version = "4.5.18"
453
+ version = "4.5.23"
454
454
  source = "registry+https://github.com/rust-lang/crates.io-index"
455
- checksum = "1ee158892bd7ce77aa15c208abbdb73e155d191c287a659b57abd5adb92feb03"
455
+ checksum = "531d7959c5bbb6e266cecdd0f20213639c3a5c3e4d615f97db87661745f781ff"
456
456
  dependencies = [
457
457
  "clap",
458
458
  ]
@@ -1514,18 +1514,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
1514
1514
 
1515
1515
  [[package]]
1516
1516
  name = "serde"
1517
- version = "1.0.208"
1517
+ version = "1.0.209"
1518
1518
  source = "registry+https://github.com/rust-lang/crates.io-index"
1519
- checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
1519
+ checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09"
1520
1520
  dependencies = [
1521
1521
  "serde_derive",
1522
1522
  ]
1523
1523
 
1524
1524
  [[package]]
1525
1525
  name = "serde_derive"
1526
- version = "1.0.208"
1526
+ version = "1.0.209"
1527
1527
  source = "registry+https://github.com/rust-lang/crates.io-index"
1528
- checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
1528
+ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
1529
1529
  dependencies = [
1530
1530
  "proc-macro2",
1531
1531
  "quote",
@@ -1545,9 +1545,9 @@ dependencies = [
1545
1545
 
1546
1546
  [[package]]
1547
1547
  name = "serde_json"
1548
- version = "1.0.125"
1548
+ version = "1.0.127"
1549
1549
  source = "registry+https://github.com/rust-lang/crates.io-index"
1550
- checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
1550
+ checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
1551
1551
  dependencies = [
1552
1552
  "indexmap",
1553
1553
  "itoa",
@@ -7,7 +7,7 @@ resolver = "2"
7
7
  lto = true
8
8
 
9
9
  [workspace.package]
10
- version = "0.26.2"
10
+ version = "0.26.3"
11
11
  authors = ["Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>"]
12
12
  edition = "2021"
13
13
  license = "MIT"
@@ -18,11 +18,11 @@ rust-version = "1.67"
18
18
  readme = "README.md"
19
19
 
20
20
  [workspace.dependencies]
21
- ast-grep-core = { path = "crates/core", version = "0.26.2" }
22
- ast-grep-config = { path = "crates/config", version = "0.26.2" }
23
- ast-grep-dynamic = { path = "crates/dynamic", version = "0.26.2" }
24
- ast-grep-language = { path = "crates/language", version = "0.26.2" }
25
- ast-grep-lsp = { path = "crates/lsp", version = "0.26.2" }
21
+ ast-grep-core = { path = "crates/core", version = "0.26.3" }
22
+ ast-grep-config = { path = "crates/config", version = "0.26.3" }
23
+ ast-grep-dynamic = { path = "crates/dynamic", version = "0.26.3" }
24
+ ast-grep-language = { path = "crates/language", version = "0.26.3" }
25
+ ast-grep-lsp = { path = "crates/lsp", version = "0.26.3" }
26
26
 
27
27
  bit-set = { version = "0.8.0" }
28
28
  ignore = { version = "0.4.22" }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ast-grep-cli
3
- Version: 0.26.2
3
+ Version: 0.26.3
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Environment :: Console
6
6
  Classifier: Intended Audience :: Developers
@@ -26,4 +26,4 @@ thiserror.workspace = true
26
26
  schemars.workspace = true
27
27
 
28
28
  [dev-dependencies]
29
- tree-sitter-typescript = "0.21.1"
29
+ tree-sitter-typescript = "0.21.1"
@@ -118,4 +118,30 @@ mod test {
118
118
  let ret = Transform::deserialize(&trans, &env);
119
119
  assert!(ret.is_ok());
120
120
  }
121
+
122
+ #[test]
123
+ fn test_transform_indentation() {
124
+ let src = "
125
+ if (true) {
126
+ let a = {
127
+ b: 123
128
+ }
129
+ }
130
+ ";
131
+ let expected = "{
132
+ b: 123
133
+ }";
134
+ let mut trans = HashMap::new();
135
+ let tr = from_str("{ substring: { source: $A } }").expect("should work");
136
+ trans.insert("TR".into(), tr);
137
+ let grep = TypeScript::Tsx.ast_grep(src);
138
+ let root = grep.root();
139
+ let mut nm = root.find("let a = $A").expect("should find");
140
+ let env = DeserializeEnv::new(TypeScript::Tsx);
141
+ let trans = Transform::deserialize(&trans, &env).expect("should deserialize");
142
+ trans.apply_transform(nm.get_env_mut(), &Default::default(), &Default::default());
143
+ let actual = nm.get_env().get_transformed("TR").expect("should have TR");
144
+ let actual = std::str::from_utf8(actual).expect("should work");
145
+ assert_eq!(actual, expected);
146
+ }
121
147
  }
@@ -109,6 +109,18 @@ pub enum Transformation<T> {
109
109
  Rewrite(Rewrite<T>),
110
110
  }
111
111
 
112
+ impl<T> Transformation<T> {
113
+ fn source(&self) -> &T {
114
+ use Transformation as T;
115
+ match self {
116
+ T::Replace(r) => &r.source,
117
+ T::Substring(s) => &s.source,
118
+ T::Convert(c) => &c.source,
119
+ T::Rewrite(r) => &r.source,
120
+ }
121
+ }
122
+ }
123
+
112
124
  pub(crate) fn parse_meta_var<L: Language>(
113
125
  src: &str,
114
126
  lang: &L,
@@ -148,38 +160,25 @@ impl Transformation<String> {
148
160
  }
149
161
 
150
162
  pub fn used_vars(&self) -> &str {
151
- fn strip(s: &str) -> &str {
152
- s.strip_prefix("$$$").unwrap_or_else(|| &s[1..])
153
- // match s {
154
- // MetaVariable::Capture(s, _) => s,
155
- // MetaVariable::Dropped(_) => "",
156
- // MetaVariable::MultiCapture(s) => s,
157
- // MetaVariable::Multiple => "",
158
- // }
159
- }
160
- use Transformation as T;
161
163
  // NOTE: meta_var in transform always starts with `$`, for now
162
- match self {
163
- T::Replace(r) => strip(&r.source),
164
- T::Substring(s) => strip(&s.source),
165
- T::Convert(c) => strip(&c.source),
166
- T::Rewrite(r) => strip(&r.source),
167
- }
164
+ let s = self.source();
165
+ s.strip_prefix("$$$").unwrap_or_else(|| &s[1..])
168
166
  }
169
167
  }
170
168
  impl Transformation<MetaVariable> {
171
169
  pub(super) fn insert<D: Doc>(&self, key: &str, ctx: &mut Ctx<D>) {
170
+ let src = self.source();
172
171
  // TODO: add this debug assertion back
173
172
  // debug_assert!(ctx.env.get_transformed(key).is_none());
174
173
  // avoid cyclic
175
- ctx.env.insert_transformation(key, vec![]);
174
+ ctx.env.insert_transformation(src, key, vec![]);
176
175
  let opt = self.compute(ctx);
177
176
  let bytes = if let Some(s) = opt {
178
177
  <D::Source as Content>::decode_str(&s).to_vec()
179
178
  } else {
180
179
  vec![]
181
180
  };
182
- ctx.env.insert_transformation(key, bytes);
181
+ ctx.env.insert_transformation(src, key, bytes);
183
182
  }
184
183
  fn compute<D: Doc>(&self, ctx: &mut Ctx<D>) -> Option<String> {
185
184
  use Transformation as T;
@@ -436,5 +435,25 @@ mod test {
436
435
  assert_eq!(actual, "camelcase_not");
437
436
  Ok(())
438
437
  }
438
+
439
+ #[test]
440
+ fn test_transform_indentation_with_insertion() -> R {
441
+ let src = "
442
+ if (true) {
443
+ let a = {
444
+ b: 123
445
+ }
446
+ }
447
+ ";
448
+ // note the indentation
449
+ let expected = "{
450
+ b: 123
451
+ }";
452
+ let tr = parse("{ substring: { source: $A } }")?;
453
+ let actual = get_transformed(src, "let a = $A", &tr).ok_or(())?;
454
+ assert_eq!(actual, expected);
455
+ Ok(())
456
+ }
457
+
439
458
  // TODO: add a symbolic test for Rewrite
440
459
  }
@@ -23,4 +23,4 @@ tree-sitter.workspace = true
23
23
  default = ["regex"]
24
24
 
25
25
  [dev-dependencies]
26
- tree-sitter-typescript = "0.21.1"
26
+ tree-sitter-typescript = "0.21.1"
@@ -39,7 +39,10 @@ impl MatchStrictness {
39
39
  ) -> MatchOneNode {
40
40
  use MatchStrictness as M;
41
41
  let k = candidate.kind_id();
42
- if k == kind && text == candidate.text() {
42
+ // work around ast-grep/ast-grep#1419 and tree-sitter/tree-sitter-typescript#306
43
+ // tree-sitter-typescript has wrong span of unnamed node so text would not match
44
+ // just compare kind for unnamed node
45
+ if k == kind && (!is_named || text == candidate.text()) {
43
46
  return MatchOneNode::MatchedBoth;
44
47
  }
45
48
  let (skip_goal, skip_candidate) = match self {
@@ -5,6 +5,8 @@ use crate::{Doc, Language, Node, StrDoc};
5
5
  use std::borrow::Cow;
6
6
  use std::collections::HashMap;
7
7
 
8
+ use crate::replacer::formatted_slice;
9
+
8
10
  pub type MetaVariableID = String;
9
11
 
10
12
  type Underlying<D> = Vec<<<D as Doc>::Source as Content>::Underlying>;
@@ -44,8 +46,18 @@ impl<'tree, D: Doc> MetaVarEnv<'tree, D> {
44
46
  }
45
47
  }
46
48
 
47
- pub fn insert_transformation(&mut self, name: &str, src: Underlying<D>) {
48
- self.transformed_var.insert(name.to_string(), src);
49
+ pub fn insert_transformation(&mut self, var: &MetaVariable, name: &str, slice: Underlying<D>) {
50
+ let node = match var {
51
+ MetaVariable::Capture(v, _) => self.single_matched.get(v),
52
+ MetaVariable::MultiCapture(vs) => self.multi_matched.get(vs).and_then(|vs| vs.first()),
53
+ _ => None,
54
+ };
55
+ let deindented = if let Some(v) = node {
56
+ formatted_slice(&slice, v.root.doc.get_source(), v.range().start).to_vec()
57
+ } else {
58
+ slice
59
+ };
60
+ self.transformed_var.insert(name.to_string(), deindented);
49
61
  }
50
62
 
51
63
  pub fn get_match(&self, var: &str) -> Option<&'_ Node<'tree, D>> {
@@ -146,6 +146,27 @@ pub fn extract_with_deindent<C: Content>(content: &C, range: Range<usize>) -> De
146
146
  DeindentedExtract::MultiLine(extract_slice, indent)
147
147
  }
148
148
 
149
+ fn deindent_slice<'a, C: Content>(
150
+ slice: &'a [C::Underlying],
151
+ content: &'a C,
152
+ start: usize,
153
+ ) -> DeindentedExtract<'a, C> {
154
+ if !slice.contains(&get_new_line::<C>()) {
155
+ return DeindentedExtract::SingleLine(slice);
156
+ }
157
+ let indent = get_indent_at_offset::<C>(content.get_range(0..start));
158
+ DeindentedExtract::MultiLine(slice, indent)
159
+ }
160
+
161
+ pub fn formatted_slice<'a, C: Content>(
162
+ slice: &'a [C::Underlying],
163
+ content: &'a C,
164
+ start: usize,
165
+ ) -> Cow<'a, [C::Underlying]> {
166
+ let deindent = deindent_slice(slice, content, start);
167
+ indent_lines(0, deindent)
168
+ }
169
+
149
170
  pub fn indent_lines<C: Content>(
150
171
  indent: usize,
151
172
  extract: DeindentedExtract<C>,
@@ -4,6 +4,8 @@ use crate::source::Edit as E;
4
4
  use crate::{Doc, Node, Root};
5
5
  use std::ops::Range;
6
6
 
7
+ pub(crate) use indent::formatted_slice;
8
+
7
9
  type Edit<D> = E<<D as Doc>::Source>;
8
10
  type Underlying<S> = Vec<<S as Content>::Underlying>;
9
11
 
@@ -17,4 +17,4 @@ ignore.workspace = true
17
17
  libloading = "0.8.3"
18
18
  serde.workspace = true
19
19
  thiserror.workspace = true
20
- tree-sitter-native = { version = "0.22.6", package = "tree-sitter" }
20
+ tree-sitter-native = { version = "0.22.6", package = "tree-sitter" }
@@ -66,4 +66,4 @@ builtin-parser = [
66
66
  "tree-sitter-swift",
67
67
  "tree-sitter-typescript",
68
68
  ]
69
- default = ["builtin-parser"]
69
+ default = ["builtin-parser"]
@@ -31,4 +31,4 @@ tokio = { version = "1.37.0", features = [
31
31
  "rt-multi-thread",
32
32
  "io-std",
33
33
  "io-util",
34
- ] }
34
+ ] }
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "ast-grep-cli"
7
- version = "0.26.2"
7
+ version = "0.26.3"
8
8
  description = "Structural Search and Rewrite code at large scale using precise AST pattern."
9
9
  authors = [{ name = "Herrington Darkholme", email = "2883231+HerringtonDarkholme@users.noreply.github.com" }]
10
10
  maintainers = [{ name = "Herrington Darkholme", email = "2883231+HerringtonDarkholme@users.noreply.github.com" }]
File without changes
File without changes