Validate sourcemap (#669)

This commit is contained in:
강동윤 2020-02-16 16:51:42 +09:00 committed by GitHub
parent f17e49934c
commit 64ed0bd801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 93 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
View File

@ -0,0 +1,5 @@
{
"devDependencies": {
"sourcemap-validator": "^2.1.0"
}
}

10
tests/source_map.js Normal file
View 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
View 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
View File

@ -0,0 +1,2 @@
*.g.js
*.map

View File

@ -0,0 +1,6 @@
function* foo() {
try {
return yield call();
} finally {
}
}