Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile decorators properly when googmodule is false and transformTypesToClosure is true #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@types/source-map-support": "^0.5.3",
"diff-match-patch": "^1.0.5",
"glob": "8.0.1",
"google-closure-compiler": "^20230411.0.0",
"google-closure-compiler": "^20230502.0.0",
"jasmine": "^4.1.0",
"jasmine-node": "^3.0.0",
"source-map": "^0.7.3",
Expand Down
60 changes: 60 additions & 0 deletions src/fix_downleveled_decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as ts from "typescript";

/**
* This is an attempt to fix the downleveled decorators so Closure doesn't have
* trouble with them. The problem is that when `experimentalDecorators` is
* enabled, we TSC ends up converting a class decorator like this:
*
* @classDecorator
* export class Person { ... }
*
* to this:
*
* let Person = class Person { ... };
*
* as well as some calls to __decorate(Person, ...)
*
* The problem is that this causes Closure Compiler to fail with this error:
* ERROR - [JSC_CANNOT_CONVERT_YET] Transpilation of 'Classes with possible name shadowing' is not yet implemented.
* 21| let Person = class Person {
* ^^^^^^^^^^^^^^
*
* As previously mentioned, this transformer is an _attempt_, it doesn't actually work it seems.
* I'm not sure if tsickle can even affect this at this point in the process or not.
claydiffrient marked this conversation as resolved.
Show resolved Hide resolved
*/
claydiffrient marked this conversation as resolved.
Show resolved Hide resolved
export function fixDownleveledDecorators() {
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
return (sourceFile: ts.SourceFile) => {
function visit(node: ts.Node): ts.Node {
// Check if the node is a VariableDeclaration
if (ts.isVariableDeclaration(node)) {
// Check if it's a variable declaration of a class expression
if (node.initializer && ts.isClassExpression(node.initializer)) {
const className = node.name;
const classNameAsString = className.getText();

const typeParameter = ts.factory.createTypeParameterDeclaration(
undefined,
classNameAsString,
undefined,
undefined
);

const classDeclaration = ts.factory.createClassDeclaration(
undefined,
undefined,
[typeParameter],
[],
node.initializer.members
);

// Replace the variable declaration with the class declaration
ts.factory.updateSourceFile(sourceFile, [classDeclaration]);
}
}
return ts.visitEachChild(node, visit, context);
}
return ts.visitEachChild(sourceFile, visit, context);
};
};
}
2 changes: 2 additions & 0 deletions src/tsickle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {FileSummary, SummaryGenerationProcessorHost} from './summary';
import {isDtsFileName} from './transformer_util';
import * as tsmes from './ts_migration_exports_shim';
import {makeTsickleDeclarationMarkerTransformerFactory} from './tsickle_declaration_marker';
import {fixDownleveledDecorators} from './fix_downleveled_decorators';

// Exported for users as a default impl of pathToModuleName.
export {pathToModuleName} from './cli_support';
Expand Down Expand Up @@ -253,6 +254,7 @@ export function emit(
transformDecoratorsOutputForClosurePropertyRenaming(
tsickleDiagnostics, false));
tsTransformers.after!.push(transformDecoratorJsdoc());
tsTransformers.after!.push(fixDownleveledDecorators())
}
if (host.addDtsClutzAliases) {
tsTransformers.afterDeclarations!.push(
Expand Down