Skip to content

Commit

Permalink
bugfix: fix handling of trailing elipsis operator (#91)
Browse files Browse the repository at this point in the history
Signed-off-by: Takashi Yoneuchi <takashi.yoneuchi@shift-js.info>
  • Loading branch information
lmt-swallow authored Sep 29, 2021
1 parent 223c272 commit 08a31bc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/cli/tests/ruleset/hcl/comment/match.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
resource "aws_ebs_volume" "volume" {
resource "comment_test" "volume" {
a = 1
// hoge
b = 1
}

resource "aws_ebs_volume" "volume" {
resource "comment_test" "volume" {
a = 1
// hoge
}

resource "aws_ebs_volume" "volume" {
resource "comment_test" "volume" {
// hoge
b = 1
}

resource "aws_ebs_volume" "volume" {
resource "comment_test" "volume" {
// hoge
}
2 changes: 1 addition & 1 deletion src/cli/tests/ruleset/hcl/comment/ruleset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ rules:
message: |
test
pattern: |
resource "aws_ebs_volume" :[NAME] {
resource "comment_test" :[NAME] {
:[...]
// hoge
:[...]
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tests/ruleset/hcl/comment/unmatch.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resource "aws_ebs_volume" "volume" {
resource "comment_test" "volume" {
size = 1
}
2 changes: 1 addition & 1 deletion src/cli/tests/ruleset/hcl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mod tests {
ruleset_test! {
unencrypted_ebs: [("ruleset.yaml", "match.tf", Ok(2), None), ("ruleset.yaml", "unmatch.tf", Ok(0), None)],
uncontrolled_ebs_encryption_key: [("ruleset.yaml", "match.tf", Ok(2), None), ("ruleset.yaml", "unmatch.tf", Ok(0), None)],
// comment: [("ruleset.yaml", "match.tf", Ok(3), None), ("ruleset.yaml", "unmatch.tf", Ok(0), None)],
comment: [("ruleset.yaml", "match.tf", Ok(4), None), ("ruleset.yaml", "unmatch.tf", Ok(0), None)],
}
}
36 changes: 30 additions & 6 deletions src/core/matcher/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,44 @@ impl<'tree, 'query, T: Queryable> TreeMatcher<'tree, 'query, T> {
))
}

(None, Some(qchild)) => match qchild.kind() {
NodeType::Ellipsis => {
let nodes = tsibilings[..tidx.min(tsibilings.len())].to_vec();
result.push((
MatcherState {
subtree: ConsecutiveNodes::try_from(nodes).ok(),
captures,
},
None,
))
}
NodeType::EllipsisMetavariable(mid) => {
let nodes = tsibilings[..tidx.min(tsibilings.len())].to_vec();
result.push((
MatcherState {
subtree: ConsecutiveNodes::try_from(nodes).ok(),
captures: [
captures.clone(),
vec![(mid, CaptureItem::from(vec![]))],
]
.concat(),
},
None,
))
}
_ => {}
},
(Some(tchild), Some(qchild)) => match qchild.kind() {
NodeType::Ellipsis => {
let mut captured_nodes = vec![];
// NOTE: this loop must end with `tsibilings.len()`
for tcidx in tidx..=tsibilings.len() {
queue.push((tcidx, qidx + 1, captures.clone()));
if let Some(tchild) = tsibilings.get(tcidx) {
captured_nodes.push(tchild);
}
}
}
NodeType::EllipsisMetavariable(mid) => {
let mut captured_nodes = vec![];
for tcidx in tidx..(tsibilings.len() + 1) {
// NOTE: this loop must end with `tsibilings.len()`
for tcidx in tidx..=tsibilings.len() {
queue.push((
tcidx,
qidx + 1,
Expand All @@ -124,7 +149,6 @@ impl<'tree, 'query, T: Queryable> TreeMatcher<'tree, 'query, T> {
}
}
},
_ => (),
}
}
result
Expand Down
2 changes: 1 addition & 1 deletion third_party/tree-sitter-hcl
2 changes: 1 addition & 1 deletion third_party/tree-sitter-hcl-query

0 comments on commit 08a31bc

Please sign in to comment.