Skip to content

Commit

Permalink
fix(dlint): avoid panic when no trailing new line is appended (#1149)
Browse files Browse the repository at this point in the history
This commit prevents dlint from panicking when the input file doesn't contain a final newline character.

Towards #1145
  • Loading branch information
magurotuna authored Apr 12, 2023
1 parent 3454c10 commit cffdc91
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/dlint/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ impl miette::SourceCode for MietteSourceCode<'_> {
end_line_column.line_index + context_lines_after,
self.source.text_str().len(),
);
let src_end = self.source.line_end(end_line_index);
let src_end = self
.source
.line_end(std::cmp::min(end_line_index, line_count - 1));
let range = SourceRange::new(src_start, src_end);
let src_text = range.text_fast(&self.source);
let byte_range = range.as_byte_range(start_pos);
Expand Down
6 changes: 6 additions & 0 deletions examples/dlint/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,10 @@ mod tests {
output: "simple.out",
exit_code: 1,
});

itest!(issue1145_no_trailing_newline {
args: "run issue1145_no_trailing_newline.ts",
output: "issue1145_no_trailing_newline.out",
exit_code: 1,
});
}
22 changes: 22 additions & 0 deletions examples/dlint/testdata/issue1145_no_trailing_newline.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
no-unused-vars

x `base` is never used
,-[issue1145_no_trailing_newline.ts:3:5]
3 | ---*/
4 | var base
: ^^|^
: `-- If this is intentional, prefix it with an underscore like `_base`
`----
help: https://lint.deno.land/#no-unused-vars

no-var

x `var` keyword is not allowed.
,-[issue1145_no_trailing_newline.ts:3:1]
3 | ---*/
4 | var base
: ^^^^^^^^
`----
help: https://lint.deno.land/#no-var

Found 2 problems
3 changes: 3 additions & 0 deletions examples/dlint/testdata/issue1145_no_trailing_newline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*---
---*/
var base

0 comments on commit cffdc91

Please sign in to comment.