Skip to content

Commit e36bf44

Browse files
committed
Add filepath to errors
1 parent f075ab7 commit e36bf44

File tree

126 files changed

+522
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+522
-67
lines changed

compiler-core/src/javascript.rs

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ pub enum JavaScriptCodegenTarget {
4343
pub struct Generator<'a> {
4444
line_numbers: &'a LineNumbers,
4545
module: &'a TypedModule,
46-
project_root: &'a Utf8Path,
4746
tracker: UsageTracker,
4847
module_scope: im::HashMap<EcoString, usize>,
4948
current_module_name_segments_count: usize,
5049
target_support: TargetSupport,
5150
typescript: TypeScriptDeclarations,
5251
stdlib_package: StdlibPackage,
52+
/// Relative path to the module, surrounded in `"`s to make it a string, and with `\`s escaped
53+
/// to `\\`.
54+
src_path: EcoString,
5355
}
5456

5557
impl<'a> Generator<'a> {
@@ -66,11 +68,18 @@ impl<'a> Generator<'a> {
6668
} = config;
6769
let current_module_name_segments_count = module.name.split('/').count();
6870

71+
let src_path = &module.type_info.src_path;
72+
let src_path = src_path
73+
.strip_prefix(project_root)
74+
.unwrap_or(src_path)
75+
.as_str();
76+
let src_path = eco_format!("\"{src_path}\"").replace("\\", "\\\\");
77+
6978
Self {
7079
current_module_name_segments_count,
7180
line_numbers,
72-
project_root,
7381
module,
82+
src_path,
7483
tracker: UsageTracker::default(),
7584
module_scope: Default::default(),
7685
target_support,
@@ -81,7 +90,7 @@ impl<'a> Generator<'a> {
8190

8291
fn type_reference(&self) -> Document<'a> {
8392
if self.typescript == TypeScriptDeclarations::None {
84-
return "".to_doc();
93+
return nil();
8594
}
8695

8796
// Get the name of the module relative the directory (similar to basename)
@@ -97,8 +106,6 @@ impl<'a> Generator<'a> {
97106
}
98107

99108
pub fn compile(&mut self) -> Output<'a> {
100-
let type_reference = self.type_reference();
101-
102109
// Determine what JavaScript imports we need to generate
103110
let mut imports = self.collect_imports();
104111

@@ -217,52 +224,66 @@ impl<'a> Generator<'a> {
217224
self.register_prelude_usage(&mut imports, "sizedFloat", None);
218225
}
219226

220-
let echo = if self.tracker.echo_used {
221-
if StdlibPackage::Present == self.stdlib_package {
222-
self.register_import(
223-
&mut imports,
224-
"gleam_stdlib",
225-
"dict",
226-
&Some((
227-
AssignName::Variable("stdlib$dict".into()),
228-
SrcSpan::default(),
229-
)),
230-
&[],
231-
);
232-
}
233-
self.register_prelude_usage(&mut imports, "BitArray", Some("$BitArray"));
234-
self.register_prelude_usage(&mut imports, "List", Some("$List"));
235-
self.register_prelude_usage(&mut imports, "UtfCodepoint", Some("$UtfCodepoint"));
236-
docvec![line(), std::include_str!("../templates/echo.mjs"), line()]
237-
} else {
238-
nil()
239-
};
227+
let echo_definition = self.echo_definition(&mut imports);
228+
let type_reference = self.type_reference();
229+
let filepath_definition = self.filepath_definition();
240230

241231
// Put it all together
242232

243233
if imports.is_empty() && statements.is_empty() {
244-
Ok(docvec![type_reference, "export {}", line(), echo])
234+
Ok(docvec![
235+
type_reference,
236+
filepath_definition,
237+
"export {}",
238+
line(),
239+
echo_definition
240+
])
245241
} else if imports.is_empty() {
246242
statements.push(line());
247-
Ok(docvec![type_reference, statements, echo])
243+
Ok(docvec![
244+
type_reference,
245+
filepath_definition,
246+
statements,
247+
echo_definition
248+
])
248249
} else if statements.is_empty() {
249250
Ok(docvec![
250251
type_reference,
251252
imports.into_doc(JavaScriptCodegenTarget::JavaScript),
252-
echo,
253+
filepath_definition,
254+
echo_definition,
253255
])
254256
} else {
255257
Ok(docvec![
256258
type_reference,
257259
imports.into_doc(JavaScriptCodegenTarget::JavaScript),
258260
line(),
261+
filepath_definition,
259262
statements,
260263
line(),
261-
echo
264+
echo_definition
262265
])
263266
}
264267
}
265268

269+
fn echo_definition(&mut self, imports: &mut Imports<'a>) -> Document<'a> {
270+
if !self.tracker.echo_used {
271+
return nil();
272+
}
273+
274+
if StdlibPackage::Present == self.stdlib_package {
275+
let value = Some((
276+
AssignName::Variable("stdlib$dict".into()),
277+
SrcSpan::default(),
278+
));
279+
self.register_import(imports, "gleam_stdlib", "dict", &value, &[]);
280+
}
281+
self.register_prelude_usage(imports, "BitArray", Some("$BitArray"));
282+
self.register_prelude_usage(imports, "List", Some("$List"));
283+
self.register_prelude_usage(imports, "UtfCodepoint", Some("$UtfCodepoint"));
284+
docvec![line(), std::include_str!("../templates/echo.mjs"), line()]
285+
}
286+
266287
fn register_prelude_usage(
267288
&self,
268289
imports: &mut Imports<'a>,
@@ -541,8 +562,7 @@ impl<'a> Generator<'a> {
541562

542563
let mut generator = expression::Generator::new(
543564
self.module.name.clone(),
544-
&self.module.type_info.src_path,
545-
self.project_root,
565+
self.src_path.clone(),
546566
self.line_numbers,
547567
"".into(),
548568
vec![],
@@ -577,8 +597,7 @@ impl<'a> Generator<'a> {
577597
.collect();
578598
let mut generator = expression::Generator::new(
579599
self.module.name.clone(),
580-
&self.module.type_info.src_path,
581-
self.project_root,
600+
self.src_path.clone(),
582601
self.line_numbers,
583602
name.clone(),
584603
argument_names,
@@ -643,6 +662,14 @@ impl<'a> Generator<'a> {
643662
}
644663
}
645664
}
665+
666+
fn filepath_definition(&self) -> Document<'a> {
667+
if !self.tracker.make_error_used {
668+
return nil();
669+
}
670+
671+
docvec!["const FILEPATH = ", self.src_path.clone(), ';', lines(2)]
672+
}
646673
}
647674

648675
#[derive(Debug)]

compiler-core/src/javascript/expression.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ impl CurrentFunction {
124124
#[derive(Debug)]
125125
pub(crate) struct Generator<'module, 'ast> {
126126
module_name: EcoString,
127-
src_path: &'module Utf8Path,
128-
project_root: &'module Utf8Path,
127+
src_path: EcoString,
129128
line_numbers: &'module LineNumbers,
130129
function_name: EcoString,
131130
function_arguments: Vec<Option<&'module EcoString>>,
@@ -175,8 +174,7 @@ impl<'module, 'a> Generator<'module, 'a> {
175174
#[allow(clippy::too_many_arguments)] // TODO: FIXME
176175
pub fn new(
177176
module_name: EcoString,
178-
src_path: &'module Utf8Path,
179-
project_root: &'module Utf8Path,
177+
src_path: EcoString,
180178
line_numbers: &'module LineNumbers,
181179
function_name: EcoString,
182180
function_arguments: Vec<Option<&'module EcoString>>,
@@ -198,7 +196,6 @@ impl<'module, 'a> Generator<'module, 'a> {
198196
tracker,
199197
module_name,
200198
src_path,
201-
project_root,
202199
line_numbers,
203200
function_name,
204201
function_arguments,
@@ -1666,6 +1663,7 @@ impl<'module, 'a> Generator<'module, 'a> {
16661663
"throw makeError",
16671664
wrap_args([
16681665
string(error_name),
1666+
"FILEPATH".to_doc(),
16691667
module,
16701668
line,
16711669
function,
@@ -1695,18 +1693,9 @@ impl<'module, 'a> Generator<'module, 'a> {
16951693
fn echo(&mut self, expression: Document<'a>, location: &'a SrcSpan) -> Output<'a> {
16961694
self.tracker.echo_used = true;
16971695

1698-
let relative_path = self
1699-
.src_path
1700-
.strip_prefix(self.project_root)
1701-
.unwrap_or(self.src_path)
1702-
.as_str()
1703-
.replace("\\", "\\\\");
1704-
1705-
let relative_path_doc = EcoString::from(relative_path).to_doc();
1706-
17071696
let echo_argument = call_arguments(vec![
17081697
Ok(expression),
1709-
Ok(relative_path_doc.surround("\"", "\"")),
1698+
Ok(self.src_path.clone().to_doc()),
17101699
Ok(self.line_numbers.line_number(location.start).to_doc()),
17111700
])?;
17121701
Ok(self.wrap_return(docvec!["echo", echo_argument]))

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_binary_operation.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ pub fn main() {
1515
----- COMPILED JAVASCRIPT
1616
import { makeError } from "../gleam.mjs";
1717

18+
const FILEPATH = "src/module.gleam";
19+
1820
export function main() {
1921
let x = true;
2022

2123
if (!(x || false)) {
2224
throw makeError(
2325
"assert",
26+
FILEPATH,
2427
"my/mod",
2528
4,
2629
"main",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_binary_operation2.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ pub fn eq(a, b) {
1414
----- COMPILED JAVASCRIPT
1515
import { makeError, isEqual } from "../gleam.mjs";
1616

17+
const FILEPATH = "src/module.gleam";
18+
1719
export function eq(a, b) {
1820
if (!(isEqual(a, b))) {
1921
throw makeError(
2022
"assert",
23+
FILEPATH,
2124
"my/mod",
2225
3,
2326
"eq",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_binary_operation3.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ pub fn assert_answer(x) {
1414
----- COMPILED JAVASCRIPT
1515
import { makeError } from "../gleam.mjs";
1616

17+
const FILEPATH = "src/module.gleam";
18+
1719
export function assert_answer(x) {
1820
let $ = 42;
1921
if (!(x === $)) {
2022
throw makeError(
2123
"assert",
24+
FILEPATH,
2225
"my/mod",
2326
3,
2427
"assert_answer",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_binary_operator_with_side_effects.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn go(x) {
1919
----- COMPILED JAVASCRIPT
2020
import { makeError } from "../gleam.mjs";
2121

22+
const FILEPATH = "src/module.gleam";
23+
2224
function wibble(a, b) {
2325
let result = a + b;
2426
return result === 10;
@@ -29,6 +31,7 @@ export function go(x) {
2931
if (!wibble(1, 4)) {
3032
throw makeError(
3133
"assert",
34+
FILEPATH,
3235
"my/mod",
3336
8,
3437
"go",
@@ -47,6 +50,7 @@ export function go(x) {
4750
} else {
4851
throw makeError(
4952
"assert",
53+
FILEPATH,
5054
"my/mod",
5155
8,
5256
"go",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_binary_operator_with_side_effects2.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn go(x) {
1919
----- COMPILED JAVASCRIPT
2020
import { makeError } from "../gleam.mjs";
2121

22+
const FILEPATH = "src/module.gleam";
23+
2224
function wibble(a, b) {
2325
let result = a + b;
2426
return result === 10;
@@ -29,6 +31,7 @@ export function go(x) {
2931
if (!wibble(4, 6)) {
3032
throw makeError(
3133
"assert",
34+
FILEPATH,
3235
"my/mod",
3336
8,
3437
"go",
@@ -47,6 +50,7 @@ export function go(x) {
4750
} else {
4851
throw makeError(
4952
"assert",
53+
FILEPATH,
5054
"my/mod",
5155
8,
5256
"go",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_function_call.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn main() {
1818
----- COMPILED JAVASCRIPT
1919
import { makeError } from "../gleam.mjs";
2020

21+
const FILEPATH = "src/module.gleam";
22+
2123
function bool() {
2224
return true;
2325
}
@@ -26,6 +28,7 @@ export function main() {
2628
if (!bool()) {
2729
throw makeError(
2830
"assert",
31+
FILEPATH,
2932
"my/mod",
3033
7,
3134
"main",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_function_call2.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn go(x) {
1818
----- COMPILED JAVASCRIPT
1919
import { makeError } from "../gleam.mjs";
2020

21+
const FILEPATH = "src/module.gleam";
22+
2123
function and(a, b) {
2224
return a && b;
2325
}
@@ -26,6 +28,7 @@ export function go(x) {
2628
if (!and(true, x)) {
2729
throw makeError(
2830
"assert",
31+
FILEPATH,
2932
"my/mod",
3033
7,
3134
"go",

compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assert__assert_literal.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ pub fn main() {
1414
----- COMPILED JAVASCRIPT
1515
import { makeError } from "../gleam.mjs";
1616

17+
const FILEPATH = "src/module.gleam";
18+
1719
export function main() {
1820
if (!false) {
1921
throw makeError(
2022
"assert",
23+
FILEPATH,
2124
"my/mod",
2225
3,
2326
"main",

0 commit comments

Comments
 (0)