Skip to content

Commit

Permalink
feat(linter): support vue generic component (#1989)
Browse files Browse the repository at this point in the history
closes #1938
  • Loading branch information
Boshen committed Jan 11, 2024
1 parent 4e7b7da commit b7ea4e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_cli/fixtures/vue/debugger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
debugger
</script>

<script setup>
<script setup lang="ts" generic="T extends Record<string, string>">
debugger
</script>
25 changes: 23 additions & 2 deletions crates/oxc_linter/src/partial_loader/vue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a> VuePartialLoader<'a> {
*pointer += offset + SCRIPT_START.len();

// find closing ">"
let offset = self.source_text[*pointer..].find('>')?;
let offset = self.find_script_closing_angle(*pointer)?;

// get ts and jsx attribute
let content = &self.source_text[*pointer..*pointer + offset];
Expand All @@ -57,6 +57,27 @@ impl<'a> VuePartialLoader<'a> {
SourceType::default().with_module(true).with_typescript(is_ts).with_jsx(is_jsx);
Some(JavaScriptSource::new(source_text, source_type, js_start))
}

/// Find closing angle for situations where there is another `>` in between.
/// e.g. `<script generic="T extends Record<string, string>">`
fn find_script_closing_angle(&self, pointer: usize) -> Option<usize> {
let mut numbers_of_open_angle = 0;
for (offset, c) in self.source_text[pointer..].char_indices() {
match c {
'>' => {
if numbers_of_open_angle == 0 {
return Some(offset);
}
numbers_of_open_angle -= 1;
}
'<' => {
numbers_of_open_angle += 1;
}
_ => {}
}
}
None
}
}

#[cfg(test)]
Expand Down Expand Up @@ -84,7 +105,7 @@ mod test {
#[test]
fn test_build_vue_with_ts_flag_1() {
let source_text = r#"
<script lang="ts" setup>
<script lang="ts" setup generic="T extends Record<string, string>">
1/1
</script>
"#;
Expand Down

0 comments on commit b7ea4e5

Please sign in to comment.