diff --git a/.github/workflows/cargo.yml b/.github/workflows/cargo.yml index ab7829b23bc..c11cba6a3fc 100644 --- a/.github/workflows/cargo.yml +++ b/.github/workflows/cargo.yml @@ -43,6 +43,7 @@ jobs: - name: Install node dependencies run: | npm config set prefix ~/npm + npm i npm i browserslist regenerator-runtime npm i -g jest diff --git a/.rustfmt.toml b/.rustfmt.toml index 169904766a9..243c068ba77 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1,4 +1,4 @@ -required_version = "1.4.11" +required_version = "1.4.12" use_field_init_shorthand = true merge_imports = true wrap_comments = true diff --git a/.travis.yml b/.travis.yml index 84bcc2dddb0..8a821469434 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,7 @@ install: - source ~/.nvm/nvm.sh - nvm install 8.15.0 - nvm use 8.15.0 + - npm install - npm install browserslist regenerator - npm install -g jest - RUST_BACKTRACE=0 cargo test --no-run --color always --all --all-features diff --git a/ecmascript/codegen/src/text_writer/basic_impl.rs b/ecmascript/codegen/src/text_writer/basic_impl.rs index 284c824a5dc..fd4fd7cf0a8 100644 --- a/ecmascript/codegen/src/text_writer/basic_impl.rs +++ b/ecmascript/codegen/src/text_writer/basic_impl.rs @@ -3,6 +3,7 @@ use sourcemap::SourceMapBuilder; use std::{ io::{self, Write}, sync::Arc, + u16, }; use swc_common::{FileName, SourceMap, Span}; @@ -74,14 +75,16 @@ impl<'a, W: Write> JsWriter<'a, W> { FileName::Real(ref p) => Some(p.display().to_string()), _ => None, }; - srcmap.add( - self.line_count as _, - self.line_pos as _, - (loc.line - 1) as _, - loc.col.0 as _, - src.as_ref().map(|s| &**s), - None, - ); + if loc.col.0 < u16::MAX as usize { + srcmap.add( + self.line_count as _, + self.line_pos as _, + (loc.line - 1) as _, + loc.col.0 as _, + src.as_ref().map(|s| &**s), + None, + ); + } } }}; } diff --git a/package.json b/package.json new file mode 100644 index 00000000000..837d6125121 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "sourcemap-validator": "^2.1.0" + } +} diff --git a/tests/source_map.js b/tests/source_map.js new file mode 100644 index 00000000000..3a4fc11b0b2 --- /dev/null +++ b/tests/source_map.js @@ -0,0 +1,10 @@ +const validate = require('sourcemap-validator'); +const fs = require('fs'); + +const jsFile = process.argv[1]; +const mapFile = process.argv[2]; + +const jsContent = fs.readFileSync(jsFile, 'utf-8'); +const mapContent = fs.readFileSync(mapFile, 'utf-8'); + +validate(jsContent, mapContent); \ No newline at end of file diff --git a/tests/source_map.rs b/tests/source_map.rs new file mode 100644 index 00000000000..8fee8d133a6 --- /dev/null +++ b/tests/source_map.rs @@ -0,0 +1,56 @@ +use std::{fs::canonicalize, process::Command}; +use swc::{ + config::{Options, SourceMapsConfig}, + Compiler, +}; +use testing::{StdErr, Tester}; + +fn file(f: &str) -> Result<(), StdErr> { + Tester::new().print_errors(|cm, handler| { + let path = canonicalize(f).expect("failed to canonicalize"); + + let c = Compiler::new(cm.clone(), handler); + + let fm = cm.load_file(&path).expect("failed to load file"); + let s = c + .process_js_file( + fm, + &Options { + swcrc: true, + is_module: true, + source_maps: Some(SourceMapsConfig::Bool(true)), + ..Default::default() + }, + ) + .expect("failed to process js file"); + + let js_path = path.parent().unwrap().join("index.g.js"); + std::fs::write(&js_path, s.code.as_bytes()).unwrap(); + + let map_path = path.parent().unwrap().join("index.js.map"); + std::fs::write(&map_path, s.map.unwrap().as_bytes()).unwrap(); + + let output = Command::new("node") + .arg("-e") + .arg(include_str!("source_map.js")) + .arg(js_path) + .arg(map_path) + .output() + .unwrap(); + + if output.status.success() { + return Ok(()); + } + + panic!( + "Validation failed: \n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + }) +} + +#[test] +fn issue_622() { + file("tests/srcmap/issue-622/index.js").unwrap(); +} diff --git a/tests/srcmap/.gitignore b/tests/srcmap/.gitignore new file mode 100644 index 00000000000..7ea475918b3 --- /dev/null +++ b/tests/srcmap/.gitignore @@ -0,0 +1,2 @@ +*.g.js +*.map \ No newline at end of file diff --git a/tests/srcmap/issue-622/index.js b/tests/srcmap/issue-622/index.js new file mode 100644 index 00000000000..f679e5c829a --- /dev/null +++ b/tests/srcmap/issue-622/index.js @@ -0,0 +1,6 @@ +function* foo() { + try { + return yield call(); + } finally { + } +} \ No newline at end of file