Skip to content

Commit

Permalink
fix: include the byte index in Location (denoland#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Jan 18, 2024
1 parent d769aba commit 449bbca
Show file tree
Hide file tree
Showing 138 changed files with 868 additions and 430 deletions.
3 changes: 2 additions & 1 deletion src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ impl DiagnosticsCollector {
.unwrap_or_else(|| Location {
filename: referenced_module.specifier().to_string(),
line: 1,
col: 1,
col: 0,
byte_index: 0,
}),
},
})
Expand Down
4 changes: 4 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ pub enum DocNodeKind {
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct Location {
pub filename: String,
/// The 1-indexed display line.
/// todo(#150): why is one of these 0-indexed and the other 1-indexed?
pub line: usize,
/// The 0-indexed display column.
pub col: usize,
/// The 0-indexed byte offset in the source text.
pub byte_index: usize,
}

impl Ord for Location {
Expand Down Expand Up @@ -152,6 +155,7 @@ impl Default for DocNode {
filename: "".to_string(),
line: 0,
col: 0,
byte_index: 0,
},
js_doc: JsDoc::default(),
function_def: None,
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ fn parse_json_module_doc_node(
filename: specifier.to_string(),
col: 0,
line: 1,
byte_index: 0,
},
declaration_kind: DeclarationKind::Export,
variable_def: Some(VariableDef {
Expand Down
53 changes: 36 additions & 17 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ async fn types_header_handling() {
"location": {
"filename": "https://example.com/a.d.ts",
"line": 1,
"col": 13
"col": 13,
"byteIndex": 13
},
"declarationKind": "export",
"variableDef": {
Expand Down Expand Up @@ -212,7 +213,8 @@ export function fooFn(a: number) {
"location": {
"filename": "file:///reexport.ts",
"line": 7,
"col": 13
"col": 13,
"byteIndex": 86
},
"declarationKind": "export",
"jsDoc": {
Expand All @@ -236,7 +238,8 @@ export function fooFn(a: number) {
"location": {
"filename": "file:///nested_reexport.ts",
"line": 5,
"col": 13
"col": 13,
"byteIndex": 41
},
"declarationKind": "export",
"jsDoc": {
Expand All @@ -260,7 +263,8 @@ export function fooFn(a: number) {
"location": {
"filename": "file:///test.ts",
"line": 6,
"col": 0
"col": 0,
"byteIndex": 152
},
"declarationKind": "export",
"jsDoc": {
Expand Down Expand Up @@ -292,7 +296,8 @@ export function fooFn(a: number) {
"location": {
"filename": "file:///test.ts",
"line": 3,
"col": 0
"col": 0,
"byteIndex": 79
},
"declarationKind": "private",
"importDef": {
Expand Down Expand Up @@ -340,7 +345,8 @@ export { Hello } from "./reexport.ts";
"location": {
"filename": "file:///reexport.ts",
"line": 2,
"col": 0
"col": 0,
"byteIndex": 1
},
"declarationKind": "export",
"interfaceDef": {
Expand All @@ -358,7 +364,8 @@ export { Hello } from "./reexport.ts";
"location": {
"filename": "file:///reexport.ts",
"line": 3,
"col": 0
"col": 0,
"byteIndex": 27
},
"declarationKind": "export",
"classDef": {
Expand Down Expand Up @@ -409,7 +416,8 @@ async fn deep_reexports() {
"location": {
"filename": "file:///foo.ts",
"line": 1,
"col": 13
"col": 13,
"byteIndex": 13
},
"declarationKind": "export",
"variableDef": {
Expand Down Expand Up @@ -466,7 +474,8 @@ export * as b from "./mod_doc.ts";
"location": {
"filename": "file:///ns.ts",
"line": 2,
"col": 7
"col": 7,
"byteIndex": 8
},
"declarationKind": "export",
"jsDoc": {
Expand All @@ -485,7 +494,8 @@ export * as b from "./mod_doc.ts";
"location": {
"filename": "file:///mod_doc.ts",
"line": 9,
"col": 13
"col": 13,
"byteIndex": 83
},
"declarationKind": "export",
"jsDoc": {
Expand Down Expand Up @@ -643,7 +653,8 @@ async fn exports_imported_earlier() {
"location": {
"filename": "file:///foo.ts",
"line": 1,
"col": 13
"col": 13,
"byteIndex": 13
},
"declarationKind": "export",
"variableDef": {
Expand All @@ -662,6 +673,7 @@ async fn exports_imported_earlier() {
"filename": "file:///test.ts",
"line": 2,
"col": 2,
"byteIndex": 3
},
"declarationKind": "private",
"importDef": {
Expand Down Expand Up @@ -703,7 +715,8 @@ async fn exports_imported_earlier_renamed() {
"location": {
"filename": "file:///foo.ts",
"line": 1,
"col": 13
"col": 13,
"byteIndex": 13
},
"declarationKind": "export",
"variableDef": {
Expand All @@ -721,7 +734,8 @@ async fn exports_imported_earlier_renamed() {
"location": {
"filename": "file:///test.ts",
"line": 2,
"col": 2
"col": 2,
"byteIndex": 3
},
"declarationKind": "private",
"importDef": {
Expand Down Expand Up @@ -764,7 +778,8 @@ async fn exports_imported_earlier_default() {
"location": {
"filename": "file:///foo.ts",
"line": 1,
"col": 6
"col": 6,
"byteIndex": 6
},
"declarationKind": "export",
"variableDef": {
Expand All @@ -782,7 +797,8 @@ async fn exports_imported_earlier_default() {
"location": {
"filename": "file:///test.ts",
"line": 2,
"col": 2
"col": 2,
"byteIndex": 3
},
"declarationKind": "private",
"importDef": {
Expand Down Expand Up @@ -831,7 +847,8 @@ async fn exports_imported_earlier_private() {
"location": {
"filename": "file:///foo.ts",
"line": 1,
"col": 13
"col": 13,
"byteIndex": 13
},
"declarationKind": "export",
"variableDef": {
Expand All @@ -850,6 +867,7 @@ async fn exports_imported_earlier_private() {
"filename": "file:///test.ts",
"line": 2,
"col": 2,
"byteIndex": 3
},
"declarationKind": "private",
"importDef": {
Expand Down Expand Up @@ -903,7 +921,8 @@ async fn json_module() {
"location": {
"filename": "file:///bar.json",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0,
},
"declarationKind": "export",
"variableDef": {
Expand Down
2 changes: 2 additions & 0 deletions src/util/swc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ pub fn get_text_info_location(
// todo(#150): for some reason we're using a display indent width of 4
let line_and_column_index =
text_info.line_and_column_display_with_indent_width(pos, 4);
let byte_index = pos.as_byte_index(text_info.range().start);
Location {
filename: specifier.to_string(),
// todo(#150): make 0-indexed
line: line_and_column_index.line_number,
col: line_and_column_index.column_number - 1,
byte_index,
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/html_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ async fn symbol_search() {
.join("symbol_search.json");

// uncomment to regenerate symbol_search.json
//std::fs::write(&symbol_search_json_path, &file_json);
// std::fs::write(&symbol_search_json_path, &file_json);

let symbol_search_json = read_to_string(symbol_search_json_path).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion tests/specs/AbstractClass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ abstract class Class
"location": {
"filename": "file:///mod.ts",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0
},
"declarationKind": "export",
"classDef": {
Expand Down
6 changes: 4 additions & 2 deletions tests/specs/AbstractClassAbstractMethod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ abstract class Class
"location": {
"filename": "file:///mod.ts",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0
},
"declarationKind": "export",
"classDef": {
Expand Down Expand Up @@ -56,7 +57,8 @@ abstract class Class
"location": {
"filename": "file:///mod.ts",
"line": 2,
"col": 2
"col": 2,
"byteIndex": 32
}
}
],
Expand Down
6 changes: 4 additions & 2 deletions tests/specs/ClassAsyncMethod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Class
"location": {
"filename": "file:///mod.ts",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0
},
"declarationKind": "export",
"classDef": {
Expand Down Expand Up @@ -72,7 +73,8 @@ class Class
"location": {
"filename": "file:///mod.ts",
"line": 2,
"col": 2
"col": 2,
"byteIndex": 23
}
}
],
Expand Down
18 changes: 12 additions & 6 deletions tests/specs/ClassConstructor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0
},
"declarationKind": "export",
"classDef": {
Expand Down Expand Up @@ -84,7 +85,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 2,
"col": 2
"col": 2,
"byteIndex": 23
}
}
],
Expand All @@ -103,7 +105,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 8,
"col": 0
"col": 0,
"byteIndex": 124
},
"declarationKind": "export",
"jsDoc": {
Expand All @@ -120,7 +123,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 9,
"col": 2
"col": 2,
"byteIndex": 148
}
}
],
Expand All @@ -139,7 +143,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 15,
"col": 0
"col": 0,
"byteIndex": 238
},
"declarationKind": "export",
"jsDoc": {
Expand Down Expand Up @@ -167,7 +172,8 @@ class Class3
"location": {
"filename": "file:///mod.ts",
"line": 16,
"col": 2
"col": 2,
"byteIndex": 262
}
}
],
Expand Down
3 changes: 2 additions & 1 deletion tests/specs/ClassDeclaration.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Class
"location": {
"filename": "file:///mod.ts",
"line": 1,
"col": 0
"col": 0,
"byteIndex": 0
},
"declarationKind": "export",
"classDef": {
Expand Down
Loading

0 comments on commit 449bbca

Please sign in to comment.