Skip to content

Commit

Permalink
Merge branch 'getgrit:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
digital-phoenix committed Mar 29, 2024
2 parents a799386 + dd171cb commit c41c1f3
Show file tree
Hide file tree
Showing 37 changed files with 2,520 additions and 1,547 deletions.
3 changes: 3 additions & 0 deletions crates/cli_bin/fixtures/css_in_vue/pattern.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language css

`display: none;` => `display: some;`
44 changes: 44 additions & 0 deletions crates/cli_bin/fixtures/css_in_vue/simple.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<script lang="ts">
export default {
inheritAttrs: false,
};
</script>

<script setup lang="ts">
defineProps<{
itemCount?: number;
showingCount?: boolean;
}>();
</script>

<template>
<transition name="fade">
<span v-if="itemCount" class="item-count">
{{ showingCount }}
</span>
</transition>
</template>

<style lang="scss" scoped>
.item-count {
position: relative;
display: none;
margin: 0 8px;
color: var(--theme--foreground-subdued);
white-space: nowrap;
@media (min-width: 600px) {
display: inline;
}
}
.fade-enter-active,
.fade-leave-active {
transition: opacity var(--medium) var(--transition);
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>
68 changes: 63 additions & 5 deletions crates/cli_bin/tests/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,39 @@ fn basic_js_in_vue_apply() -> Result<()> {
Ok(())
}

#[test]
fn basic_css_in_vue_apply() -> Result<()> {
// Keep _temp_dir around so that the tempdir is not deleted
let (_temp_dir, dir) = get_fixture("css_in_vue", false)?;

// from the tempdir as cwd, run init
run_init(&dir.as_path())?;

// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
// apply_cmd.current_dir(basic_path);
apply_cmd.arg("apply").arg("--force").arg("pattern.grit");
let output = apply_cmd.output()?;

// Assert that the command executed successfully
println!("OUTPUT: {:#?}", output);
assert!(
output.status.success(),
"Command didn't finish successfully: {}",
String::from_utf8(output.stderr)?
);

// Read back the require.js file
let target_file = dir.join("simple.vue");
let content: String = std::fs::read_to_string(target_file)?;

// assert that it matches snapshot
assert_snapshot!(content);

Ok(())
}

#[test]
fn invalid_md_file_parse_errors() -> Result<()> {
let tempdir = tempfile::tempdir()?;
Expand Down Expand Up @@ -2028,7 +2061,12 @@ fn language_option_file_pattern_apply() -> Result<()> {
// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
apply_cmd.arg("apply").arg("--force").arg("pattern.grit").arg("--language").arg("java");
apply_cmd
.arg("apply")
.arg("--force")
.arg("pattern.grit")
.arg("--language")
.arg("java");
let output = apply_cmd.output()?;

// Assert that the command failed
Expand Down Expand Up @@ -2059,7 +2097,12 @@ fn language_option_inline_pattern_apply() -> Result<()> {
// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
apply_cmd.arg("apply").arg(pattern).arg("--force").arg("--lang").arg("python");
apply_cmd
.arg("apply")
.arg(pattern)
.arg("--force")
.arg("--lang")
.arg("python");
let output = apply_cmd.output()?;

// Assert that the command executed successfully
Expand Down Expand Up @@ -2095,7 +2138,12 @@ fn language_option_named_pattern_apply() -> Result<()> {
// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
apply_cmd.arg("apply").arg(pattern).arg("--force").arg("--lang").arg("python");
apply_cmd
.arg("apply")
.arg(pattern)
.arg("--force")
.arg("--lang")
.arg("python");
let output = apply_cmd.output()?;

// Assert that the command executed successfully
Expand Down Expand Up @@ -2130,7 +2178,12 @@ fn language_option_conflict_apply() -> Result<()> {
// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
apply_cmd.arg("apply").arg(pattern).arg("--force").arg("--language").arg("python");
apply_cmd
.arg("apply")
.arg(pattern)
.arg("--force")
.arg("--language")
.arg("python");
let output = apply_cmd.output()?;

// Assert that the command failed
Expand Down Expand Up @@ -2162,7 +2215,12 @@ fn invalid_language_option_apply() -> Result<()> {
// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
apply_cmd.arg("apply").arg(pattern).arg("--force").arg("--lang").arg("__invalid");
apply_cmd
.arg("apply")
.arg(pattern)
.arg("--force")
.arg("--lang")
.arg("__invalid");
let output = apply_cmd.output()?;

// Assert that the command failed
Expand Down
48 changes: 48 additions & 0 deletions crates/cli_bin/tests/snapshots/apply__basic_css_in_vue_apply.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: crates/cli_bin/tests/apply.rs
expression: content
---
<script lang="ts">
export default {
inheritAttrs: false,
};
</script>

<script setup lang="ts">
defineProps<{
itemCount?: number;
showingCount?: boolean;
}>();
</script>

<template>
<transition name="fade">
<span v-if="itemCount" class="item-count">
{{ showingCount }}
</span>
</transition>
</template>

<style lang="scss" scoped>
.item-count {
position: relative;
display: some;
margin: 0 8px;
color: var(--theme--foreground-subdued);
white-space: nowrap;

@media (min-width: 600px) {
display: inline;
}
}

.fade-enter-active,
.fade-leave-active {
transition: opacity var(--medium) var(--transition);
}

.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>
18 changes: 12 additions & 6 deletions crates/core/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,22 @@ impl FileOwner {
new: bool,
language: &impl Language,
logs: &mut AnalysisLogs,
) -> Result<Self> {
) -> Result<Option<Self>> {
let name = name.into();
let tree = language.parse_file(name.to_string_lossy().as_ref(), &source, logs, new)?;
let Some(tree) =
language.parse_file(name.to_string_lossy().as_ref(), &source, logs, new)?
else {
return Ok(None);
};
let absolute_path = PathBuf::from(absolutize(name.to_string_lossy().as_ref())?);
Ok(FileOwner {
Ok(Some(FileOwner {
name,
absolute_path,
tree,
source,
matches: matches.unwrap_or_default().into(),
new,
})
}))
}
}

Expand Down Expand Up @@ -404,8 +408,10 @@ impl Problem {
);
match owned_file {
Result::Ok(owned_file) => {
file_pointers.push(FilePtr::new(file_pointers.len() as u16, 0));
owned_files.push(owned_file);
if let Some(owned_file) = owned_file {
file_pointers.push(FilePtr::new(file_pointers.len() as u16, 0));
owned_files.push(owned_file);
}
done_files.push(MatchResult::DoneFile(DoneFile {
relative_file_path: file.path.to_string(),
has_results: None,
Expand Down
20 changes: 16 additions & 4 deletions crates/core/src/pattern/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
pattern::{InputRanges, MatchRanges},
text_unparser::apply_effects,
};
use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use im::vector;
use marzano_language::language::Language;
use marzano_util::analysis_logs::{AnalysisLogBuilder, AnalysisLogs};
Expand Down Expand Up @@ -275,13 +275,19 @@ impl Matcher for Step {
let ranges =
MatchRanges::new(new_ranges.into_iter().map(|r| r.into()).collect());
let owned_file = FileOwner::new(
new_filename,
new_filename.clone(),
new_src,
Some(ranges),
true,
context.language(),
logs,
)?;
)?
.ok_or_else(|| {
anyhow!(
"failed to construct new file for file {}",
new_filename.to_string_lossy()
)
})?;
context.files().push(owned_file);
state
.files
Expand All @@ -303,7 +309,13 @@ impl Matcher for Step {
.into();
let body = file.body(&state.files).text(&state.files).unwrap().into();
let owned_file =
FileOwner::new(name, body, None, true, context.language(), logs)?;
FileOwner::new(name.clone(), body, None, true, context.language(), logs)?
.ok_or_else(|| {
anyhow!(
"failed to construct new file for file {}",
name.to_string_lossy()
)
})?;
context.files().push(owned_file);
let _ = state.files.push_new_file(context.files().last().unwrap());
} else {
Expand Down
Loading

0 comments on commit c41c1f3

Please sign in to comment.