All files / src/compiler/phases/1-parse remove_typescript_nodes.js

79.06% Statements 68/86
90.47% Branches 19/21
64.28% Functions 9/14
78.57% Lines 66/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 852x 2x 2x 2x 2x 2x 18x 16x 18x 16x 16x 14x 14x 14x     2x 2x 3x 1x 1x 1x 1x                   2x 2x     2x 2x     2x 2x 22x 2x 2x 2x 2x 2x 14x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x   2x 2x   2x 2x 463x 29x 29x 29x 29x 29x 434x 434x 2x 2x 2x 2x 2x 2x 2x 2x 66x 66x  
import { walk } from 'zimmerframe';
import * as b from '../../utils/builders.js';
 
/** @type {import('zimmerframe').Visitors<any, null>} */
const visitors = {
	ImportDeclaration(node) {
		if (node.importKind === 'type') return b.empty;
 
		if (node.specifiers?.length > 0) {
			const specifiers = node.specifiers.filter((/** @type {any} */ s) => s.importKind !== 'type');
			if (specifiers.length === 0) return b.empty;
 
			return { ...node, specifiers };
		}

		return node;
	},
	ExportNamedDeclaration(node, context) {
		if (node.exportKind === 'type') return b.empty;
 
		if (node.declaration) {
			return context.next();
		}

		if (node.specifiers) {
			const specifiers = node.specifiers.filter((/** @type {any} */ s) => s.exportKind !== 'type');
			if (specifiers.length === 0) return b.empty;

			return { ...node, specifiers };
		}

		return node;
	},
	ExportDefaultDeclaration(node) {
		if (node.exportKind === 'type') return b.empty;
		return node;
	},
	ExportAllDeclaration(node) {
		if (node.exportKind === 'type') return b.empty;
		return node;
	},
	TSAsExpression(node, context) {
		return context.visit(node.expression);
	},
	TSSatisfiesExpression(node, context) {
		return context.visit(node.expression);
	},
	TSNonNullExpression(node, context) {
		return context.visit(node.expression);
	},
	TSInterfaceDeclaration() {
		return b.empty;
	},
	TSTypeAliasDeclaration() {
		return b.empty;
	},
	TSTypeParameterDeclaration() {
		return b.empty;
	},
	TSTypeParameterInstantiation() {
		return b.empty;
	},
	TSEnumDeclaration() {
		return b.empty;
	},
	Identifier(node) {
		if (node.typeAnnotation) {
			return {
				...node,
				typeAnnotation: null
			};
		}
		return node;
	}
};
 
/**
 * @template T
 * @param {T} ast
 * @returns {T}
 */
export function remove_typescript_nodes(ast) {
	return walk(ast, null, visitors);
}