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

89.21% Statements 91/102
90% Branches 27/30
68.75% Functions 11/16
89% Lines 89/100

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1012x 2x 2x 2x 2x 2x 2x 8x 8x 2x 2x 8x 8x 2x 2x 2x 2x 18x 16x 18x 16x 16x 14x 14x 14x     2x 2x 4x 2x 2x 1x 1x 1x 1x 1x 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 2x 2x 2x 517x 29x 29x 29x 29x 29x 488x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 76x 76x  
import { walk } from 'zimmerframe';
import * as b from '../../utils/builders.js';
 
/**
 * @param {import('estree').FunctionExpression | import('estree').FunctionDeclaration} node
 * @param {import('zimmerframe').Context<any, any>} context
 */
function remove_this_param(node, context) {
	if (node.params[0]?.type === 'Identifier' && node.params[0].name === 'this') {
		node.params.shift();
	}
	return context.next();
}
 
/** @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;
	},
	TSParameterProperty(node) {
		return node.parameter;
	},
	Identifier(node) {
		if (node.typeAnnotation) {
			return {
				...node,
				typeAnnotation: null
			};
		}
		return node;
	},
	FunctionExpression: remove_this_param,
	FunctionDeclaration: remove_this_param
};
 
/**
 * @template T
 * @param {T} ast
 * @returns {T}
 */
export function remove_typescript_nodes(ast) {
	return walk(ast, null, visitors);
}