mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
correctly report
This commit is contained in:
parent
5a6c12861f
commit
fb47288f90
3 changed files with 37 additions and 38 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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::<LintPluginContainer>();
|
||||
container.report(
|
||||
id,
|
||||
specifier,
|
||||
message,
|
||||
start_line,
|
||||
start_column,
|
||||
end_line,
|
||||
end_column,
|
||||
);
|
||||
container.report(id, specifier, message, start, end);
|
||||
}
|
||||
|
|
15
plugin.js
15
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"
|
||||
) {
|
||||
/*
|
||||
|
|
Loading…
Add table
Reference in a new issue