Skip to content

Commit

Permalink
feat(deno-lint): upgrade deno_lint swc
Browse files Browse the repository at this point in the history
  • Loading branch information
LongYinan committed Sep 3, 2020
1 parent 82404e6 commit 5aff9ab
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/deno-lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
deno_lint = "=0.1.26"
deno_lint = "=0.1.28"
ignore = "0.4"
napi = { version = "0.4" }
napi-derive = { version = "0.4" }
serde = "1"
serde_json = "1"
swc_ecmascript = { version = "=0.6.3", features = ["parser", "transforms", "utils", "visit"] }
swc_ecmascript = { version = "=0.7.0", features = ["parser", "transforms", "utils", "visit"] }
termcolor = "1.1"

[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
Expand Down
113 changes: 90 additions & 23 deletions packages/deno-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ fn cyan(s: String) -> impl fmt::Display {
style(&s, style_spec)
}

fn bold(s: String) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_bold(true);
style(&s, style_spec)
}

fn style(s: &str, colorspec: ColorSpec) -> impl fmt::Display {
let mut v = Vec::new();
let mut ansi_writer = Ansi::new(&mut v);
Expand All @@ -63,7 +69,7 @@ fn style(s: &str, colorspec: ColorSpec) -> impl fmt::Display {
String::from_utf8_lossy(&v).into_owned()
}

pub fn format_diagnostic(diagnostic: &LintDiagnostic) -> String {
pub fn format_diagnostic(diagnostic: &LintDiagnostic, source: &str) -> String {
let pretty_error = format!(
"({}) {}",
gray(diagnostic.code.to_string()),
Expand All @@ -78,35 +84,96 @@ pub fn format_diagnostic(diagnostic: &LintDiagnostic) -> String {
format!("./{}", file_name)
};

let line_str_len = diagnostic.location.line.to_string().len();
let line_str_len = diagnostic.range.end.line.to_string().len();
let pretty_location = cyan(format!(
"{}--> {}:{}:{}",
" ".repeat(line_str_len),
location,
diagnostic.location.line,
diagnostic.location.col
diagnostic.range.start.line,
diagnostic.range.start.col
))
.to_string();

let dummy = format!("{} |", " ".repeat(line_str_len));
let pretty_line_src = format!("{} | {}", diagnostic.location.line, diagnostic.line_src);
let red_glyphs = format!(
"{} | {}{}",
" ".repeat(line_str_len),
" ".repeat(diagnostic.location.col),
red("^".repeat(diagnostic.snippet_length))
);

let lines = vec![
pretty_error,
pretty_location,
dummy.clone(),
pretty_line_src,
red_glyphs,
dummy,
];
if diagnostic.range.start.line == diagnostic.range.end.line {
let snippet_length = diagnostic.range.end.col - diagnostic.range.start.col;
let source_lines: Vec<&str> = source.split('\n').collect();
let line = source_lines[diagnostic.range.start.line - 1];
let pretty_line_src = format!("{} | {}", diagnostic.range.start.line, line);
let red_glyphs = format!(
"{} | {}{}",
" ".repeat(line_str_len),
" ".repeat(diagnostic.range.start.col),
red("^".repeat(snippet_length))
);

let lines = vec![
pretty_error,
pretty_location,
dummy.clone(),
pretty_line_src,
red_glyphs,
dummy,
];

lines.join("\n")
} else {
let mut lines = vec![pretty_error, pretty_location, dummy.clone()];
let source_lines: Vec<&str> = source.split('\n').collect();

for i in diagnostic.range.start.line..(diagnostic.range.end.line + 1) {
let line = source_lines[i - 1];
let is_first = i == diagnostic.range.start.line;
let is_last = i == diagnostic.range.end.line;

if is_first {
let (rest, snippet) = line.split_at(diagnostic.range.start.col);
lines.push(format!("{} | {}{}", i, rest, bold(snippet.to_string())));
} else if is_last {
let (snippet, rest) = line.split_at(diagnostic.range.end.col);
lines.push(format!(
"{} | {} {}{}",
i,
red("|".to_string()),
bold(snippet.to_string()),
rest
));
} else {
lines.push(format!(
"{} | {} {}",
i,
red("|".to_string()),
bold(line.to_string())
));
}

lines.join("\n")
// If this is the first line, render the ∨ symbols
if is_first {
lines.push(format!(
"{} | {}{}",
" ".repeat(line_str_len),
red("_".repeat(diagnostic.range.start.col + 1)),
red("^".to_string())
));
}

// If this is the last line, render the ∨ symbols
if is_last {
lines.push(format!(
"{} | {}{}{}",
" ".repeat(line_str_len),
red("|".to_string()),
red("_".repeat(diagnostic.range.end.col)),
red("^".to_string())
));
}
}

lines.push(dummy);

lines.join("\n")
}
}

fn init(js_module: &mut Module) -> Result<()> {
Expand Down Expand Up @@ -151,7 +218,7 @@ fn lint(ctx: CallContext) -> Result<JsObject> {
ctx.env.create_int32(index as i32)?,
ctx
.env
.create_string(format_diagnostic(diagnostic).as_str())?,
.create_string(format_diagnostic(diagnostic, source_string).as_str())?,
)?;
}

Expand Down Expand Up @@ -248,15 +315,15 @@ fn lint_command(ctx: CallContext) -> Result<JsBoolean> {
&p
)))?
.to_owned(),
file_content,
file_content.clone(),
)
.map_err(|e| Error {
status: Status::GenericFailure,
reason: format!("Lint failed: {}, at: {:?}", e, &p),
})?;
for diagnostic in file_diagnostics {
has_error = true;
println!("{}", format_diagnostic(&diagnostic));
println!("{}", format_diagnostic(&diagnostic, file_content.as_str()));
}
}
}
Expand Down

0 comments on commit 5aff9ab

Please sign in to comment.