From fb47288f90ef4877df1fc737b28f5ca5b365cc46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 3 Dec 2024 03:11:26 +0100 Subject: [PATCH] correctly report --- cli/tools/lint/lint.js | 26 ++++++++++++++++++++------ cli/tools/lint/plugins.rs | 34 ++++++++-------------------------- plugin.js | 15 +++++++++------ 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/cli/tools/lint/lint.js b/cli/tools/lint/lint.js index de6f06fa66..f86f64a814 100644 --- a/cli/tools/lint/lint.js +++ b/cli/tools/lint/lint.js @@ -11,14 +11,28 @@ export class Context { } 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( this.id, this.fileName, data.message, - data.startLine, - data.startColumn, - data.endLine, - data.endColumn, + start, + end, ); } } @@ -61,12 +75,12 @@ function traverse(ast, visitor, parent = null) { ast.parent = parent; // Call visitor if it exists for this node type if (visitor[nodeType] && typeof visitor[nodeType] === "function") { - visitor[nodeType](ast, parent); + visitor[nodeType](ast); } // Traverse child nodes for (const key in ast) { - if (key === "parent" || key === "type" || key === "span") { + if (key === "parent" || key === "type") { continue; } diff --git a/cli/tools/lint/plugins.rs b/cli/tools/lint/plugins.rs index 87bab70528..e2d1bc931e 100644 --- a/cli/tools/lint/plugins.rs +++ b/cli/tools/lint/plugins.rs @@ -7,6 +7,7 @@ use deno_ast::ModuleSpecifier; use deno_ast::ParsedSource; use deno_ast::SourceRange; use deno_ast::SourceTextInfo; +use deno_ast::SourceTextProvider; use deno_core::anyhow::Context; use deno_core::error::custom_error; use deno_core::error::AnyError; @@ -451,21 +452,12 @@ impl LintPluginContainer { id: String, specifier: String, message: String, - start_line: usize, - start_column: usize, - end_line: usize, - end_column: usize, + start: usize, + end: usize, ) { let source_text_info = self.source_text_info.as_ref().unwrap(); - let start_range = source_text_info.loc_to_source_pos(LineAndColumnIndex { - line_index: start_line, - 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 start_pos = source_text_info.start_pos(); + let source_range = SourceRange::new(start_pos + start, start_pos + end); let range = LintDiagnosticRange { range: source_range, description: None, @@ -526,19 +518,9 @@ fn op_lint_report( #[string] id: String, #[string] specifier: String, #[string] message: String, - #[smi] start_line: usize, - #[smi] start_column: usize, - #[smi] end_line: usize, - #[smi] end_column: usize, + #[smi] start: usize, + #[smi] end: usize, ) { let container = state.borrow_mut::(); - container.report( - id, - specifier, - message, - start_line, - start_column, - end_line, - end_column, - ); + container.report(id, specifier, message, start, end); } diff --git a/plugin.js b/plugin.js index bf067b752e..283a969cc4 100644 --- a/plugin.js +++ b/plugin.js @@ -6,10 +6,10 @@ const rule = { create(context) { console.log("Hello from", `${PLUGIN_NAME}/${RULE1_NAME}`); context.report({ - startLine: 0, - startColumn: 6, - endLine: 0, - endColumn: 9, + span: { + start: 6, + end: 9, + }, message: "Error from " + `${PLUGIN_NAME}/${RULE1_NAME}`, data: { some: "Data", @@ -23,12 +23,15 @@ const rule = { VariableDeclarator(node) { // console.log("variable declarator", node); // Check if a `const` variable declaration + console.log("node.parent.kind", node.parent.kind); if (node.parent.kind === "const") { // 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" + console.log("node.init", node.init); if ( - node.init && node.init.type === "Literal" && + node.init && node.init.type === "StringLiteral" && node.init.value !== "bar" ) { /*