feat(transformer/typescript): reports error for import lib = require(...); (#3020)

This commit is contained in:
Dunqing authored and GitHub committed 2024-04-18 18:54:45 +08:00
1 parent e14ac17c72
commit 7416de217b
5 files changed
+24 -4

No files matched your search

+5
View File
@@ -9,6 +9,7 @@ use oxc_allocator::Allocator;
use oxc_ast::AstBuilder;
use oxc_diagnostics::Error;
use oxc_semantic::Semantic;
use oxc_span::SourceType;
use crate::{helpers::module_imports::ModuleImports, TransformOptions};
@@ -74,4 +75,8 @@ impl<'a> TransformCtx<'a> {
pub fn error<T: Into<Error>>(&self, error: T) {
self.errors.borrow_mut().push(error.into());
}
pub fn source_type(&self) -> &SourceType {
self.semantic.source_type()
}
}
@@ -0,0 +1,10 @@
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_span::Span;
#[derive(Debug, Error, Diagnostic)]
#[error("`import lib = require(...);` is only supported when compiling modules to CommonJS.\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.")]
#[diagnostic(severity(warning))]
pub struct ImportEqualsRequireUnsupported(#[label] pub Span);
@@ -1,5 +1,6 @@
mod annotations;
mod collector;
mod diagnostics;
mod r#enum;
mod module;
mod namespace;
@@ -2,7 +2,7 @@ use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_span::SPAN;
use super::TypeScript;
use super::{diagnostics::ImportEqualsRequireUnsupported, TypeScript};
impl<'a> TypeScript<'a> {
fn transform_ts_type_name(&self, type_name: &mut TSTypeName<'a>) -> Expression<'a> {
@@ -39,10 +39,15 @@ impl<'a> TypeScript<'a> {
let binding_identifier = BindingIdentifier::new(SPAN, decl.id.name.clone());
let binding_pattern_kind = self.ctx.ast.binding_pattern_identifier(binding_identifier);
let binding = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false);
let decl_span = decl.span;
let init = match &mut *decl.module_reference {
TSModuleReference::TypeName(type_name) => self.transform_ts_type_name(type_name),
TSModuleReference::ExternalModuleReference(reference) => {
if self.ctx.source_type().is_module() {
self.ctx.error(ImportEqualsRequireUnsupported(decl_span));
}
let callee = self.ctx.ast.identifier_reference_expression(
IdentifierReference::new(SPAN, "require".into()),
);
+2 -3
View File
@@ -1,4 +1,4 @@
Passed: 151/209
Passed: 152/209
# All Passed:
* babel-plugin-transform-react-jsx-source
@@ -17,7 +17,7 @@ Passed: 151/209
* opts/optimizeConstEnums/input.ts
* opts/rewriteImportExtensions/input.ts
# babel-plugin-transform-typescript (95/139)
# babel-plugin-transform-typescript (96/139)
* class/accessor-allowDeclareFields-false/input.ts
* class/accessor-allowDeclareFields-true/input.ts
* exports/declared-types/input.ts
@@ -26,7 +26,6 @@ Passed: 151/209
* exports/export=/input.ts
* imports/enum-id/input.ts
* imports/enum-value/input.ts
* imports/import=-module/input.ts
* imports/type-only-export-specifier-2/input.ts
* namespace/ambient-module-nested/input.ts
* namespace/ambient-module-nested-exported/input.ts