From 9b3d27fe980672fc62a4113febe2f8289c10c431 Mon Sep 17 00:00:00 2001 From: Alex Kanunnikov Date: Sat, 9 Dec 2023 21:17:25 +0300 Subject: [PATCH] improve types --- plugins/remove-legacy-layout.ts | 48 ++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/plugins/remove-legacy-layout.ts b/plugins/remove-legacy-layout.ts index c3303d6..02b66a7 100644 --- a/plugins/remove-legacy-layout.ts +++ b/plugins/remove-legacy-layout.ts @@ -1,18 +1,35 @@ +import type { NodePath } from '@babel/traverse'; +import type { + Program, + ImportDeclaration, + ObjectProperty, + ClassProperty, +} from '@babel/types'; +import type * as b from '@babel/types'; + export function removeLegacyLayout(addons: string[]) { - return function removeLegacyLayoutPlugin (babel) { - const shouldContinue = (state) => { + return function removeLegacyLayoutPlugin(babel: any) { + type State = { + file: { + opts: { + filename: string; + }; + }; + shouldAppendLayout?: boolean; + }; + const shouldContinue = (state: State) => { const fName = state.file.opts.filename; return ( fName.includes('node_modules') && addons.some((el) => fName.includes(`/${el}/`)) ); }; - const { types: t } = babel; + const { types: t } = babel as { types: typeof b }; return { name: 'remove-layout', visitor: { Program: { - exit(path, state) { + exit(path: NodePath, state: State) { if (state.shouldAppendLayout) { path.node.body.push( t.variableDeclaration('var', [ @@ -25,7 +42,7 @@ export function removeLegacyLayout(addons: string[]) { } }, }, - ImportDeclaration(path, state) { + ImportDeclaration(path: NodePath, state: State) { if (!shouldContinue(state)) { return; } @@ -46,10 +63,16 @@ export function removeLegacyLayout(addons: string[]) { state.shouldAppendLayout = true; } }, - ObjectProperty(path, state) { + ObjectProperty(path: NodePath, state: State) { if (!shouldContinue(state)) { return; } + if ( + !t.isIdentifier(path.node.key) || + !t.isIdentifier(path.node.value) + ) { + return; + } if ( path.node.value.name === 'layout' && path.node.key.name === 'layout' @@ -57,10 +80,16 @@ export function removeLegacyLayout(addons: string[]) { path.remove(); } }, - ClassProperty(path, state) { + ClassProperty(path: NodePath, state: State) { if (!shouldContinue(state)) { return; } + if ( + !t.isIdentifier(path.node.key) || + !t.isIdentifier(path.node.value) + ) { + return; + } if ( path.node.value && path.node.value.name === 'layout' && @@ -87,6 +116,5 @@ export function removeLegacyLayout(addons: string[]) { // }, }, }; - } - -} \ No newline at end of file + }; +}