0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix: harden pragma and reference parsing in module analysis (#6702)

This commit is contained in:
Bartek Iwańczuk 2020-07-11 18:30:30 +02:00 committed by GitHub
parent a26b4a9f1e
commit 871f9255e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -165,10 +165,22 @@ impl Future for CompilerWorker {
} }
} }
// TODO(bartlomieju): use JSONC parser from dprint instead of Regex
lazy_static! { lazy_static! {
// TODO(bartlomieju): use JSONC parser from dprint instead of Regex
static ref CHECK_JS_RE: Regex = static ref CHECK_JS_RE: Regex =
Regex::new(r#""checkJs"\s*?:\s*?true"#).unwrap(); Regex::new(r#""checkJs"\s*?:\s*?true"#).unwrap();
static ref DENO_TYPES_RE: Regex =
Regex::new(r"^\s*@deno-types\s?=\s?(\S+)\s*(.*)\s*$").unwrap();
// These regexes were adapted from TypeScript
// https://github.com/microsoft/TypeScript/blob/87fd1827f2f2f3dafa76c14f13b9defc69481766/src/compiler/parser.ts#L8780-L8781
static ref XML_COMMENT_START_RE: Regex =
Regex::new(r"^/\s*<(\S+)\s.*?/>").unwrap();
static ref PATH_REFERENCE_RE: Regex =
Regex::new(r#"(\spath\s*=\s*)('|")(.+?)('|")"#).unwrap();
static ref TYPES_REFERENCE_RE: Regex =
Regex::new(r#"(\stypes\s*=\s*)('|")(.+?)('|")"#).unwrap();
static ref LIB_REFERENCE_RE: Regex =
Regex::new(r#"(\slib\s*=\s*)('|")(.+?)('|")"#).unwrap();
} }
/// Create a new worker with snapshot of TS compiler and setup compiler's /// Create a new worker with snapshot of TS compiler and setup compiler's
@ -1478,54 +1490,37 @@ fn get_deno_types(parser: &AstParser, span: Span) -> Option<String> {
parse_deno_types(&comment) parse_deno_types(&comment)
} }
// TODO(bartlomieju): refactor
fn parse_ts_reference(comment: &str) -> Option<(TsReferenceKind, String)> { fn parse_ts_reference(comment: &str) -> Option<(TsReferenceKind, String)> {
let (kind, specifier_in_quotes) = if comment.starts_with("/ <reference path=") if !XML_COMMENT_START_RE.is_match(comment) {
{
(
TsReferenceKind::Path,
comment.trim_start_matches("/ <reference path="),
)
} else if comment.starts_with("/ <reference lib=") {
(
TsReferenceKind::Lib,
comment.trim_start_matches("/ <reference lib="),
)
} else if comment.starts_with("/ <reference types=") {
(
TsReferenceKind::Types,
comment.trim_start_matches("/ <reference types="),
)
} else {
return None; return None;
}; }
let specifier = specifier_in_quotes let (kind, specifier) =
.trim_end_matches("/>") if let Some(capture_groups) = PATH_REFERENCE_RE.captures(comment) {
.trim_end() (TsReferenceKind::Path, capture_groups.get(3).unwrap())
.trim_start_matches('\"') } else if let Some(capture_groups) = TYPES_REFERENCE_RE.captures(comment) {
.trim_start_matches('\'') (TsReferenceKind::Types, capture_groups.get(3).unwrap())
.trim_end_matches('\"') } else if let Some(capture_groups) = LIB_REFERENCE_RE.captures(comment) {
.trim_end_matches('\'') (TsReferenceKind::Lib, capture_groups.get(3).unwrap())
.to_string(); } else {
return None;
};
Some((kind, specifier)) Some((kind, specifier.as_str().to_string()))
} }
fn parse_deno_types(comment: &str) -> Option<String> { fn parse_deno_types(comment: &str) -> Option<String> {
if comment.starts_with("@deno-types") { if let Some(capture_groups) = DENO_TYPES_RE.captures(comment) {
let split: Vec<String> = if let Some(specifier) = capture_groups.get(1) {
comment.split('=').map(|s| s.to_string()).collect(); let s = specifier
assert_eq!(split.len(), 2); .as_str()
let specifier_in_quotes = split.get(1).unwrap().to_string(); .trim_start_matches('\"')
let specifier = specifier_in_quotes .trim_start_matches('\'')
.trim() .trim_end_matches('\"')
.trim_start_matches('\"') .trim_end_matches('\'')
.trim_start_matches('\'') .to_string();
.trim_end_matches('\"') return Some(s);
.trim_end_matches('\'') }
.to_string();
return Some(specifier);
} }
None None
@ -1547,6 +1542,10 @@ mod tests {
parse_deno_types("@deno-types=./a/b/c.d.ts"), parse_deno_types("@deno-types=./a/b/c.d.ts"),
Some("./a/b/c.d.ts".to_string()) Some("./a/b/c.d.ts".to_string())
); );
assert_eq!(
parse_deno_types("@deno-types=\"./a/b/c.d.ts\""),
Some("./a/b/c.d.ts".to_string())
);
assert_eq!( assert_eq!(
parse_deno_types("@deno-types = https://dneo.land/x/some/package/a.d.ts"), parse_deno_types("@deno-types = https://dneo.land/x/some/package/a.d.ts"),
Some("https://dneo.land/x/some/package/a.d.ts".to_string()) Some("https://dneo.land/x/some/package/a.d.ts".to_string())
@ -1557,6 +1556,16 @@ mod tests {
); );
assert!(parse_deno_types("asdf").is_none()); assert!(parse_deno_types("asdf").is_none());
assert!(parse_deno_types("// deno-types = fooo").is_none()); assert!(parse_deno_types("// deno-types = fooo").is_none());
assert_eq!(
parse_deno_types("@deno-types=./a/b/c.d.ts some comment"),
Some("./a/b/c.d.ts".to_string())
);
assert_eq!(
parse_deno_types(
"@deno-types=./a/b/c.d.ts // some comment after slashes"
),
Some("./a/b/c.d.ts".to_string())
);
} }
#[test] #[test]