Skip to main content

turbopack_ecmascript/references/
type_issue.rs

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