1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-29 17:38:04 +01:00
bitwarden-browser/libs/shared/es2020-transformer.ts
Thomas Rittson 23ec317767
[EC-648] Fix esm features in node testing environment (#4223)
* Add AST transformer to remove import.meta in tests
2023-01-12 13:23:14 +10:00

37 lines
1.4 KiB
TypeScript

import * as ts from "typescript";
// Custom Typescript AST transformer for use with ts-jest / jest-preset-angular
// Removes specified ES2020 syntax from source code, as node does not support it yet
// Reference: https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers
// Use this tool to understand how we identify and filter AST nodes: https://ts-ast-viewer.com/
/**
* Remember to increase the version whenever transformer's content is changed. This is to inform Jest to not reuse
* the previous cache which contains old transformer's content
*/
export const version = 1;
export const name = "bit-es2020-transformer";
// Returns true for 'import.meta' statements
const isImportMetaStatement = (node: ts.Node) =>
ts.isPropertyAccessExpression(node) &&
ts.isMetaProperty(node.expression) &&
node.expression.keywordToken === ts.SyntaxKind.ImportKeyword;
export const factory = function (/*opts?: Opts*/) {
function visitor(ctx: ts.TransformationContext, sf: ts.SourceFile) {
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<any> => {
if (isImportMetaStatement(node)) {
return null;
}
// Continue searching child nodes
return ts.visitEachChild(node, visitor, ctx);
};
return visitor;
}
return (ctx: ts.TransformationContext): ts.Transformer<any> => {
return (sf: ts.SourceFile) => ts.visitNode(sf, visitor(ctx, sf));
};
};