mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 05:32:09 +03:00
Validate sourcemap (#669)
This commit is contained in:
parent
f17e49934c
commit
64ed0bd801
1
.github/workflows/cargo.yml
vendored
1
.github/workflows/cargo.yml
vendored
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
5
package.json
Normal file
5
package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"sourcemap-validator": "^2.1.0"
|
||||
}
|
||||
}
|
10
tests/source_map.js
Normal file
10
tests/source_map.js
Normal file
@ -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);
|
56
tests/source_map.rs
Normal file
56
tests/source_map.rs
Normal file
@ -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();
|
||||
}
|
2
tests/srcmap/.gitignore
vendored
Normal file
2
tests/srcmap/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.g.js
|
||||
*.map
|
6
tests/srcmap/issue-622/index.js
Normal file
6
tests/srcmap/issue-622/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
function* foo() {
|
||||
try {
|
||||
return yield call();
|
||||
} finally {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user