Skip to content

Commit 5d9341c

Browse files
committed
Update tree-sitter-powershell to 0.25.10
1 parent 65bc42d commit 5d9341c

File tree

6 files changed

+148
-67
lines changed

6 files changed

+148
-67
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ minusone-cli = ["clap"]
2424

2525
[dependencies]
2626
tree-sitter = "0.25"
27-
tree-sitter-powershell = "0.25.9"
27+
tree-sitter-powershell = "0.25.10"
2828
clap = { version = "^2.33", optional = true }
2929
base64 = "0.21.5"
3030
num = "0.4.3"

src/ps/array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ mod test {
661661
.unwrap()
662662
.child(0)
663663
.unwrap()
664+
.child(0)
665+
.unwrap()
664666
.data()
665667
.expect("Inferred data"),
666668
Array(vec![Num(0); 16]),

src/ps/foreach.rs

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::error::{Error, MinusOneResult};
2+
use crate::ps::tool::CommandTool;
23
use crate::ps::Powershell;
34
use crate::ps::Powershell::{Array, PSItem, Raw};
45
use crate::rule::RuleMut;
@@ -84,20 +85,25 @@ impl<'a> RuleMut<'a> for PSItemInferrator {
8485
if let Some(script_block_expression) =
8586
view.get_parent_of_types(vec!["script_block_expression"])
8687
{
87-
if let Some(foreach_command) =
88-
script_block_expression.get_parent_of_types(vec!["foreach_command"])
88+
if let Some(command) = script_block_expression.get_parent_of_types(vec!["command"])
8989
{
90-
if let Some(previous) = find_previous_expr(&foreach_command.parent().unwrap())?
91-
{
92-
// the previous in the pipeline
93-
match previous.data() {
94-
Some(Array(values)) => {
95-
node.set(PSItem(values.clone()));
96-
}
97-
Some(Raw(value)) => {
98-
node.set(PSItem(vec![value.clone()]));
90+
if let Some(command_name) = command.child(0) {
91+
if matches!(
92+
command_name.text().unwrap(),
93+
"foreach" | "%" | "foreach-object"
94+
) {
95+
if let Some(previous) = find_previous_expr(&command)? {
96+
// the previous in the pipeline
97+
match previous.data() {
98+
Some(Array(values)) => {
99+
node.set(PSItem(values.clone()));
100+
}
101+
Some(Raw(value)) => {
102+
node.set(PSItem(vec![value.clone()]));
103+
}
104+
_ => (),
105+
}
99106
}
100-
_ => (),
101107
}
102108
}
103109
}
@@ -163,61 +169,67 @@ impl<'a> RuleMut<'a> for ForEach {
163169
) -> MinusOneResult<()> {
164170
let view = node.view();
165171
// find usage of magic variable
166-
if view.kind() == "foreach_command"
167-
&& view.child_count() == 2
168-
&& view.child(1).unwrap().kind() == "script_block_expression"
169-
{
170-
let script_block_expression = view.child(1).unwrap();
171-
if let Some(previous_command) = find_previous_expr(&view.parent().unwrap())? {
172-
// if the previous pipeline was inferred as an array
173-
let mut previous_values = Vec::new();
174-
match previous_command.data() {
175-
Some(Array(values)) => previous_values.extend(values.clone()),
176-
// array of size 1
177-
Some(Raw(value)) => previous_values.push(value.clone()),
178-
_ => (),
179-
}
180-
let script_block_body = script_block_expression
181-
.child(1)
182-
.ok_or(Error::invalid_child())? // script_block node
183-
.named_child("script_block_body");
172+
if view.is_command() {
173+
let args = view.get_command_args();
174+
if matches!(
175+
view.get_command_name().as_str(),
176+
"foreach" | "foreach-object" | "%"
177+
) && args.len() == 1
178+
{
179+
let script_block_expression = args[0].smallest_child();
180+
if script_block_expression.kind() == "script_block_expression" {
181+
if let Some(previous_command) = find_previous_expr(&view)? {
182+
// if the previous pipeline was inferred as an array
183+
let mut previous_values = Vec::new();
184+
match previous_command.data() {
185+
Some(Array(values)) => previous_values.extend(values.clone()),
186+
// array of size 1
187+
Some(Raw(value)) => previous_values.push(value.clone()),
188+
_ => (),
189+
}
190+
let script_block_body = script_block_expression
191+
.child(1)
192+
.ok_or(Error::invalid_child())? // script_block node
193+
.named_child("script_block_body");
184194

185-
if let Some(script_block_body_node) = script_block_body {
186-
if let Some(statement_list) =
187-
script_block_body_node.named_child("statement_list")
188-
{
189-
// determine the number of loop
190-
// by looping over the size of the array
195+
if let Some(script_block_body_node) = script_block_body {
196+
if let Some(statement_list) =
197+
script_block_body_node.named_child("statement_list")
198+
{
199+
// determine the number of loop
200+
// by looping over the size of the array
191201

192-
let mut result = Vec::new();
193-
for i in 0..previous_values.len() {
194-
for child_statement in statement_list.iter() {
195-
if child_statement.kind() == "empty_statement" {
196-
continue;
197-
}
202+
let mut result = Vec::new();
203+
for i in 0..previous_values.len() {
204+
for child_statement in statement_list.iter() {
205+
if child_statement.kind() == "empty_statement" {
206+
continue;
207+
}
198208

199-
match child_statement.data() {
200-
Some(PSItem(values)) => {
201-
result.push(values[i].clone());
202-
}
203-
Some(Raw(r)) => {
204-
result.push(r.clone());
205-
}
206-
Some(Array(array_value)) => {
207-
for v in array_value {
208-
result.push(v.clone());
209+
match child_statement.data() {
210+
Some(PSItem(values)) => {
211+
result.push(values[i].clone());
212+
}
213+
Some(Raw(r)) => {
214+
result.push(r.clone());
215+
}
216+
Some(Array(array_value)) => {
217+
for v in array_value {
218+
result.push(v.clone());
219+
}
220+
}
221+
_ => {
222+
// stop inferring we have not enough infos
223+
return Ok(());
224+
}
209225
}
210226
}
211-
_ => {
212-
// stop inferring we have not enough infos
213-
return Ok(());
214-
}
227+
}
228+
if !result.is_empty() {
229+
node.set(Array(result));
215230
}
216231
}
217232
}
218-
if !result.is_empty() {
219-
node.set(Array(result));
220-
}
221233
}
222234
}
223235
}

src/ps/forward.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::process::Child;
2+
13
use crate::error::{Error, MinusOneResult};
24
use crate::ps::Powershell;
35
use crate::ps::Powershell::Null;
@@ -103,23 +105,28 @@ impl<'a> RuleMut<'a> for Forward {
103105
}
104106
}
105107

106-
// we infer pipeline type with the value of the last expression
107-
"pipeline" => {
108+
// we infer pipeline chain type with the value of the last expression
109+
"pipeline_chain" => {
108110
if let Some(expression) = view.child(view.child_count() - 1) {
109111
if let Some(expression_data) = expression.data() {
110112
node.set(expression_data.clone())
111113
}
112114
}
113115
}
114-
"command" => {
115-
if let Some(sub_command) = view.child(0) {
116-
if sub_command.kind() == "foreach_command" {
117-
if let Some(expression_data) = sub_command.data() {
118-
node.reduce(expression_data.clone())
116+
117+
// we infer pipeline only if there is one pipeline_chain child
118+
"pipeline" => {
119+
if view.child_count() == 1 {
120+
if let Some(child) = view.child(0) {
121+
if child.kind() == "pipeline_chain" {
122+
if let Some(expression_data) = child.data() {
123+
node.set(expression_data.clone())
124+
}
119125
}
120126
}
121127
}
122128
}
129+
123130
"type_literal" => {
124131
if let Some(expression) = view.child(1) {
125132
if let Some(expression_data) = expression.data() {

src/ps/tool.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::{ps::Powershell, tree::Node};
2+
13
pub trait StringTool {
24
fn remove_tilt(self) -> Self;
35
fn remove_quote(self) -> Self;
@@ -32,3 +34,37 @@ impl StringTool for String {
3234
self.to_lowercase().remove_tilt().remove_quote()
3335
}
3436
}
37+
38+
pub trait CommandTool<'a> {
39+
fn is_command(&self) -> bool;
40+
fn get_command_name(&self) -> String;
41+
fn get_command_args(&self) -> Vec<Node<'a, Powershell>>;
42+
fn get_command_params(&self) -> Vec<Node<'a, Powershell>>;
43+
}
44+
45+
impl<'a> CommandTool<'a> for Node<'a, Powershell> {
46+
fn is_command(&self) -> bool {
47+
self.kind() == "command"
48+
}
49+
50+
fn get_command_name(&self) -> String {
51+
self.child(0).unwrap().text().unwrap().to_owned()
52+
}
53+
54+
fn get_command_args(&self) -> Vec<Node<'a, Powershell>> {
55+
self.child(1)
56+
.unwrap()
57+
.iter()
58+
.filter(|n| n.kind() != "command_argument_sep" && n.kind() != "command_parameter")
59+
.collect()
60+
}
61+
62+
fn get_command_params(&self) -> Vec<Node<'a, Powershell>> {
63+
self.child(1)
64+
.unwrap()
65+
.iter()
66+
.skip(1)
67+
.filter(|n| n.kind() == "command_parameter")
68+
.collect()
69+
}
70+
}

src/ps/var.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ mod test {
743743
.child(1)
744744
.unwrap() // pipeline
745745
.child(0)
746+
.unwrap() // pipeline chain
747+
.child(0)
746748
.unwrap() //command
747749
.child(1)
748750
.unwrap() // command_elements
@@ -781,6 +783,8 @@ mod test {
781783
.child(1)
782784
.unwrap() // pipeline
783785
.child(0)
786+
.unwrap() // pipeline chain
787+
.child(0)
784788
.unwrap() //command
785789
.child(1)
786790
.unwrap() // command_elements
@@ -848,6 +852,8 @@ mod test {
848852
.child(2)
849853
.unwrap() // pipeline
850854
.child(0)
855+
.unwrap() // pipeline chain
856+
.child(0)
851857
.unwrap() //command
852858
.child(1)
853859
.unwrap() // command_elements
@@ -893,6 +899,8 @@ mod test {
893899
.child(2)
894900
.unwrap() // pipeline
895901
.child(0)
902+
.unwrap() // pipeline chain
903+
.child(0)
896904
.unwrap() //command
897905
.child(1)
898906
.unwrap() // command_elements
@@ -939,6 +947,8 @@ mod test {
939947
.child(2)
940948
.unwrap() // pipeline
941949
.child(0)
950+
.unwrap() // pipeline chain
951+
.child(0)
942952
.unwrap() //command
943953
.child(1)
944954
.unwrap() // command_elements
@@ -987,6 +997,8 @@ mod test {
987997
.child(2)
988998
.unwrap() // pipeline
989999
.child(0)
1000+
.unwrap() // pipeline chain
1001+
.child(0)
9901002
.unwrap() //command
9911003
.child(1)
9921004
.unwrap() // command_elements
@@ -1032,6 +1044,8 @@ mod test {
10321044
.child(2)
10331045
.unwrap() // pipeline
10341046
.child(0)
1047+
.unwrap() // pipeline chain
1048+
.child(0)
10351049
.unwrap() //command
10361050
.child(1)
10371051
.unwrap() // command_elements
@@ -1076,6 +1090,8 @@ mod test {
10761090
.child(2)
10771091
.unwrap() // pipeline
10781092
.child(0)
1093+
.unwrap() // pipeline chain
1094+
.child(0)
10791095
.unwrap() //command
10801096
.child(1)
10811097
.unwrap() // command_elements
@@ -1120,6 +1136,8 @@ mod test {
11201136
.child(2)
11211137
.unwrap() // pipeline
11221138
.child(0)
1139+
.unwrap() // pipeline chain
1140+
.child(0)
11231141
.unwrap() //command
11241142
.child(1)
11251143
.unwrap() // command_elements
@@ -1167,6 +1185,8 @@ mod test {
11671185
.child(2)
11681186
.unwrap() // pipeline
11691187
.child(0)
1188+
.unwrap() // pipeline chain
1189+
.child(0)
11701190
.unwrap() //command
11711191
.child(1)
11721192
.unwrap() // command_elements
@@ -1214,6 +1234,8 @@ mod test {
12141234
.child(2)
12151235
.unwrap() // pipeline
12161236
.child(0)
1237+
.unwrap() // pipeline chain
1238+
.child(0)
12171239
.unwrap() //command
12181240
.child(1)
12191241
.unwrap() // command_elements
@@ -2182,6 +2204,8 @@ mod test {
21822204
.child(3)
21832205
.unwrap() // Write-Host pipeline
21842206
.child(0)
2207+
.unwrap() // pipeline_chain
2208+
.child(0)
21852209
.unwrap() // Write-host command
21862210
.named_child("command_elements")
21872211
.unwrap() // cmd elements

0 commit comments

Comments
 (0)