turbopack_ecmascript/references/
type_issue.rs

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