Skip to content

Commit

Permalink
fix: parse error in concatenatedModule should be emitted to diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Sep 3, 2024
1 parent b3ab9d5 commit 3d5f1c0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
52 changes: 35 additions & 17 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ use crate::{
unaffected_cache::UnaffectedModulesCache,
BoxDependency, BoxModule, CacheCount, CacheOptions, Chunk, ChunkByUkey, ChunkContentHash,
ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkKind, ChunkUkey, CodeGenerationJob,
CodeGenerationResults, CompilationLogger, CompilationLogging, CompilerOptions, DependencyId,
DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId,
Filename, ImportVarMap, LocalFilenameFn, Logger, Module, ModuleFactory, ModuleGraph,
ModuleGraphPartial, ModuleIdentifier, PathData, ResolverFactory, RuntimeGlobals, RuntimeModule,
RuntimeSpecMap, SharedPluginDriver, SourceType, Stats,
CodeGenerationResult, CodeGenerationResults, CompilationLogger, CompilationLogging,
CompilerOptions, DependencyId, DependencyType, Entry, EntryData, EntryOptions, EntryRuntime,
Entrypoint, ExecuteModuleId, Filename, ImportVarMap, LocalFilenameFn, Logger, Module,
ModuleFactory, ModuleGraph, ModuleGraphPartial, ModuleIdentifier, PathData, ResolverFactory,
RuntimeGlobals, RuntimeModule, RuntimeSpecMap, SharedPluginDriver, SourceType, Stats,
};

pub type BuildDependency = (
Expand Down Expand Up @@ -869,19 +869,33 @@ impl Compilation {
.into_par_iter()
.map(|job| {
let module = job.module;
self
.old_cache
.code_generate_occasion
.use_cache(job, |module, runtime| {
let module = module_graph
.module_by_identifier(&module)
.expect("should have module");
module.code_generation(self, Some(runtime), None)
})
.map(|(res, runtimes, from_cache)| (module, res, runtimes, from_cache))
let runtimes = job.runtimes.clone();
(
module,
runtimes,
self
.old_cache
.code_generate_occasion
.use_cache(job, |module, runtime| {
let module = module_graph
.module_by_identifier(&module)
.expect("should have module");
module.code_generation(self, Some(runtime), None)
}),
)
})
.collect::<Result<Vec<_>>>()?;
for (module, codegen_res, runtimes, from_cache) in results {
.map(|(module, runtime, result)| match result {
Ok((result, runtime, from_cache)) => (module, result, runtime, from_cache, None),
Err(err) => (
module,
CodeGenerationResult::default(),
runtime,
false,
Some(err),
),
})
.collect::<Vec<_>>();
for (module, codegen_res, runtimes, from_cache, err) in results {
if let Some(counter) = cache_counter {
if from_cache {
counter.hit();
Expand All @@ -901,6 +915,10 @@ impl Compilation {
.add(module, runtime, codegen_res_id);
}
self.code_generated_modules.insert(module);

if let Some(err) = err {
self.push_diagnostic(err.into());
}
}
Ok(())
}
Expand Down
31 changes: 16 additions & 15 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,21 +1715,22 @@ impl ConcatenatedModule {
Ok(res) => Program::Module(res),
Err(err) => {
let span: ErrorSpan = err.span().into();
self
.diagnostics
.lock()
.expect("should have diagnostics")
.append(&mut map_box_diagnostics_to_module_parse_diagnostics(vec![
rspack_error::TraceableError::from_source_file(
&fm,
span.start as usize,
span.end as usize,
"JavaScript parsing error".to_string(),
err.kind().msg().to_string(),
)
.with_kind(DiagnosticKind::JavaScript),
]));
return Ok(ModuleInfo::Concatenated(module_info));

// return empty error as we already push error to compilation.diagnostics
return Err(
rspack_error::TraceableError::from_source_file(
&fm,
span.start as usize,
span.end as usize,
format!(
"{} parsing error:\n",
module.readable_identifier(&compilation.options.context)
),
err.kind().msg().to_string(),
)
.with_kind(DiagnosticKind::JavaScript)
.into(),
);
}
};
let mut ast = Ast::new(program, cm, Some(comments));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(DEFINE_VAR)
export default 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import v from './foo'

console.log(v)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const rspack = require('@rspack/core')

/** @type {import('@rspack/core').Configuration} */
module.exports = {
plugins: [
new rspack.DefinePlugin({
DEFINE_VAR: '1 2 3'
})
],
optimization: {
concatenateModules: true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ERROR in × ./foo.js parsing error:
│ : Expected ',', got 'numeric literal (2, 2)'
╭─[1:14]
1 │ console.log(1 2 3)
· ─
2 │ /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (1);
╰────

0 comments on commit 3d5f1c0

Please sign in to comment.