Skip to content

Commit

Permalink
feat(transformer/typescript): handle namespace directive correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 5, 2024
1 parent 30ba828 commit ee9a215
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 21 additions & 2 deletions crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,34 @@ impl<'a> TypeScript<'a> {
&self,
arg_name: Atom<'a>,
real_name: Atom<'a>,
stmts: Vec<'a, Statement<'a>>,
mut stmts: Vec<'a, Statement<'a>>,
parent_export: Option<Expression<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx,
) -> Statement<'a> {
let mut directives = self.ctx.ast.new_vec();

// Check if the namespace has a `use strict` directive
if stmts.first().is_some_and(|stmt| {
matches!(stmt, Statement::ExpressionStatement(es) if
matches!(&es.expression, Expression::StringLiteral(literal) if
literal.value == "use strict")
)
}) {
stmts.remove(0);
let directive = self.ctx.ast.new_atom("use strict");
let directive = Directive {
span: SPAN,
expression: StringLiteral::new(SPAN, directive.clone()),
directive,
};
directives.push(directive);
}

// `(function (_N) { var x; })(N || (N = {}))`;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
let callee = {
let body = self.ctx.ast.function_body(SPAN, self.ctx.ast.new_vec(), stmts);
let body = self.ctx.ast.function_body(SPAN, directives, stmts);
let params = {
let ident =
self.ctx.ast.binding_pattern_identifier(BindingIdentifier::new(SPAN, arg_name));
Expand Down
3 changes: 1 addition & 2 deletions tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ commit: 64d2eeea

transformer_typescript Summary:
AST Parsed : 5243/5243 (100.00%)
Positive Passed: 5237/5243 (99.89%)
Positive Passed: 5238/5243 (99.90%)
Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts"
Mismatch: "compiler/jsxComplexSignatureHasApplicabilityError.tsx"
Mismatch: "compiler/jsxEmptyExpressionNotCountedAsChild.tsx"
Mismatch: "compiler/sourceMapValidationClasses.ts"
Mismatch: "compiler/styledComponentsInstantiaionLimitNotReached.ts"
Mismatch: "compiler/tsxReactPropsInferenceSucceedsOnIntersections.tsx"

0 comments on commit ee9a215

Please sign in to comment.