Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/getgrit/gritql into main-wa…
Browse files Browse the repository at this point in the history
…tchMode
  • Loading branch information
Ashu999 committed Jul 6, 2024
2 parents 5895757 + 632cea0 commit 43dc81e
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 78 deletions.
33 changes: 9 additions & 24 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ jobs:
test-rust:
name: Rust tests (marzano)
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
os: [nscloud-ubuntu-22.04-amd64-8x32]
runs-on: ${{ matrix.os }}
runs-on: namespace-profile-standard-ubuntu22-amd64
permissions:
contents: "read"
id-token: "write"
Expand All @@ -25,10 +21,8 @@ jobs:
BUILD_PLATFORM: amd64
steps:
- name: clone code
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
uses: namespacelabs/nscloud-checkout-action@v2
- run: nsc git-checkout update-submodules --mirror_base_path=${NSC_GIT_MIRROR} --repository_path=${GITHUB_WORKSPACE} --recurse
- name: Install Protoc
run: sudo apt-get install -y protobuf-compiler
- name: install Rust
Expand Down Expand Up @@ -74,19 +68,14 @@ jobs:
test-rust-wasm:
name: Rust wasm
timeout-minutes: 15
strategy:
fail-fast: false
runs-on:
- nscloud-ubuntu-22.04-amd64-8x32
runs-on: namespace-profile-standard-ubuntu22-amd64
permissions:
contents: "read"
id-token: "write"
steps:
- name: clone code
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
uses: namespacelabs/nscloud-checkout-action@v2
- run: nsc git-checkout update-submodules --mirror_base_path=${NSC_GIT_MIRROR} --repository_path=${GITHUB_WORKSPACE} --recurse
- name: install Rust
uses: actions-rs/toolchain@v1
with:
Expand All @@ -102,18 +91,14 @@ jobs:
test-stdlib:
name: Test the standard library
timeout-minutes: 30
strategy:
fail-fast: false
runs-on:
- nscloud-ubuntu-22.04-amd64-4x16
permissions:
contents: "read"
id-token: "write"
runs-on: namespace-profile-standard-ubuntu22-amd64
steps:
- name: clone code
uses: actions/checkout@v3
with:
submodules: true
uses: namespacelabs/nscloud-checkout-action@v2
- run: nsc git-checkout update-submodules --mirror_base_path=${NSC_GIT_MIRROR} --repository_path=${GITHUB_WORKSPACE} --recurse
- name: install Rust
uses: actions-rs/toolchain@v1
with:
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ pub(crate) async fn run_check(
let reason = Some(MatchReason {
metadata_json: None,
source: RewriteSource::Gritql,
title: result.pattern.title().map(|s| s.to_string()),
name: Some(result.pattern.local_name.to_string()),
level: Some(result.pattern.level()),
explanation: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/patterns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ async fn enable_watch_mode(

let testable_patterns_map = testable_patterns
.iter()
.map(|p| (p.local_name.as_ref().unwrap(), p.as_ref()))
.map(|p| (p.local_name.as_ref().unwrap(), p))
.collect::<HashMap<_, _>>();

// event processing
Expand Down
40 changes: 30 additions & 10 deletions crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt;
use log::{debug, error, info, warn};
use marzano_core::api::{
AllDone, AnalysisLog, AnalysisLogLevel, CreateFile, DoneFile, FileMatchResult, InputFile,
Match, MatchResult, PatternInfo, RemoveFile, Rewrite,
Match, MatchReason, MatchResult, PatternInfo, RemoveFile, Rewrite,
};
use marzano_core::constants::DEFAULT_FILE_NAME;
use marzano_messenger::output_mode::OutputMode;
Expand Down Expand Up @@ -137,6 +137,28 @@ pub fn get_human_error(mut log: AnalysisLog, input_pattern: &str) -> String {
result
}

/// Print a header for a match, with the path and (maybe) a title/explanation
pub fn print_file_header(
path: &str,
reason: &Option<MatchReason>,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
let path_title = path.bold();
if let Some(r) = reason {
if let Some(title) = &r.title {
writeln!(f, "{}: {}", path_title, title)?;
} else {
writeln!(f, "{}", path_title)?;
}
if let Some(explanation) = &r.explanation {
writeln!(f, " {}", explanation.italic())?;
}
} else {
writeln!(f, "{}", path_title)?;
}
Ok(())
}

impl fmt::Display for FormattedResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -165,12 +187,12 @@ impl fmt::Display for FormattedResult {
Ok(())
}
FormattedResult::Match(m) => {
let path_title = m.file_name().bold();
writeln!(f, "{}", path_title)?;
print_file_header(m.file_name(), &m.reason, f)?;

let source = m.content();
match source {
Err(e) => {
writeln!(f, "Could not read file: {}", e)?;
writeln!(f, "Could not read flie: {}", e)?;
return Ok(());
}
Ok(source) => {
Expand Down Expand Up @@ -232,24 +254,22 @@ impl fmt::Display for FormattedResult {
item.original.source_file, item.rewritten.source_file
)
};
let path_title = path_name.bold();
writeln!(f, "{}", path_title)?;
print_file_header(&path_name, &item.reason, f)?;

let result: MatchResult = item.clone().into();
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Ok(())
}
FormattedResult::CreateFile(item) => {
let path_title = item.file_name().bold();
print_file_header(item.file_name(), &item.reason, f)?;
let result: MatchResult = item.clone().into();
writeln!(f, "{}", path_title)?;
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Ok(())
}
FormattedResult::RemoveFile(item) => {
let path_title = item.file_name().bold();
writeln!(f, "{}", path_title)?;
print_file_header(item.file_name(), &item.reason, f)?;
let result: MatchResult = item.clone().into();
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/cli_bin/fixtures/.grit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.gritmodules*
*.log
26 changes: 13 additions & 13 deletions crates/core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,12 @@ impl MatchResult {
/// Make a path look the way provolone expects it to
/// Removes leading "./", or the root path if it's provided
fn normalize_path_in_project<'a>(path: &'a str, root_path: Option<&'a PathBuf>) -> &'a str {
#[cfg(debug_assertions)]
if let Some(root_path) = root_path {
if !root_path.to_str().unwrap_or_default().ends_with('/') {
panic!(
"root_path '{}' must end with a slash.",
root_path.to_str().unwrap_or_default()
);
}
}
if let Some(root_path) = root_path {
let root_path = root_path.to_str().unwrap();
path.strip_prefix(root_path).unwrap_or(path)
let basic = path
.strip_prefix(root_path.to_string_lossy().as_ref())
.unwrap_or(path);
// Stip the leading / if it's there
basic.strip_prefix('/').unwrap_or(basic)
} else {
path.strip_prefix("./").unwrap_or(path)
}
Expand Down Expand Up @@ -506,7 +500,11 @@ impl FileMatchResult for Rewrite {
pub struct MatchReason {
pub metadata_json: Option<String>,
pub source: RewriteSource,
/// The name of the pattern that matched, or another programmatic identifier
pub name: Option<String>,
/// A human-readable title for the match
pub title: Option<String>,
/// A human-readable explanation of the match
pub explanation: Option<String>,
pub level: Option<EnforcementLevel>,
}
Expand Down Expand Up @@ -795,11 +793,13 @@ mod tests {
}

#[test]
#[should_panic]
fn test_normalize_path_in_project_with_root_no_slash() {
let path = "/home/user/project/src/main.rs";
let root_path = PathBuf::from("/home/user/project");
normalize_path_in_project(path, Some(&root_path));
assert_eq!(
normalize_path_in_project(path, Some(&root_path)),
"src/main.rs",
);
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions crates/core/src/pattern_compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,9 @@ pub fn get_dependents_of_target_patterns_by_traversal_from_src(

let name_to_filename: BTreeMap<&String, &String> = pattern_file
.iter()
.map(|(k, v)| (k, (v)))
.chain(predicate_file.iter().map(|(k, v)| (k, (v))))
.chain(function_file.iter().map(|(k, v)| (k, (v))))
.chain(foreign_file.iter().map(|(k, v)| (k, (v))))
.chain(predicate_file.iter())
.chain(function_file.iter())
.chain(foreign_file.iter())
.collect();

let mut traversed_stack = <Vec<String>>::new();
Expand Down
11 changes: 11 additions & 0 deletions crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 0.0.1
patterns:
- file: ../docs/guides/version_5_upgrade.md
- file: ../docs/guides/something.md
- name: remove_console_error
level: error
body: |
engine marzano(0.1)
language js
`console.error($_)` => .
82 changes: 82 additions & 0 deletions crates/gritmodule/fixtures/pattern_files/docs/guides/something.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: Compare `null` using `===` or `!==`
---

Comparing to `null` needs a type-checking operator (=== or !==), to avoid incorrect results when the value is `undefined`.

tags: #good

```grit
engine marzano(0.1)
language js
// We use the syntax-tree node binary_expression to capture all expressions where $a and $b are operated on by "==" or "!=".
// This code takes advantage of Grit's allowing us to nest rewrites inside match conditions and to match syntax-tree fields on patterns.
binary_expression($operator, $left, $right) where {
$operator <: or { "==" => `===` , "!=" => `!==` },
or { $left <: `null`, $right <: `null`}
}
```

```
```

## `$val == null` => `$val === null`

```javascript
if (val == null) {
done();
}
```

```typescript
if (val === null) {
done();
}
```

## `$val != null` => `$val !== null`

```javascript
if (val != null) {
done();
}
```

```typescript
if (val !== null) {
done();
}
```

## `$val != null` => `$val !== null` into `while`

```javascript
while (val != null) {
did();
}
```

```typescript
while (val !== null) {
did();
}
```

## Do not change `$val === null`

```javascript
if (val === null) {
done();
}
```

## Do not change `$val !== null`

```
while (val !== null) {
doSomething();
}
```
Loading

0 comments on commit 43dc81e

Please sign in to comment.