Skip to content

Commit

Permalink
improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Dec 9, 2023
1 parent a75ec5e commit 9b3d27f
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions plugins/remove-legacy-layout.ts
Original file line number Diff line number Diff line change
@@ -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<Program>, state: State) {
if (state.shouldAppendLayout) {
path.node.body.push(
t.variableDeclaration('var', [
Expand All @@ -25,7 +42,7 @@ export function removeLegacyLayout(addons: string[]) {
}
},
},
ImportDeclaration(path, state) {
ImportDeclaration(path: NodePath<ImportDeclaration>, state: State) {
if (!shouldContinue(state)) {
return;
}
Expand All @@ -46,21 +63,33 @@ export function removeLegacyLayout(addons: string[]) {
state.shouldAppendLayout = true;
}
},
ObjectProperty(path, state) {
ObjectProperty(path: NodePath<ObjectProperty>, 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'
) {
path.remove();
}
},
ClassProperty(path, state) {
ClassProperty(path: NodePath<ClassProperty>, 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' &&
Expand All @@ -87,6 +116,5 @@ export function removeLegacyLayout(addons: string[]) {
// },
},
};
}

}
};
}

0 comments on commit 9b3d27f

Please sign in to comment.