diff --git a/Cargo.toml b/Cargo.toml index 4b2ac22d4..d3879195e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,8 @@ tree-sitter-thrift = "0.5.0" tree-sitter-strings = { git = "https://github.com/uber/tree-sitter-strings.git" } tree-sitter-query = "0.1.0" tree-sitter-scala = "0.20.1" +# newer versions of "tree-sitter-yaml" require that we bump "tree-sitter" version +tree-sitter-yaml = "0.0.1" derive_builder = "0.12.0" getset = "0.1.2" pyo3 = "0.20.0" diff --git a/polyglot_piranha.pyi b/polyglot_piranha.pyi index f4ab1fbb4..8700b3bff 100644 --- a/polyglot_piranha.pyi +++ b/polyglot_piranha.pyi @@ -13,7 +13,7 @@ from __future__ import annotations from typing import List, Optional, Literal # Languages that Piranha supports (see ./src/models/language.rs) -PiranhaLanguage = Literal["java", "kt", "kotlin", "go", "py", "swift", "ts", "tsx", "thrift", "strings", "scm", "scala", "rb"] +PiranhaLanguage = Literal["java", "kt", "kotlin", "go", "py", "swift", "ts", "tsx", "thrift", "strings", "scm", "scala", "rb", "yaml", "yml"] def execute_piranha(piranha_argument: PiranhaArguments) -> list[PiranhaOutputSummary]: diff --git a/src/models/default_configs.rs b/src/models/default_configs.rs index 3df89f9d1..b610b08d7 100644 --- a/src/models/default_configs.rs +++ b/src/models/default_configs.rs @@ -34,6 +34,8 @@ pub const STRINGS: &str = "strings"; pub const TS_SCHEME: &str = "scm"; // We support scheme files that contain tree-sitter query pub const SCALA: &str = "scala"; pub const RUBY: &str = "rb"; +pub const YAML: &str = "yaml"; +pub const YAML_ALIAS: &str = "yml"; pub const REGEX_QUERY_PREFIX: &str = "rgx "; pub const CONCRETE_SYNTAX_QUERY_PREFIX: &str = "cs "; diff --git a/src/models/language.rs b/src/models/language.rs index e6c1e5155..13ac4fbf9 100644 --- a/src/models/language.rs +++ b/src/models/language.rs @@ -22,7 +22,7 @@ use crate::utilities::parse_toml; use super::{ default_configs::{ default_language, GO, JAVA, JAVA_CS, KOTLIN, KOTLIN_ALIAS, PYTHON, RUBY, SCALA, STRINGS, SWIFT, - THRIFT, TSX, TS_SCHEME, TYPESCRIPT, + THRIFT, TSX, TS_SCHEME, TYPESCRIPT, YAML, YAML_ALIAS, }, outgoing_edges::Edges, rule::Rules, @@ -69,6 +69,7 @@ pub enum SupportedLanguage { TsScheme, Scala, Ruby, + Yaml, } impl PiranhaLanguage { @@ -290,6 +291,15 @@ impl std::str::FromStr for PiranhaLanguage { edges: Some(ruby_edges), }) } + YAML | YAML_ALIAS => Ok(PiranhaLanguage { + extension: language.to_string(), + supported_language: SupportedLanguage::Yaml, + language: tree_sitter_yaml::language(), + rules: None, + edges: None, + scopes: vec![], + comment_nodes: vec![], + }), _ => Err("Language not supported"), } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index f5db3b601..09799cca2 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -40,6 +40,7 @@ mod test_piranha_thrift; mod test_piranha_ruby; mod test_piranha_scm; mod test_piranha_strings; +mod test_piranha_yaml; use std::sync::Once; @@ -165,7 +166,7 @@ fn execute_piranha_and_check_result( /// "java", /// test_a1: "relative/path_1", HashMap::from([("match_class", 2)); /// test_a2: "relative/path_2", HashMap::from([("match_class", 2), ("match_class_1", 1)]) -/// +/// /// ; /// } /// ``` diff --git a/src/tests/test_piranha_yaml.rs b/src/tests/test_piranha_yaml.rs new file mode 100644 index 000000000..fd293574e --- /dev/null +++ b/src/tests/test_piranha_yaml.rs @@ -0,0 +1,24 @@ +/* + Copyright (c) 2023 Uber Technologies, Inc. + +
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + except in compliance with the License. You may obtain a copy of the License at +
http://www.apache.org/licenses/LICENSE-2.0 + +
Unless required by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + express or implied. See the License for the specific language governing permissions and + limitations under the License. +*/ + +use crate::models::default_configs::YAML; + +use super::{create_rewrite_tests, substitutions}; + +create_rewrite_tests! { + YAML, + test_yaml_find_replace_with_holes: "find_replace/",1, + substitutions= substitutions! { + "value_to_replace" => "localhost" + }; +} diff --git a/test-resources/yaml/find_replace/configurations/rules.toml b/test-resources/yaml/find_replace/configurations/rules.toml new file mode 100644 index 000000000..e5c8a2bf8 --- /dev/null +++ b/test-resources/yaml/find_replace/configurations/rules.toml @@ -0,0 +1,32 @@ +# Copyright (c) 2024 Uber Technologies, Inc. +# +#
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of the License at +#
http://www.apache.org/licenses/LICENSE-2.0 +# +#
Unless required by applicable law or agreed to in writing, software distributed under the +# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +# express or implied. See the License for the specific language governing permissions and +# limitations under the License. + +[[rules]] +name = "replace_hostname_value" +query = """( + (block_mapping_pair + key: (flow_node) @config_key + value: (block_node + (block_mapping + (block_mapping_pair + key: (flow_node) @hostname_key + value: (flow_node) @hostname_value + ) @inner_mapping + ) @block_mapping + ) @block_node + (#eq? @config_key "config") + (#eq? @hostname_key "hostname") + (#eq? @hostname_value "\\\"@value_to_replace\\\"") + ) @outer_mapping +)""" +replace_node = "hostname_value" +replace = "\"127.0.0.1\"" +holes = ["value_to_replace"] diff --git a/test-resources/yaml/find_replace/expected/sample.yaml b/test-resources/yaml/find_replace/expected/sample.yaml new file mode 100644 index 000000000..54a123624 --- /dev/null +++ b/test-resources/yaml/find_replace/expected/sample.yaml @@ -0,0 +1,16 @@ +# Copyright (c) 2024 Uber Technologies, Inc. +# +#
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of the License at +#
http://www.apache.org/licenses/LICENSE-2.0 +# +#
Unless required by applicable law or agreed to in writing, software distributed under the +# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +# express or implied. See the License for the specific language governing permissions and +# limitations under the License. + +config: + hostname: "127.0.0.1" + port: 8080 + username: "admin" + password: "admin" diff --git a/test-resources/yaml/find_replace/input/sample.yaml b/test-resources/yaml/find_replace/input/sample.yaml new file mode 100644 index 000000000..f02464f3a --- /dev/null +++ b/test-resources/yaml/find_replace/input/sample.yaml @@ -0,0 +1,16 @@ +# Copyright (c) 2024 Uber Technologies, Inc. +# +#
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of the License at +#
http://www.apache.org/licenses/LICENSE-2.0 +# +#
Unless required by applicable law or agreed to in writing, software distributed under the +# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +# express or implied. See the License for the specific language governing permissions and +# limitations under the License. + +config: + hostname: "localhost" + port: 8080 + username: "admin" + password: "admin"