turbopack_ecmascript/references/
type_issue.rs1use turbo_tasks::{ResolvedVc, Vc};
2use turbo_tasks_fs::FileSystemPath;
3use turbopack_core::issue::{Issue, IssueSeverity, IssueStage, OptionStyledString, StyledString};
4
5use crate::SpecifiedModuleType;
6
7#[turbo_tasks::value(shared)]
8pub struct SpecifiedModuleTypeIssue {
9 pub path: ResolvedVc<FileSystemPath>,
10 pub specified_type: SpecifiedModuleType,
11}
12
13#[turbo_tasks::value_impl]
14impl Issue for SpecifiedModuleTypeIssue {
15 #[turbo_tasks::function]
16 fn file_path(&self) -> Vc<FileSystemPath> {
17 *self.path
18 }
19
20 #[turbo_tasks::function]
21 fn title(&self) -> Vc<StyledString> {
22 StyledString::Text(match self.specified_type {
23 SpecifiedModuleType::CommonJs => "Specified module format (CommonJs) is not matching \
24 the module format of the source code (EcmaScript \
25 Modules)"
26 .into(),
27 SpecifiedModuleType::EcmaScript => "Specified module format (EcmaScript Modules) is \
28 not matching the module format of the source code \
29 (CommonJs)"
30 .into(),
31 SpecifiedModuleType::Automatic => "Specified module format is not matching the module \
32 format of the source code"
33 .into(),
34 })
35 .cell()
36 }
37
38 #[turbo_tasks::function]
39 fn description(&self) -> Vc<OptionStyledString> {
40 Vc::cell(Some(
41 StyledString::Text(match self.specified_type {
42 SpecifiedModuleType::CommonJs => {
43 "The CommonJs module format was specified in the package.json that is \
44 affecting this source file or by using an special extension, but Ecmascript \
45 import/export syntax is used in the source code.\nThe module was \
46 automatically converted to an EcmaScript module, but that is in conflict with \
47 the specified module format. Either change the \"type\" field in the \
48 package.json or replace EcmaScript import/export syntax with CommonJs syntas \
49 in the source file.\nIn some cases EcmaScript import/export syntax is added \
50 by an transform and isn't actually part of the source code. In these cases \
51 revisit transformation options to inject the correct syntax."
52 .into()
53 }
54 SpecifiedModuleType::EcmaScript => {
55 "The EcmaScript module format was specified in the package.json that is \
56 affecting this source file or by using an special extension, but it looks \
57 like that CommonJs syntax is used in the source code.\nExports made by \
58 CommonJs syntax will lead to a runtime error, since the module is in \
59 EcmaScript mode. Either change the \"type\" field in the package.json or \
60 replace CommonJs syntax with EcmaScript import/export syntax in the source \
61 file."
62 .into()
63 }
64 SpecifiedModuleType::Automatic => "The module format specified in the \
65 package.json file is not matching the module \
66 format of the source code."
67 .into(),
68 })
69 .resolved_cell(),
70 ))
71 }
72
73 #[turbo_tasks::function]
74 fn severity(&self) -> Vc<IssueSeverity> {
75 match self.specified_type {
76 SpecifiedModuleType::CommonJs => IssueSeverity::Error.cell(),
77 SpecifiedModuleType::EcmaScript => IssueSeverity::Warning.cell(),
78 SpecifiedModuleType::Automatic => IssueSeverity::Hint.cell(),
79 }
80 }
81
82 #[turbo_tasks::function]
83 fn stage(&self) -> Vc<IssueStage> {
84 IssueStage::Analysis.into()
85 }
86}