From 8165832418f0b1a649f2e6caf58d17f1224d7375 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Fri, 15 Nov 2024 15:03:27 -0500 Subject: [PATCH] chore: capture the full range for markdown patterns --- Cargo.lock | 56 +++---- .../tests/snapshots/list__list_jsonl.snap | 96 +++++++++--- .../snapshots/patterns__gets_json_output.snap | 108 +++++++++---- .../private__run_pattern_with_private.snap | 48 ++++-- crates/gritmodule/src/config.rs | 6 +- crates/gritmodule/src/dot_grit.rs | 18 ++- crates/gritmodule/src/markdown.rs | 8 +- ...dot_grit__tests__gets_module_patterns.snap | 36 +++-- ...ule__markdown__tests__markdown_parser.snap | 12 +- ...wn__tests__multiple_markdown_patterns.snap | 24 ++- ...odule__markdown__tests__sample_labels.snap | 12 +- ...dule__markdown__tests__weird_patterns.snap | 12 +- ..._markdown__tests__with_no_frontmatter.snap | 12 +- ...own__tests__with_non_matching_samples.snap | 12 +- ...ds_patterns_from_custom_pattern_files.snap | 48 ++++-- ...e_direct_imports_with_same_local_name.snap | 24 ++- ...amespace_imports_with_same_local_name.snap | 24 ++- ...language_imports_with_same_local_name.snap | 24 ++- ...ver__tests__resolve_enforcement_level.snap | 36 +++-- ...e__resolver__tests__resolve_from_root.snap | 144 +++++++++++++----- ...olver__tests__resolve_multiple_layers.snap | 36 +++-- ...esolver__tests__resolve_single_module.snap | 72 ++++++--- ...le__resolver__tests__without_grit_yml.snap | 24 ++- ...le__yaml__tests__gets_module_patterns.snap | 48 ++++-- crates/gritmodule/src/yaml.rs | 10 +- crates/lsp/src/server.rs | 4 +- 26 files changed, 697 insertions(+), 257 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae8a5b98b..d9de33f81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1412,6 +1412,34 @@ dependencies = [ "regex-syntax 0.8.2", ] +[[package]] +name = "grit" +version = "0.1.0" +dependencies = [ + "anyhow", + "assert_cmd", + "fs-err", + "fs_extra", + "insta", + "marzano-cli", + "marzano-gritmodule", + "marzano-util", + "ntest", + "opentelemetry", + "opentelemetry-otlp", + "opentelemetry_sdk", + "predicates", + "rayon", + "regex", + "reqwest 0.11.24", + "serde_json", + "tempfile", + "tokio", + "tracing", + "tracing-opentelemetry", + "tracing-subscriber", +] + [[package]] name = "grit-pattern-matcher" version = "0.5.0" @@ -2151,34 +2179,6 @@ dependencies = [ "quote", ] -[[package]] -name = "marzano" -version = "0.1.0" -dependencies = [ - "anyhow", - "assert_cmd", - "fs-err", - "fs_extra", - "insta", - "marzano-cli", - "marzano-gritmodule", - "marzano-util", - "ntest", - "opentelemetry", - "opentelemetry-otlp", - "opentelemetry_sdk", - "predicates", - "rayon", - "regex", - "reqwest 0.11.24", - "serde_json", - "tempfile", - "tokio", - "tracing", - "tracing-opentelemetry", - "tracing-subscriber", -] - [[package]] name = "marzano-auth" version = "0.1.0" diff --git a/crates/cli_bin/tests/snapshots/list__list_jsonl.snap b/crates/cli_bin/tests/snapshots/list__list_jsonl.snap index 805ba2cf2..b6ed4b657 100644 --- a/crates/cli_bin/tests/snapshots/list__list_jsonl.snap +++ b/crates/cli_bin/tests/snapshots/list__list_jsonl.snap @@ -11,9 +11,15 @@ expression: parsed_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 18 - column: 9 + range: + start: + line: 18 + column: 9 + end: + line: 18 + column: 22 + startByte: 322 + endByte: 335 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: parsed_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 5 - column: 9 + range: + start: + line: 5 + column: 9 + end: + line: 5 + column: 21 + startByte: 75 + endByte: 87 raw: ~ module: type: Module @@ -59,9 +71,15 @@ expression: parsed_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 1 - column: 9 + range: + start: + line: 1 + column: 9 + end: + line: 1 + column: 21 + startByte: 8 + endByte: 20 raw: ~ module: type: Module @@ -83,9 +101,15 @@ expression: parsed_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 9 - column: 9 + range: + start: + line: 9 + column: 9 + end: + line: 9 + column: 24 + startByte: 142 + endByte: 157 raw: ~ module: type: Module @@ -196,9 +220,15 @@ expression: parsed_patterns endByte: 1380 output_range: ~ path: ".grit/patterns/broken_pattern.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null` to change\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n console.log(\"This should be removed by grit patterns test --update\");\n}\n\n```\n\n## `$val != null` => `$val !== null` not channge\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while` not change\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null` not change\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null` not change\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -313,9 +343,15 @@ expression: parsed_patterns endByte: 1447 output_range: ~ path: ".grit/patterns/multiple_broken_patterns.md" - position: - line: 9 - column: 1 + range: + start: + line: 9 + column: 1 + end: + line: 19 + column: 1 + startByte: 212 + endByte: 662 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\ntags: [\"good\", \"exclude2\"]\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null` to change\n\n```javascript\nif (val == null) {\n done();\n cnonsole.log(\"This should be added to the output by patterns test --update\");\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` to channge\n\n```javascript\nif (val != null) {\n done();\n console.log(\"This should be added as well\");\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while` not change\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null` not change\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null` not change\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -428,9 +464,15 @@ expression: parsed_patterns endByte: 1263 output_range: ~ path: ".grit/patterns/no_eq_null.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 209 + endByte: 659 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good, #exclude1\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -454,9 +496,15 @@ expression: parsed_patterns tags: ~ samples: ~ path: ".grit/grit.yml" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: type: Module diff --git a/crates/cli_bin/tests/snapshots/patterns__gets_json_output.snap b/crates/cli_bin/tests/snapshots/patterns__gets_json_output.snap index 5d457cf76..b86cb0d82 100644 --- a/crates/cli_bin/tests/snapshots/patterns__gets_json_output.snap +++ b/crates/cli_bin/tests/snapshots/patterns__gets_json_output.snap @@ -11,9 +11,15 @@ expression: v tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 18 - column: 9 + range: + start: + line: 18 + column: 9 + end: + line: 18 + column: 22 + startByte: 322 + endByte: 335 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: v tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 5 - column: 9 + range: + start: + line: 5 + column: 9 + end: + line: 5 + column: 21 + startByte: 75 + endByte: 87 raw: ~ module: type: Module @@ -59,9 +71,15 @@ expression: v tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 1 - column: 9 + range: + start: + line: 1 + column: 9 + end: + line: 1 + column: 21 + startByte: 8 + endByte: 20 raw: ~ module: type: Module @@ -83,9 +101,15 @@ expression: v tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 9 - column: 9 + range: + start: + line: 9 + column: 9 + end: + line: 9 + column: 24 + startByte: 142 + endByte: 157 raw: ~ module: type: Module @@ -196,9 +220,15 @@ expression: v endByte: 1380 output_range: ~ path: ".grit/patterns/broken_pattern.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null` to change\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n console.log(\"This should be removed by grit patterns test --update\");\n}\n\n```\n\n## `$val != null` => `$val !== null` not channge\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while` not change\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null` not change\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null` not change\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -222,9 +252,15 @@ expression: v tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 13 - column: 10 + range: + start: + line: 13 + column: 10 + end: + line: 13 + column: 28 + startByte: 216 + endByte: 234 raw: ~ module: type: Module @@ -337,9 +373,15 @@ expression: v endByte: 1447 output_range: ~ path: ".grit/patterns/multiple_broken_patterns.md" - position: - line: 9 - column: 1 + range: + start: + line: 9 + column: 1 + end: + line: 19 + column: 1 + startByte: 212 + endByte: 662 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\ntags: [\"good\", \"exclude2\"]\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null` to change\n\n```javascript\nif (val == null) {\n done();\n cnonsole.log(\"This should be added to the output by patterns test --update\");\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` to channge\n\n```javascript\nif (val != null) {\n done();\n console.log(\"This should be added as well\");\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while` not change\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null` not change\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null` not change\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -452,9 +494,15 @@ expression: v endByte: 1263 output_range: ~ path: ".grit/patterns/no_eq_null.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 209 + endByte: 659 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good, #exclude1\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -478,9 +526,15 @@ expression: v tags: ~ samples: ~ path: ".grit/grit.yml" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: type: Module diff --git a/crates/cli_bin/tests/snapshots/private__run_pattern_with_private.snap b/crates/cli_bin/tests/snapshots/private__run_pattern_with_private.snap index 4d180dfb3..723c74139 100644 --- a/crates/cli_bin/tests/snapshots/private__run_pattern_with_private.snap +++ b/crates/cli_bin/tests/snapshots/private__run_pattern_with_private.snap @@ -11,9 +11,15 @@ expression: parsed tags: ~ samples: ~ path: ".grit/patterns/patterns.grit" - position: - line: 9 - column: 9 + range: + start: + line: 9 + column: 9 + end: + line: 9 + column: 17 + startByte: 122 + endByte: 130 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: parsed tags: ~ samples: [] path: ".grit/patterns/marky.md" - position: - line: 6 - column: 1 + range: + start: + line: 6 + column: 1 + end: + line: 19 + column: 1 + startByte: 75 + endByte: 236 raw: format: markdown content: "# Markdown Pattern\n\nThis is to show how to use markdown patterns.\n\n```grit\npattern show_this_one() {\n `foo` => `bar`\n}\n\nprivate pattern hide_this() {\n `foods` => `calories`\n}\n\nor {\n `foo` => `bar`,\n show_this_one(),\n hide_this()\n}\n```\n" @@ -62,9 +74,15 @@ expression: parsed - hidden samples: ~ path: ".grit/patterns/patterns.grit" - position: - line: 5 - column: 17 + range: + start: + line: 5 + column: 17 + end: + line: 5 + column: 32 + startByte: 65 + endByte: 80 raw: ~ module: type: Module @@ -86,9 +104,15 @@ expression: parsed tags: ~ samples: ~ path: ".grit/patterns/patterns.grit" - position: - line: 1 - column: 9 + range: + start: + line: 1 + column: 9 + end: + line: 1 + column: 23 + startByte: 8 + endByte: 22 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/config.rs b/crates/gritmodule/src/config.rs index c5c64941b..d11ca5b25 100644 --- a/crates/gritmodule/src/config.rs +++ b/crates/gritmodule/src/config.rs @@ -1,5 +1,5 @@ use anyhow::Context; -use grit_util::{Position, Range}; +use grit_util::Range; use log::info; use marzano_core::api::EnforcementLevel; use marzano_language::{grit_parser::MarzanoGritParser, target_language::PatternLanguage}; @@ -101,7 +101,7 @@ pub struct GritDefinitionConfig { pub kind: Option, pub samples: Option>, pub path: String, - pub position: Option, + pub range: Option, pub raw: Option, } @@ -114,7 +114,7 @@ impl GritDefinitionConfig { kind: serialized.kind, samples: serialized.samples, path, - position: None, + range: None, raw: None, } } diff --git a/crates/gritmodule/src/dot_grit.rs b/crates/gritmodule/src/dot_grit.rs index 35d23eeff..13673647d 100644 --- a/crates/gritmodule/src/dot_grit.rs +++ b/crates/gritmodule/src/dot_grit.rs @@ -7,7 +7,7 @@ use crate::{ parser::extract_relative_file_path, }; use anyhow::{anyhow, bail, Result}; -use grit_util::{Ast, AstNode, Position}; +use grit_util::{Ast, AstNode, Position, Range}; use marzano_language::{grit_parser::MarzanoGritParser, target_language::PatternLanguage}; use marzano_util::rich_path::RichFile; use std::collections::HashMap; @@ -59,9 +59,17 @@ pub fn get_patterns_from_grit( } None => plain_body.to_string(), }; - let position = Position::new( - name_node.node.start_position().row() + 1, - name_node.node.start_position().column() + 1, + let range = Range::new( + Position::new( + name_node.node.start_position().row() + 1, + name_node.node.start_position().column() + 1, + ), + Position::new( + name_node.node.end_position().row() + 1, + name_node.node.end_position().column() + 1, + ), + name_node.node.start_byte(), + name_node.node.end_byte(), ); let module_grit_pattern = ModuleGritPattern { @@ -70,7 +78,7 @@ pub fn get_patterns_from_grit( body: Some(body.to_string()), kind, path: extract_relative_file_path(file, root), - position: Some(position), + range: Some(range), meta: GritPatternMetadata { tags: if is_public { None diff --git a/crates/gritmodule/src/markdown.rs b/crates/gritmodule/src/markdown.rs index 0673fc3f7..e680cde5a 100644 --- a/crates/gritmodule/src/markdown.rs +++ b/crates/gritmodule/src/markdown.rs @@ -7,7 +7,7 @@ use crate::{ }; use anyhow::{bail, Context, Result}; use fs_err::OpenOptions; -use grit_util::{traverse, Ast, Order, Position}; +use grit_util::{traverse, Ast, Order, Range}; use marzano_core::analysis::defines_itself; use marzano_core::api::EnforcementLevel; use marzano_language::grit_parser::MarzanoGritParser; @@ -30,7 +30,7 @@ fn parse_metadata(yaml_content: &str) -> Result { #[derive(Debug)] struct MarkdownBody { body: String, - position: Position, + range: Range, section_heading: String, section_level: u32, /// Track the samples which have already been matched together @@ -108,7 +108,7 @@ pub fn get_patterns_from_md( if current_code_block_language == Some(std::borrow::Cow::Borrowed("grit")) { let definition = MarkdownBody { body: content.to_string(), - position: n.range().start, + range: n.range(), section_heading: current_heading.1.clone(), section_level: current_heading.0, samples: Vec::new(), @@ -232,7 +232,7 @@ js"hello world" kind: Some(DefinitionKind::Pattern), samples: Some(p.samples), path: relative_path.clone(), - position: Some(p.position), + range: Some(p.range), raw: Some(RawGritDefinition { content: src.to_string(), format: crate::parser::PatternFileExt::Md, diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__dot_grit__tests__gets_module_patterns.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__dot_grit__tests__gets_module_patterns.snap index 3bcc6ac63..a150af165 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__dot_grit__tests__gets_module_patterns.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__dot_grit__tests__gets_module_patterns.snap @@ -11,9 +11,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 12 - column: 9 + range: + start: + line: 12 + column: 9 + end: + line: 12 + column: 27 + startByte: 120 + endByte: 138 raw: ~ module: ~ local_name: no_tests_with_only @@ -26,9 +32,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 18 - column: 9 + range: + start: + line: 18 + column: 9 + end: + line: 18 + column: 27 + startByte: 235 + endByte: 253 raw: ~ module: ~ local_name: no_tests_with_skip @@ -41,9 +53,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 24 + startByte: 41 + endByte: 56 raw: ~ module: ~ local_name: test_identifier diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__markdown_parser.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__markdown_parser.snap index 740d3438a..48e1173ea 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__markdown_parser.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__markdown_parser.snap @@ -33,9 +33,15 @@ expression: patterns startByte: 368 endByte: 414 path: no_debugger.md - position: - line: 9 - column: 1 + range: + start: + line: 9 + column: 1 + end: + line: 13 + column: 1 + startByte: 193 + endByte: 252 raw: format: markdown content: "---\ntitle: Remove `debugger` statement\ntags: [fix]\n---\n\nThe code in production should not contain a `debugger`. It causes the browser to stop executing the code and open the debugger.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\ndebugger_statement() => .\n```\n\n## Remove debbuger\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__multiple_markdown_patterns.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__multiple_markdown_patterns.snap index d3175a749..2bce65743 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__multiple_markdown_patterns.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__multiple_markdown_patterns.snap @@ -53,9 +53,15 @@ expression: patterns startByte: 410 endByte: 456 path: no_debugger.md - position: - line: 7 - column: 1 + range: + start: + line: 7 + column: 1 + end: + line: 11 + column: 1 + startByte: 86 + endByte: 145 raw: format: markdown content: "\nThis is a single Markdown file with multiple grit patterns.\n\n## Pattern one\n\n```grit\nengine marzano(0.1)\nlanguage js\n\ndebugger_statement() => .\n```\n\nExample:\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n### Test case 2\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n## Pattern two\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n`console.log($_)` => .\n```\n\n### Test case 3 - non matching.\n\n```javascript\nfunction isTruthy(x) {\n console.error(\"Hello\");\n return Boolean(x);\n}\n```\n\n### Test case 4\n\n```javascript\nfunction isTruthy(x) {\n console.log(\"Hello\");\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n" @@ -104,9 +110,15 @@ expression: patterns startByte: 790 endByte: 836 path: no_debugger.md - position: - line: 46 - column: 1 + range: + start: + line: 46 + column: 1 + end: + line: 50 + column: 1 + startByte: 485 + endByte: 541 raw: format: markdown content: "\nThis is a single Markdown file with multiple grit patterns.\n\n## Pattern one\n\n```grit\nengine marzano(0.1)\nlanguage js\n\ndebugger_statement() => .\n```\n\nExample:\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n### Test case 2\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n## Pattern two\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n`console.log($_)` => .\n```\n\n### Test case 3 - non matching.\n\n```javascript\nfunction isTruthy(x) {\n console.error(\"Hello\");\n return Boolean(x);\n}\n```\n\n### Test case 4\n\n```javascript\nfunction isTruthy(x) {\n console.log(\"Hello\");\n return Boolean(x);\n}\n```\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__sample_labels.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__sample_labels.snap index 524bb9110..8989ccc51 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__sample_labels.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__sample_labels.snap @@ -67,9 +67,15 @@ expression: patterns endByte: 735 output_range: ~ path: no_debugger.md - position: - line: 9 - column: 1 + range: + start: + line: 9 + column: 1 + end: + line: 13 + column: 1 + startByte: 193 + endByte: 252 raw: format: markdown content: "---\ntitle: Remove `debugger` statement\ntags: [fix]\n---\n\nThe code in production should not contain a `debugger`. It causes the browser to stop executing the code and open the debugger.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\ndebugger_statement() => .\n```\n\n## Remove debugger - pairs\n\n### Bad\n\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\n### Good\n\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n## Remove debugger - group 2\nBad:\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n\nGood:\n```typescript\nfunction isTruthy(x) {\n return Boolean(x);\n}\n```\n\n## Remove debugger - no match\nBad:\n```javascript\nfunction isTruthy(x) {\n debugger;\n return Boolean(x);\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__weird_patterns.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__weird_patterns.snap index 558fff477..1d6b05d26 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__weird_patterns.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__weird_patterns.snap @@ -33,9 +33,15 @@ expression: patterns startByte: 780 endByte: 1023 path: CloneActivities.md - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 271 + endByte: 528 raw: format: markdown content: "---\ntitle: \"Oracle to PG: Dollar quote stored procedure body\"\ntags: [fix]\n---\n\nIn Postgres, function and procedure bodies need to be wrapped in $$dollar quotes$$.\nThis pattern wraps a PLSQL `CREATE PROCEDURE` body in dollar quotes and adds a language specifier.\n\n```grit\nengine marzano(0.1)\nlanguage sql\n\npattern dollar_quote_procedure_body() {\n`CREATE PROCEDURE $name($args) AS $decl $block;` as $proc where {\n $block => `$$$block;\\n$$ LANGUAGE plpgsql`,\n $decl => `DECLARE\\n $decl`\n }\n}\ndollar_quote_procedure_body()\n```\n\n## Basic procedure\n\n```sql\nCREATE PROCEDURE remove_emp (employee_id int) AS\n tot_emps int;\n BEGIN\n DELETE FROM employees\n WHERE employees.employee_id = remove_emp.employee_id;\n tot_emps := tot_emps - 1;\n END;\n```\n\n```sql\nCREATE PROCEDURE remove_emp (employee_id int) AS\n DECLARE\n tot_emps int;\n $$BEGIN\n DELETE FROM employees\n WHERE employees.employee_id = remove_emp.employee_id;\n tot_emps := tot_emps - 1;\n END;\n$$ LANGUAGE plpgsql;\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_no_frontmatter.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_no_frontmatter.snap index 220b31450..ad6eb0438 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_no_frontmatter.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_no_frontmatter.snap @@ -11,9 +11,15 @@ expression: patterns tags: ~ samples: [] path: CloneActivities.md - position: - line: 6 - column: 1 + range: + start: + line: 6 + column: 1 + end: + line: 26 + column: 1 + startByte: 173 + endByte: 775 raw: format: markdown content: "# Copy activity names to timekeeper\n\nWe cannot directly include `@getgrit/sdk` in `@getgrit/timekeeper` but we _can_ use Grit to copy over the function names list.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\nmultifile {\n $names = [],\n bubble($names) file($name, $body) where {\n $name <: r\".+sdk/src/stdlib/index.ts\",\n $body <: contains `const stdlib = { $activities }` where {\n $activities <: some bubble($names) $activity where {\n $names += `'$activity'`\n }\n }\n },\n bubble($names) file($name, $body) where {\n $body <: contains `const stdlib = [$old] as const`,\n $name <: r\".+__generated__/stdlib.ts\",\n $new_names = join(list = $names, separator = \", \"),\n $old => `$new_names`,\n },\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_non_matching_samples.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_non_matching_samples.snap index b09b576ed..4f84b0ab6 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_non_matching_samples.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__markdown__tests__with_non_matching_samples.snap @@ -101,9 +101,15 @@ expression: patterns endByte: 1252 output_range: ~ path: no_eq_null.md - position: - line: 9 - column: 1 + range: + start: + line: 9 + column: 1 + end: + line: 19 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\ntags: [good]\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap index 31a4f226b..c407e76b1 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/grit.yaml" - position: - line: 7 - column: 11 + range: + start: + line: 7 + column: 11 + end: + line: 7 + column: 11 + startByte: 182 + endByte: 182 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: [] path: ".grit/../docs/guides/new-name.md" - position: - line: 6 - column: 1 + range: + start: + line: 6 + column: 1 + end: + line: 9 + column: 1 + startByte: 84 + endByte: 101 raw: format: markdown content: "# This pattern has a different name in the config.\n\nIt still works though!\n\n```grit\n\n`foo` => `bar`\n\n```\n" @@ -163,9 +175,15 @@ expression: resolved_patterns endByte: 1262 output_range: ~ path: ".grit/../docs/guides/something.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n```\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -291,9 +309,15 @@ expression: resolved_patterns endByte: 1262 output_range: ~ path: ".grit/../docs/guides/version_5_upgrade.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n```\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_direct_imports_with_same_local_name.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_direct_imports_with_same_local_name.snap index b8f7c3f34..82bcd8fbc 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_direct_imports_with_same_local_name.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_direct_imports_with_same_local_name.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/js/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 41 + endByte: 70 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/python/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 45 + endByte: 74 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_namespace_imports_with_same_local_name.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_namespace_imports_with_same_local_name.snap index b8f7c3f34..82bcd8fbc 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_namespace_imports_with_same_local_name.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_different_language_namespace_imports_with_same_local_name.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/js/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 41 + endByte: 70 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/python/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 45 + endByte: 74 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_mixed_language_imports_with_same_local_name.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_mixed_language_imports_with_same_local_name.snap index b8f7c3f34..82bcd8fbc 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_mixed_language_imports_with_same_local_name.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__recognizes_mixed_language_imports_with_same_local_name.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/js/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 41 + endByte: 70 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/fakeorg/python/.grit/patterns/importing.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 38 + startByte: 45 + endByte: 74 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_enforcement_level.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_enforcement_level.snap index 255b17748..020aa0908 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_enforcement_level.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_enforcement_level.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/grit.yaml" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/grit.yaml" - position: - line: 15 - column: 11 + range: + start: + line: 15 + column: 11 + end: + line: 15 + column: 11 + startByte: 266 + endByte: 266 raw: ~ module: type: Module @@ -59,9 +71,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/grit.yaml" - position: - line: 8 - column: 11 + range: + start: + line: 8 + column: 11 + end: + line: 8 + column: 11 + startByte: 135 + endByte: 135 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_from_root.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_from_root.snap index 7ee8818c7..4e5b94b90 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_from_root.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_from_root.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 5 - column: 9 + range: + start: + line: 5 + column: 9 + end: + line: 5 + column: 21 + startByte: 75 + endByte: 87 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 1 - column: 9 + range: + start: + line: 1 + column: 9 + end: + line: 1 + column: 21 + startByte: 8 + endByte: 20 raw: ~ module: type: Module @@ -59,9 +71,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 9 - column: 9 + range: + start: + line: 9 + column: 9 + end: + line: 9 + column: 24 + startByte: 142 + endByte: 157 raw: ~ module: type: Module @@ -83,9 +101,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/patterns/react_node.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 18 + startByte: 41 + endByte: 50 raw: ~ module: type: Module @@ -191,9 +215,15 @@ expression: resolved_patterns startByte: 3008 endByte: 3320 path: ".grit/.gritmodules/github.com/getgrit/js/.grit/patterns/es6_exports.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 51 + column: 1 + startByte: 173 + endByte: 2022 raw: format: markdown content: "---\ntitle: Prefer ES6-style exports over module.exports\n---\n\nConverts CommonJS `module.exports` to ES6-style exports.\n\ntags: #js, #es6, #migration, #cjs, #commonjs\n\n```grit\nengine marzano(0.1)\nlanguage js\n\nor {\n and {\n // handle a default export of an object by exporting the individual original definitions\n `module.exports = { $vals }` where {\n $new_export = \"\",\n // it's only safe to remove the overall export if every property is individually exported\n $vals <: some bubble($new_export) $prop where {\n $prop <: or {\n shorthand_property_identifier() as $name where { $value = $name },\n pair(key=$name, $value)\n },\n or {\n $program <: contains or {\n // does not handle difficult trying to match a sublist of the module.exports\n `const $name = require($val).$foo` => `export { $foo as $name } from $val`,\n `const $name = require($val)` => `export { default as $name } from $val`,\n `const $name = require($val).default` => `export { default as $name } from $val`,\n or {\n `let $name = $val`,\n `var $name = $val`,\n `const $name = $val`,\n function_declaration($name)\n } as $match => `export $match`\n },\n if ($value <: $name) {\n $new_export += `export { $name };\\n`\n } else {\n $new_export += `export const $name = $value;\\n`\n }\n }\n }\n } => `$new_export`\n },\n // handle other default exports\n `module.exports = $export` => `export default $export`,\n // Handle individually named exports\n `module.exports.$name = $export` => `export const $name = $export;\\n`\n}\n```\n\n## Transform direct exports\n\n```js\nmodule.exports.king = '9';\n```\n\n```js\nexport const king = '9';\n```\n\n## Transform default exports\n\n```js\nasync function createTeam() {\n console.log('cool');\n}\n\nconst addTeamToOrgSubscription = () => console.log('cool');\n\nmodule.exports = {\n createTeam,\n addTeamToOrgSubscription,\n};\n```\n\n```js\nexport async function createTeam() {\n console.log('cool');\n}\n\nexport const addTeamToOrgSubscription = () => console.log('cool');\n```\n\n### Keep inline values in tact\n\n```js\nconst king = '9';\n\nmodule.exports = {\n king,\n queen: '8',\n};\n```\n\n```js\nexport const king = '9';\n\nexport const queen = '8';\n```\n\n### Work on\n\n```js\nconst c1 = require('./mod1');\nconst c2 = require('./mod2');\nconst c3 = require('./mod3');\nconst myDefaultConst = require('./mod4').default;\nconst myRenamed = require('mod5').originalName;\nconst { sub1, sub2 } = require('mod5'); // not handled\n\nmodule.exports = { c1, c2, c3, myDefaultConst, myRenamed, sub1, sub2 };\n```\n\n```js\nexport { default as c1 } from './mod1';\nexport { default as c2 } from './mod2';\nexport { default as c3 } from './mod3';\nexport { default as myDefaultConst } from './mod4';\nexport { originalName as myRenamed } from 'mod5';\nconst { sub1, sub2 } = require('mod5'); // not handled\n\nexport { sub1 };\nexport { sub2 };\n```\n" @@ -217,9 +247,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/getgrit/js/.grit/patterns/patterns.grit" - position: - line: 8 - column: 9 + range: + start: + line: 8 + column: 9 + end: + line: 8 + column: 22 + startByte: 118 + endByte: 131 raw: ~ module: type: Module @@ -241,9 +277,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/getgrit/js/.grit/patterns/patterns.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 22 + startByte: 41 + endByte: 54 raw: ~ module: type: Module @@ -265,9 +307,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/getgrit/json/.grit/grit.yml" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: type: Module @@ -344,9 +392,15 @@ expression: resolved_patterns endByte: 967 output_range: ~ path: ".grit/.gritmodules/github.com/getgrit/js/.grit/patterns/no_dead_code.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 26 + column: 1 + startByte: 158 + endByte: 573 raw: format: markdown content: "---\ntitle: Remove unreachable code\n---\n\nRemove unreachable code found after `return` / `throw` / `continue` or `break` statements.\n\ntags: #good, #SE\n\n```grit\nengine marzano(0.1)\nlanguage js\n\nstatement_block($statements) where {\n $deleting = \"false\",\n $statements <: some bubble($deleting) $s where {\n if ($deleting <: \"true\") {\n $s => .\n } else {\n // we start deleting\n if ($s <: or { throw_statement() , continue_statement() , return_statement() }) {\n $deleting = \"true\",\n }\n }\n }\n}\n```\n\n## Remove code after return\n\n```javascript\nfunction f() {\n return 3;\n 1 + 1;\n}\n```\n\n```typescript\nfunction f() {\n return 3;\n}\n```\n\n## Remove code after return, multiline\n\n```javascript\nfunction f() {\n foo();\n return 3;\n 1 + 1;\n}\n```\n\n```typescript\nfunction f() {\n foo();\n return 3;\n}\n```\n\n## Don't exit a scope\n\n```javascript\nfunction f() {\n if (a) {\n return 3;\n }\n 1 + 1;\n}\n```\n" @@ -472,9 +526,15 @@ expression: resolved_patterns endByte: 1262 output_range: ~ path: ".grit/patterns/no_eq_null.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n```\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -498,9 +558,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/grit.yml" - position: - line: 9 - column: 11 + range: + start: + line: 9 + column: 11 + end: + line: 9 + column: 11 + startByte: 217 + endByte: 217 raw: ~ module: type: Module @@ -606,9 +672,15 @@ expression: resolved_patterns startByte: 4649 endByte: 4961 path: ".grit/.gritmodules/github.com/getgrit/json/.grit/patterns/strict_tsconfig.md" - position: - line: 12 - column: 1 + range: + start: + line: 12 + column: 1 + end: + line: 54 + column: 1 + startByte: 261 + endByte: 1815 raw: format: markdown content: "---\ntitle: Strict tsconfig\n---\n\n# Strict tsconfig\n\nAdds `\"strict\": true, \"allowJs\": true, \"checkJs\": false` from a tsconfig's `compilerOptions`, and then removes existing redundant options (such as `noImplicitAny`).\n\ntags: #js, #ts, #tsconfig, #strict\n\n```grit\nengine marzano(0.1)\nlanguage json\n\npair(key=`\"compilerOptions\"`, $value) where {\n $value <: object($properties),\n $newOptions = [],\n\n $properties <: maybe contains pair(key=`\"noImplicitAny\"`, value=`false` => `true`),\n $properties <: maybe contains pair(key=`\"strict\"`, value=`false` => `true`),\n if ($properties <: contains pair(key=`\"strict\"`, value=$strict)) {\n $strict <: maybe `false` => `true`\n } else {\n $newOptions += `\"strict\": true`\n },\n\n // These are all included by default; ideally we'd delete, but can't, so just mark as true\n $properties <: maybe contains pair(key=`\"noImplicitThis\"`) => .,\n $properties <: maybe contains pair(key=`\"alwaysStrict\"`) => .,\n $properties <: maybe contains pair(key=`\"strictBindCallApply\"`) => .,\n $properties <: maybe contains pair(key=`\"strictNullChecks\"`) => .,\n $properties <: maybe contains pair(key=`\"strictFunctionTypes\"`) => .,\n $properties <: maybe contains pair(key=`\"strictPropertyInitialization\"`) => .,\n\n // allow JS\n if ($properties <: contains pair(key=`\"allowJs\"`, value=$allow_js)) {\n $allow_js <: maybe `false` => `true`\n } else {\n $newOptions += `\"allowJs\": true`\n },\n\n // check JS\n if ($properties <: contains pair(key=`\"checkJs\"`, value=$check_js)) {\n $check_js <: maybe `true` => `false`\n } else {\n $newOptions += `\"checkJs\": false`\n },\n\n if (!$newOptions <: []) {\n $joined = join(list=$newOptions, separator=\", \"),\n $properties => `$joined, $properties`\n }\n}\n```\n\n## Transform standard tsconfig.json\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"outDir\": \"../../dist/out-tsc\",\n \"types\": [\"node\"],\n \"foo\": \"bar\"\n },\n \"exclude\": [\"**/*.spec.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"strict\": true,\n \"allowJs\": true,\n \"checkJs\": false,\n \"outDir\": \"../../dist/out-tsc\",\n \"types\": [\"node\"],\n \"foo\": \"bar\"\n },\n \"exclude\": [\"**/*.spec.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n\n## Handles redundant options\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"outDir\": \"../../dist/out-tsc\",\n \"types\": [\"node\"],\n \"foo\": \"bar\",\n \"baz\": \"raz\"\n },\n \"exclude\": [\"**/*.spec.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"strict\": true,\n \"allowJs\": true,\n \"checkJs\": false,\n \"outDir\": \"../../dist/out-tsc\",\n \"types\": [\"node\"],\n \"foo\": \"bar\",\n \"baz\": \"raz\"\n },\n \"exclude\": [\"**/*.spec.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n\n## Handles existing strict\n\n```json\n{\n \"compilerOptions\": {\n \"target\": \"es5\",\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"allowJs\": true,\n \"skipLibCheck\": true,\n \"strict\": true,\n \"forceConsistentCasingInFileNames\": true,\n \"noEmit\": true,\n \"esModuleInterop\": true,\n \"module\": \"esnext\",\n \"moduleResolution\": \"node\",\n \"resolveJsonModule\": true,\n \"isolatedModules\": true,\n \"jsx\": \"preserve\",\n \"incremental\": true,\n \"noImplicitAny\": false,\n \"plugins\": [\n {\n \"name\": \"next\"\n }\n ],\n \"baseUrl\": \".\",\n \"paths\": {\n \"@/*\": [\"./*\"]\n }\n },\n \"include\": [\"next-env.d.ts\", \"**/*.ts\", \"**/*.tsx\", \".next/types/**/*.ts\"],\n \"exclude\": [\"node_modules\"]\n}\n```\n\n```json\n{\n \"compilerOptions\": {\n \"checkJs\": false,\n \"target\": \"es5\",\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"allowJs\": true,\n \"skipLibCheck\": true,\n \"strict\": true,\n \"forceConsistentCasingInFileNames\": true,\n \"noEmit\": true,\n \"esModuleInterop\": true,\n \"module\": \"esnext\",\n \"moduleResolution\": \"node\",\n \"resolveJsonModule\": true,\n \"isolatedModules\": true,\n \"jsx\": \"preserve\",\n \"incremental\": true,\n \"noImplicitAny\": true,\n \"plugins\": [\n {\n \"name\": \"next\"\n }\n ],\n \"baseUrl\": \".\",\n \"paths\": {\n \"@/*\": [\"./*\"]\n }\n },\n \"include\": [\"next-env.d.ts\", \"**/*.ts\", \"**/*.tsx\", \".next/types/**/*.ts\"],\n \"exclude\": [\"node_modules\"]\n}\n```\n\n## Bugfix $decl <: false\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"outDir\": \"../../dist/out-tsc\",\n \"module\": \"commonjs\",\n \"types\": [\"node\", \"express\"],\n \"allowJs\": true\n },\n \"exclude\": [\"jest.config.ts\", \"**/*.spec.ts\", \"**/*.test.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n\n```json\n{\n \"extends\": \"./tsconfig.json\",\n \"compilerOptions\": {\n \"strict\": true,\n \"checkJs\": false,\n \"outDir\": \"../../dist/out-tsc\",\n \"module\": \"commonjs\",\n \"types\": [\"node\", \"express\"],\n \"allowJs\": true\n },\n \"exclude\": [\"jest.config.ts\", \"**/*.spec.ts\", \"**/*.test.ts\"],\n \"include\": [\"**/*.ts\"]\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_multiple_layers.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_multiple_layers.snap index 3d1e0a8df..16f5e2f24 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_multiple_layers.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_multiple_layers.snap @@ -11,9 +11,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/custodian-sample-org/testrepo-A/.grit/grit.yaml" - position: - line: 9 - column: 11 + range: + start: + line: 9 + column: 11 + end: + line: 9 + column: 11 + startByte: 203 + endByte: 203 raw: ~ module: type: Module @@ -35,9 +41,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/custodian-sample-org/testrepo-A/.grit/grit.yaml" - position: - line: 11 - column: 11 + range: + start: + line: 11 + column: 11 + end: + line: 11 + column: 11 + startByte: 274 + endByte: 274 raw: ~ module: type: Module @@ -59,9 +71,15 @@ expression: resolved_patterns tags: ~ samples: ~ path: ".grit/.gritmodules/github.com/custodian-sample-org/testrepo-B/.grit/grit.yaml" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_single_module.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_single_module.snap index 75c8260af..a1f791f10 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_single_module.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__resolve_single_module.snap @@ -13,9 +13,15 @@ NamedPattern: tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 5 - column: 9 + range: + start: + line: 5 + column: 9 + end: + line: 5 + column: 21 + startByte: 75 + endByte: 87 raw: ~ module: type: Module @@ -39,9 +45,15 @@ OtherPattern: tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 1 - column: 9 + range: + start: + line: 1 + column: 9 + end: + line: 1 + column: 21 + startByte: 8 + endByte: 20 raw: ~ module: type: Module @@ -65,9 +77,15 @@ PatternWithArgs: tags: ~ samples: ~ path: ".grit/patterns/other.grit" - position: - line: 9 - column: 9 + range: + start: + line: 9 + column: 9 + end: + line: 9 + column: 24 + startByte: 142 + endByte: 157 raw: ~ module: type: Module @@ -91,9 +109,15 @@ ReactNode: tags: ~ samples: ~ path: ".grit/patterns/react_node.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 18 + startByte: 41 + endByte: 50 raw: ~ module: type: Module @@ -219,9 +243,15 @@ no_eq_null: endByte: 1262 output_range: ~ path: ".grit/patterns/no_eq_null.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n```\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" @@ -247,9 +277,15 @@ remove_console_error: tags: ~ samples: ~ path: ".grit/grit.yml" - position: - line: 9 - column: 11 + range: + start: + line: 9 + column: 11 + end: + line: 9 + column: 11 + startByte: 221 + endByte: 221 raw: ~ module: type: Module diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__without_grit_yml.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__without_grit_yml.snap index c08315b39..d642f9161 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__without_grit_yml.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__without_grit_yml.snap @@ -13,9 +13,15 @@ ReactNode: tags: ~ samples: ~ path: ".grit/patterns/react_node.grit" - position: - line: 4 - column: 9 + range: + start: + line: 4 + column: 9 + end: + line: 4 + column: 18 + startByte: 41 + endByte: 50 raw: ~ module: type: Module @@ -141,9 +147,15 @@ no_eq_null: endByte: 1262 output_range: ~ path: ".grit/patterns/no_eq_null.md" - position: - line: 10 - column: 1 + range: + start: + line: 10 + column: 1 + end: + line: 20 + column: 1 + startByte: 198 + endByte: 648 raw: format: markdown content: "---\ntitle: Compare `null` using `===` or `!==`\n---\n\nComparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.\n\ntags: #good\n\n```grit\nengine marzano(0.1)\nlanguage js\n\n// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by \"==\" or \"!=\".\n// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.\nbinary_expression($operator, $left, $right) where {\n $operator <: or { \"==\" => `===` , \"!=\" => `!==` },\n or { $left <: `null`, $right <: `null`}\n}\n\n```\n\n```\n\n```\n\n## `$val == null` => `$val === null`\n\n```javascript\nif (val == null) {\n done();\n}\n```\n\n```typescript\nif (val === null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null`\n\n```javascript\nif (val != null) {\n done();\n}\n```\n\n```typescript\nif (val !== null) {\n done();\n}\n```\n\n## `$val != null` => `$val !== null` into `while`\n\n```javascript\nwhile (val != null) {\n did();\n}\n```\n\n```typescript\nwhile (val !== null) {\n did();\n}\n```\n\n## Do not change `$val === null`\n\n```javascript\nif (val === null) {\n done();\n}\n```\n\n## Do not change `$val !== null`\n\n```\nwhile (val !== null) {\n doSomething();\n}\n```\n" diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__yaml__tests__gets_module_patterns.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__yaml__tests__gets_module_patterns.snap index 74c35f3d4..0242dd628 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__yaml__tests__gets_module_patterns.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__yaml__tests__gets_module_patterns.snap @@ -11,9 +11,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 3 - column: 11 + range: + start: + line: 3 + column: 11 + end: + line: 3 + column: 11 + startByte: 35 + endByte: 35 raw: ~ module: host: github.com @@ -30,9 +36,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 5 - column: 11 + range: + start: + line: 5 + column: 11 + end: + line: 5 + column: 11 + startByte: 85 + endByte: 85 raw: ~ module: host: github.com @@ -49,9 +61,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 7 - column: 11 + range: + start: + line: 7 + column: 11 + end: + line: 7 + column: 11 + startByte: 151 + endByte: 151 raw: ~ module: host: github.com @@ -68,9 +86,15 @@ expression: patterns tags: ~ samples: ~ path: "" - position: - line: 9 - column: 11 + range: + start: + line: 9 + column: 11 + end: + line: 9 + column: 11 + startByte: 217 + endByte: 217 raw: ~ module: ~ local_name: remove_console_error diff --git a/crates/gritmodule/src/yaml.rs b/crates/gritmodule/src/yaml.rs index 8154ec41a..cfc6f6f37 100644 --- a/crates/gritmodule/src/yaml.rs +++ b/crates/gritmodule/src/yaml.rs @@ -1,6 +1,6 @@ use anyhow::{bail, Result}; use futures::{future::BoxFuture, FutureExt as _}; -use grit_util::Position; +use grit_util::{Position, Range}; use marzano_util::rich_path::RichFile; use std::{ collections::HashSet, @@ -72,7 +72,13 @@ pub fn get_patterns_from_yaml<'a>( for pattern in config.patterns.iter_mut() { pattern.kind = Some(DefinitionKind::Pattern); let offset = file.content.find(&pattern.name).unwrap_or(0); - pattern.position = Some(Position::from_byte_index(&file.content, offset)); + let start = Position::from_byte_index(&file.content, offset); + pattern.range = Some(Range::new( + start, + start, + offset.try_into().unwrap(), + offset.try_into().unwrap(), + )); } let patterns: Result> = config diff --git a/crates/lsp/src/server.rs b/crates/lsp/src/server.rs index e2cd2e483..d330be34c 100644 --- a/crates/lsp/src/server.rs +++ b/crates/lsp/src/server.rs @@ -478,8 +478,8 @@ impl LanguageServer for GritServer { return Ok(None); }; let url = Url::parse(definition.url(&our_repo, &root_path).as_str()).unwrap(); - let definition_position = match &definition.config.position { - Some(position) => convert_grit_position_to_lsp_position(position), + let definition_position = match &definition.config.range { + Some(range) => convert_grit_position_to_lsp_position(&range.start), None => { return Ok(None); }