File size: 23,172 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
use std::{future::Future, sync::Arc};
use anyhow::{Context, Result, anyhow};
use bytes_str::BytesStr;
use rustc_hash::FxHashSet;
use swc_core::{
base::SwcComments,
common::{
BytePos, FileName, GLOBALS, Globals, LineCol, Mark, SyntaxContext,
errors::{HANDLER, Handler},
input::StringInput,
source_map::{Files, SourceMapGenConfig, build_source_map},
util::take::Take,
},
ecma::{
ast::{EsVersion, Id, ObjectPatProp, Pat, Program, VarDecl},
lints::{config::LintConfig, rules::LintParams},
parser::{EsSyntax, Parser, Syntax, TsSyntax, lexer::Lexer},
transforms::base::{
helpers::{HELPERS, Helpers},
resolver,
},
visit::{Visit, VisitMutWith, VisitWith, noop_visit_type},
},
};
use tracing::{Instrument, Level, instrument};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc, util::WrapFuture};
use turbo_tasks_fs::{FileContent, FileSystemPath, rope::Rope};
use turbo_tasks_hash::hash_xxh3_hash64;
use turbopack_core::{
SOURCE_URL_PROTOCOL,
asset::{Asset, AssetContent},
error::PrettyPrintError,
issue::{
Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
source::Source,
source_map::utils::add_default_ignore_list,
};
use turbopack_swc_utils::emitter::IssueEmitter;
use super::EcmascriptModuleAssetType;
use crate::{
EcmascriptInputTransform,
analyzer::{ImportMap, graph::EvalContext},
swc_comments::ImmutableComments,
transform::{EcmascriptInputTransforms, TransformContext},
};
#[turbo_tasks::value(shared, serialization = "none", eq = "manual")]
#[allow(clippy::large_enum_variant)]
pub enum ParseResult {
// Note: Ok must not contain any Vc as it's snapshot by failsafe_parse
Ok {
#[turbo_tasks(debug_ignore, trace_ignore)]
program: Program,
#[turbo_tasks(debug_ignore, trace_ignore)]
comments: Arc<ImmutableComments>,
#[turbo_tasks(debug_ignore, trace_ignore)]
eval_context: EvalContext,
#[turbo_tasks(debug_ignore, trace_ignore)]
globals: Arc<Globals>,
#[turbo_tasks(debug_ignore, trace_ignore)]
source_map: Arc<swc_core::common::SourceMap>,
},
Unparsable {
messages: Option<Vec<RcStr>>,
},
NotFound,
}
impl PartialEq for ParseResult {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Ok { .. }, Self::Ok { .. }) => false,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
#[turbo_tasks::value_impl]
impl ParseResult {
#[turbo_tasks::function]
pub fn empty() -> Vc<ParseResult> {
let globals = Globals::new();
let eval_context = GLOBALS.set(&globals, || EvalContext {
unresolved_mark: Mark::new(),
top_level_mark: Mark::new(),
imports: ImportMap::default(),
force_free_values: Default::default(),
});
ParseResult::Ok {
program: Program::Module(swc_core::ecma::ast::Module::dummy()),
comments: Default::default(),
eval_context,
globals: Arc::new(globals),
source_map: Default::default(),
}
.cell()
}
}
/// `original_source_maps_complete` indicates whether the `original_source_maps` cover the whole
/// map, i.e. whether every module that ended up in `mappings` had an original sourcemap.
#[instrument(level = Level::INFO, skip_all)]
pub fn generate_js_source_map<'a>(
files_map: &impl Files,
mappings: Vec<(BytePos, LineCol)>,
original_source_maps: impl IntoIterator<Item = &'a Rope>,
original_source_maps_complete: bool,
inline_sources_content: bool,
) -> Result<Rope> {
let original_source_maps = original_source_maps
.into_iter()
.map(|map| map.to_bytes())
.collect::<Vec<_>>();
let original_source_maps = original_source_maps
.iter()
.map(|map| Ok(swc_sourcemap::lazy::decode(map)?.into_source_map()?))
.collect::<Result<Vec<_>>>()?;
let fast_path_single_original_source_map =
original_source_maps.len() == 1 && original_source_maps_complete;
let mut new_mappings = build_source_map(
files_map,
&mappings,
None,
&InlineSourcesContentConfig {
// If we are going to adjust the source map, we are going to throw the source contents
// of this source map away regardless.
//
// In other words, we don't need the content of `B` in source map chain of A -> B -> C.
// We only need the source content of `A`, and a way to map the content of `B` back to
// `A`, while constructing the final source map, `C`.
inline_sources_content: inline_sources_content && !fast_path_single_original_source_map,
},
);
if original_source_maps.is_empty() {
// We don't convert sourcemap::SourceMap into raw_sourcemap::SourceMap because we don't
// need to adjust mappings
add_default_ignore_list(&mut new_mappings);
let mut result = vec![];
new_mappings.to_writer(&mut result)?;
Ok(Rope::from(result))
} else if fast_path_single_original_source_map {
let mut map = original_source_maps.into_iter().next().unwrap();
// TODO: Make this more efficient
map.adjust_mappings(new_mappings);
// TODO: Enable this when we have a way to handle the ignore list
// add_default_ignore_list(&mut map);
let map = map.into_raw_sourcemap();
let result = serde_json::to_vec(&map)?;
Ok(Rope::from(result))
} else {
let mut map = new_mappings.adjust_mappings_from_multiple(original_source_maps);
add_default_ignore_list(&mut map);
let mut result = vec![];
map.to_writer(&mut result)?;
Ok(Rope::from(result))
}
}
/// A config to generate a source map which includes the source content of every
/// source file. SWC doesn't inline sources content by default when generating a
/// sourcemap, so we need to provide a custom config to do it.
pub struct InlineSourcesContentConfig {
inline_sources_content: bool,
}
impl SourceMapGenConfig for InlineSourcesContentConfig {
fn file_name_to_source(&self, f: &FileName) -> String {
match f {
FileName::Custom(s) => {
format!("{SOURCE_URL_PROTOCOL}///{s}")
}
_ => f.to_string(),
}
}
fn inline_sources_content(&self, _f: &FileName) -> bool {
self.inline_sources_content
}
}
#[turbo_tasks::function]
pub async fn parse(
source: ResolvedVc<Box<dyn Source>>,
ty: EcmascriptModuleAssetType,
transforms: Vc<EcmascriptInputTransforms>,
) -> Result<Vc<ParseResult>> {
let name = source.ident().to_string().await?.to_string();
let span = tracing::info_span!("parse ecmascript", name = name, ty = display(&ty));
match parse_internal(source, ty, transforms)
.instrument(span)
.await
{
Ok(result) => Ok(result),
Err(error) => Err(error.context(format!(
"failed to parse {}",
source.ident().to_string().await?
))),
}
}
async fn parse_internal(
source: ResolvedVc<Box<dyn Source>>,
ty: EcmascriptModuleAssetType,
transforms: Vc<EcmascriptInputTransforms>,
) -> Result<Vc<ParseResult>> {
let content = source.content();
let fs_path_vc = source.ident().path().owned().await?;
let fs_path = fs_path_vc.clone();
let ident = &*source.ident().to_string().await?;
let file_path_hash = hash_xxh3_hash64(&*source.ident().to_string().await?) as u128;
let content = match content.await {
Ok(content) => content,
Err(error) => {
let error: RcStr = PrettyPrintError(&error).to_string().into();
ReadSourceIssue {
source: IssueSource::from_source_only(source),
error: error.clone(),
}
.resolved_cell()
.emit();
return Ok(ParseResult::Unparsable {
messages: Some(vec![error]),
}
.cell());
}
};
Ok(match &*content {
AssetContent::File(file) => match &*file.await? {
FileContent::NotFound => ParseResult::NotFound.cell(),
FileContent::Content(file) => {
match BytesStr::from_utf8(file.content().clone().into_bytes()) {
Ok(string) => {
let transforms = &*transforms.await?;
match parse_file_content(
string,
fs_path_vc.clone(),
&fs_path,
ident,
source.ident().await?.query.clone(),
file_path_hash,
source,
ty,
transforms,
)
.await
{
Ok(result) => result,
Err(e) => {
return Err(e).context(anyhow!(
"Transforming and/or parsing of {} failed",
source.ident().to_string().await?
));
}
}
}
Err(error) => {
let error: RcStr = PrettyPrintError(
&anyhow::anyhow!(error).context("failed to convert rope into string"),
)
.to_string()
.into();
ReadSourceIssue {
// Technically we could supply byte offsets to the issue source, but
// that would cause another utf8 error to be produced when we attempt to
// infer line/column offsets
source: IssueSource::from_source_only(source),
error: error.clone(),
}
.resolved_cell()
.emit();
ParseResult::Unparsable {
messages: Some(vec![error]),
}
.cell()
}
}
}
},
AssetContent::Redirect { .. } => ParseResult::Unparsable { messages: None }.cell(),
})
}
async fn parse_file_content(
string: BytesStr,
fs_path_vc: FileSystemPath,
fs_path: &FileSystemPath,
ident: &str,
query: RcStr,
file_path_hash: u128,
source: ResolvedVc<Box<dyn Source>>,
ty: EcmascriptModuleAssetType,
transforms: &[EcmascriptInputTransform],
) -> Result<Vc<ParseResult>> {
let source_map: Arc<swc_core::common::SourceMap> = Default::default();
let (emitter, collector) = IssueEmitter::new(
source,
source_map.clone(),
Some("Ecmascript file had an error".into()),
);
let handler = Handler::with_emitter(true, false, Box::new(emitter));
let (emitter, collector_parse) = IssueEmitter::new(
source,
source_map.clone(),
Some("Parsing ecmascript source code failed".into()),
);
let parser_handler = Handler::with_emitter(true, false, Box::new(emitter));
let globals = Arc::new(Globals::new());
let globals_ref = &globals;
let mut result = WrapFuture::new(
async {
let file_name = FileName::Custom(ident.to_string());
let fm = source_map.new_source_file(file_name.clone().into(), string);
let comments = SwcComments::default();
let mut parsed_program = {
let lexer = Lexer::new(
match ty {
EcmascriptModuleAssetType::Ecmascript => Syntax::Es(EsSyntax {
jsx: true,
fn_bind: true,
decorators: true,
decorators_before_export: true,
export_default_from: true,
import_attributes: true,
allow_super_outside_method: true,
allow_return_outside_function: true,
auto_accessors: true,
explicit_resource_management: true,
}),
EcmascriptModuleAssetType::Typescript { tsx, .. } => {
Syntax::Typescript(TsSyntax {
decorators: true,
dts: false,
no_early_errors: true,
tsx,
disallow_ambiguous_jsx_like: false,
})
}
EcmascriptModuleAssetType::TypescriptDeclaration => {
Syntax::Typescript(TsSyntax {
decorators: true,
dts: true,
no_early_errors: true,
tsx: false,
disallow_ambiguous_jsx_like: false,
})
}
},
EsVersion::latest(),
StringInput::from(&*fm),
Some(&comments),
);
let mut parser = Parser::new_from(lexer);
let span = tracing::trace_span!("swc_parse").entered();
let program_result = parser.parse_program();
drop(span);
let mut has_errors = vec![];
for e in parser.take_errors() {
let mut e = e.into_diagnostic(&parser_handler);
has_errors.extend(e.message.iter().map(|m| m.0.as_str().into()));
e.emit();
}
if !has_errors.is_empty() {
return Ok(ParseResult::Unparsable {
messages: Some(has_errors),
});
}
match program_result {
Ok(parsed_program) => parsed_program,
Err(e) => {
let mut e = e.into_diagnostic(&parser_handler);
let messages = e.message.iter().map(|m| m.0.as_str().into()).collect();
e.emit();
return Ok(ParseResult::Unparsable {
messages: Some(messages),
});
}
}
};
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
let is_typescript = matches!(
ty,
EcmascriptModuleAssetType::Typescript { .. }
| EcmascriptModuleAssetType::TypescriptDeclaration
);
let helpers=Helpers::new(true);
let span = tracing::trace_span!("swc_resolver").entered();
parsed_program.visit_mut_with(&mut resolver(
unresolved_mark,
top_level_mark,
is_typescript,
));
drop(span);
let span = tracing::trace_span!("swc_lint").entered();
let lint_config = LintConfig::default();
let rules = swc_core::ecma::lints::rules::all(LintParams {
program: &parsed_program,
lint_config: &lint_config,
unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark),
es_version: EsVersion::latest(),
source_map: source_map.clone(),
});
parsed_program.mutate(swc_core::ecma::lints::rules::lint_pass(rules));
drop(span);
HELPERS.set(&helpers, || {
parsed_program.mutate(
swc_core::ecma::transforms::proposal::explicit_resource_management::explicit_resource_management(),
);
});
let var_with_ts_declare = if is_typescript {
VarDeclWithTsDeclareCollector::collect(&parsed_program)
} else {
FxHashSet::default()
};
let mut helpers = helpers.data();
let transform_context = TransformContext {
comments: &comments,
source_map: &source_map,
top_level_mark,
unresolved_mark,
file_path_str: &fs_path.path,
file_name_str: fs_path.file_name(),
file_name_hash: file_path_hash,
query_str: query,
file_path: fs_path_vc.clone(),
source
};
let span = tracing::trace_span!("transforms");
async {
for transform in transforms.iter() {
helpers = transform
.apply(&mut parsed_program, &transform_context, helpers)
.await?;
}
anyhow::Ok(())
}
.instrument(span)
.await?;
if parser_handler.has_errors() {
let messages = if let Some(error) = collector_parse.last_emitted_issue() {
// The emitter created in here only uses StyledString::Text
if let StyledString::Text(xx) = &*error.await?.message.await? {
Some(vec![xx.clone()])
} else {
None
}
} else {
None
};
let messages =
Some(messages.unwrap_or_else(|| vec![fm.src.clone().into()]));
return Ok(ParseResult::Unparsable { messages });
}
let helpers = Helpers::from_data(helpers);
HELPERS.set(&helpers, || {
parsed_program.mutate(
swc_core::ecma::transforms::base::helpers::inject_helpers(unresolved_mark),
);
});
let eval_context = EvalContext::new(
&parsed_program,
unresolved_mark,
top_level_mark,
Arc::new(var_with_ts_declare),
Some(&comments),
Some(source),
);
Ok::<ParseResult, anyhow::Error>(ParseResult::Ok {
program: parsed_program,
comments: Arc::new(ImmutableComments::new(comments)),
eval_context,
// Temporary globals as the current one can't be moved yet, since they are
// borrowed
globals: Arc::new(Globals::new()),
source_map,
})
},
|f, cx| {
GLOBALS.set(globals_ref, || {
HANDLER.set(&handler, || f.poll(cx))
})
},
)
.await?;
if let ParseResult::Ok {
globals: ref mut g, ..
} = result
{
// Assign the correct globals
*g = globals;
}
collector.emit().await?;
collector_parse.emit().await?;
Ok(result.cell())
}
#[turbo_tasks::value]
struct ReadSourceIssue {
source: IssueSource,
error: RcStr,
}
#[turbo_tasks::value_impl]
impl Issue for ReadSourceIssue {
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.source.file_path()
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text(rcstr!("Reading source code for parsing failed")).cell()
}
#[turbo_tasks::function]
fn description(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(
StyledString::Text(
format!(
"An unexpected error happened while trying to read the source code to parse: \
{}",
self.error
)
.into(),
)
.resolved_cell(),
))
}
fn severity(&self) -> IssueSeverity {
IssueSeverity::Error
}
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Load.cell()
}
#[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> {
Vc::cell(Some(self.source))
}
}
struct VarDeclWithTsDeclareCollector {
id_with_no_ts_declare: FxHashSet<Id>,
id_with_ts_declare: FxHashSet<Id>,
}
impl VarDeclWithTsDeclareCollector {
fn collect<N: VisitWith<VarDeclWithTsDeclareCollector>>(n: &N) -> FxHashSet<Id> {
let mut collector = VarDeclWithTsDeclareCollector {
id_with_no_ts_declare: Default::default(),
id_with_ts_declare: Default::default(),
};
n.visit_with(&mut collector);
collector
.id_with_ts_declare
.retain(|id| !collector.id_with_no_ts_declare.contains(id));
collector.id_with_ts_declare
}
fn handle_pat(&mut self, pat: &Pat, declare: bool) {
match pat {
Pat::Ident(binding_ident) => {
if declare {
self.id_with_ts_declare.insert(binding_ident.to_id());
} else {
self.id_with_no_ts_declare.insert(binding_ident.to_id());
}
}
Pat::Array(array_pat) => {
for pat in array_pat.elems.iter().flatten() {
self.handle_pat(pat, declare);
}
}
Pat::Object(object_pat) => {
for prop in object_pat.props.iter() {
match prop {
ObjectPatProp::KeyValue(key_value_pat_prop) => {
self.handle_pat(&key_value_pat_prop.value, declare);
}
ObjectPatProp::Assign(assign_pat_prop) => {
if declare {
self.id_with_ts_declare.insert(assign_pat_prop.key.to_id());
} else {
self.id_with_no_ts_declare
.insert(assign_pat_prop.key.to_id());
}
}
_ => {}
}
}
}
_ => {}
}
}
}
impl Visit for VarDeclWithTsDeclareCollector {
noop_visit_type!();
fn visit_var_decl(&mut self, node: &VarDecl) {
for decl in node.decls.iter() {
self.handle_pat(&decl.name, node.declare);
}
}
}
|