0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 20:25:12 -05:00

correctly report

This commit is contained in:
Bartek Iwańczuk 2024-12-03 03:11:26 +01:00
parent 5a6c12861f
commit fb47288f90
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
3 changed files with 37 additions and 38 deletions

View file

@ -11,14 +11,28 @@ export class Context {
} }
report(data) { report(data) {
// TODO(bartlomieju): if there's `node` then convert position automatically
// otherwise lookup `location`
let start, end;
if (data.node) {
start = data.node.span.start - 1;
end = data.node.span.end - 1;
} else if (data.span) {
start = data.span.start - 1;
end = data.span.end - 1;
} else {
throw new Error(
"Either `node` or `span` must be provided when reporting an error",
);
}
op_lint_report( op_lint_report(
this.id, this.id,
this.fileName, this.fileName,
data.message, data.message,
data.startLine, start,
data.startColumn, end,
data.endLine,
data.endColumn,
); );
} }
} }
@ -61,12 +75,12 @@ function traverse(ast, visitor, parent = null) {
ast.parent = parent; ast.parent = parent;
// Call visitor if it exists for this node type // Call visitor if it exists for this node type
if (visitor[nodeType] && typeof visitor[nodeType] === "function") { if (visitor[nodeType] && typeof visitor[nodeType] === "function") {
visitor[nodeType](ast, parent); visitor[nodeType](ast);
} }
// Traverse child nodes // Traverse child nodes
for (const key in ast) { for (const key in ast) {
if (key === "parent" || key === "type" || key === "span") { if (key === "parent" || key === "type") {
continue; continue;
} }

View file

@ -7,6 +7,7 @@ use deno_ast::ModuleSpecifier;
use deno_ast::ParsedSource; use deno_ast::ParsedSource;
use deno_ast::SourceRange; use deno_ast::SourceRange;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
use deno_ast::SourceTextProvider;
use deno_core::anyhow::Context; use deno_core::anyhow::Context;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -451,21 +452,12 @@ impl LintPluginContainer {
id: String, id: String,
specifier: String, specifier: String,
message: String, message: String,
start_line: usize, start: usize,
start_column: usize, end: usize,
end_line: usize,
end_column: usize,
) { ) {
let source_text_info = self.source_text_info.as_ref().unwrap(); let source_text_info = self.source_text_info.as_ref().unwrap();
let start_range = source_text_info.loc_to_source_pos(LineAndColumnIndex { let start_pos = source_text_info.start_pos();
line_index: start_line, let source_range = SourceRange::new(start_pos + start, start_pos + end);
column_index: start_column,
});
let end_range = source_text_info.loc_to_source_pos(LineAndColumnIndex {
line_index: end_line,
column_index: end_column,
});
let source_range = SourceRange::new(start_range, end_range);
let range = LintDiagnosticRange { let range = LintDiagnosticRange {
range: source_range, range: source_range,
description: None, description: None,
@ -526,19 +518,9 @@ fn op_lint_report(
#[string] id: String, #[string] id: String,
#[string] specifier: String, #[string] specifier: String,
#[string] message: String, #[string] message: String,
#[smi] start_line: usize, #[smi] start: usize,
#[smi] start_column: usize, #[smi] end: usize,
#[smi] end_line: usize,
#[smi] end_column: usize,
) { ) {
let container = state.borrow_mut::<LintPluginContainer>(); let container = state.borrow_mut::<LintPluginContainer>();
container.report( container.report(id, specifier, message, start, end);
id,
specifier,
message,
start_line,
start_column,
end_line,
end_column,
);
} }

View file

@ -6,10 +6,10 @@ const rule = {
create(context) { create(context) {
console.log("Hello from", `${PLUGIN_NAME}/${RULE1_NAME}`); console.log("Hello from", `${PLUGIN_NAME}/${RULE1_NAME}`);
context.report({ context.report({
startLine: 0, span: {
startColumn: 6, start: 6,
endLine: 0, end: 9,
endColumn: 9, },
message: "Error from " + `${PLUGIN_NAME}/${RULE1_NAME}`, message: "Error from " + `${PLUGIN_NAME}/${RULE1_NAME}`,
data: { data: {
some: "Data", some: "Data",
@ -23,12 +23,15 @@ const rule = {
VariableDeclarator(node) { VariableDeclarator(node) {
// console.log("variable declarator", node); // console.log("variable declarator", node);
// Check if a `const` variable declaration // Check if a `const` variable declaration
console.log("node.parent.kind", node.parent.kind);
if (node.parent.kind === "const") { if (node.parent.kind === "const") {
// Check if variable name is `foo` // Check if variable name is `foo`
if (node.id.type === "Identifier" && node.id.name === "foo") { console.log("node.id.type", node.id.type, node.id.value);
if (node.id.type === "Identifier" && node.id.value === "foo") {
// Check if value of variable is "bar" // Check if value of variable is "bar"
console.log("node.init", node.init);
if ( if (
node.init && node.init.type === "Literal" && node.init && node.init.type === "StringLiteral" &&
node.init.value !== "bar" node.init.value !== "bar"
) { ) {
/* /*