Skip to content

Commit 0fd89bd

Browse files
committed
feat: use @babel/types for node type judgment
1 parent 108ffc4 commit 0fd89bd

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

packages/tailwindcss-patch/src/inspector.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export function inspectProcessTailwindFeaturesReturnContext(content: string) {
99
FunctionDeclaration(p) {
1010
const n = p.node
1111
if (n.id?.name === 'processTailwindFeatures') {
12-
if (n.body.body.length === 1 && n.body.body[0].type === 'ReturnStatement') {
12+
if (n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
1313
const rts = n.body.body[0]
1414
if (t.isFunctionExpression(rts.argument)) {
1515
const body = rts.argument.body.body
1616
const lastStatement = body[body.length - 1]
17-
hasPatched = lastStatement.type === 'ReturnStatement' && lastStatement.argument?.type === 'Identifier' && lastStatement.argument.name === 'context'
17+
hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === 'context'
1818
if (!hasPatched) {
1919
// return context;
2020
const rts = t.returnStatement(t.identifier('context'))
@@ -41,28 +41,30 @@ export function inspectPostcssPlugin(content: string) {
4141
traverse(ast, {
4242
Program(p) {
4343
const n = p.node
44+
// find module.exports = function tailwindcss(configOrPath)
4445
const idx = n.body.findIndex((x) => {
4546
return (
46-
x.type === 'ExpressionStatement' &&
47-
x.expression.type === 'AssignmentExpression' &&
48-
x.expression.left.type === 'MemberExpression' &&
49-
x.expression.right.type === 'FunctionExpression' &&
47+
t.isExpressionStatement(x) &&
48+
t.isAssignmentExpression(x.expression) &&
49+
t.isMemberExpression(x.expression.left) &&
50+
t.isFunctionExpression(x.expression.right) &&
5051
x.expression.right.id?.name === 'tailwindcss'
5152
)
5253
})
54+
5355
if (idx > -1) {
5456
const prevStatement = n.body[idx - 1]
5557
const lastStatement = n.body[n.body.length - 1]
5658
const hasPatchedCondition0 =
5759
prevStatement &&
58-
prevStatement.type === 'VariableDeclaration' &&
60+
t.isVariableDeclaration(prevStatement) &&
5961
prevStatement.declarations.length === 1 &&
60-
prevStatement.declarations[0].id.type === 'Identifier' &&
62+
t.isIdentifier(prevStatement.declarations[0].id) &&
6163
prevStatement.declarations[0].id.name === variableName
6264
const hasPatchedCondition1 =
63-
lastStatement.type === 'ExpressionStatement' &&
64-
lastStatement.expression.type === 'AssignmentExpression' &&
65-
lastStatement.expression.right.type === 'Identifier' &&
65+
t.isExpressionStatement(lastStatement) &&
66+
t.isAssignmentExpression(lastStatement.expression) &&
67+
t.isIdentifier(lastStatement.expression.right) &&
6668
lastStatement.expression.right.name === variableName
6769

6870
hasPatched = hasPatchedCondition0 || hasPatchedCondition1
@@ -93,26 +95,26 @@ export function inspectPostcssPlugin(content: string) {
9395
}
9496
const n = p.node
9597
if (n.id?.name === 'tailwindcss') {
96-
if (n.body.body.length === 1 && n.body.body[0].type === 'ReturnStatement') {
98+
if (n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
9799
const returnStatement = n.body.body[0]
98-
if (returnStatement.argument?.type === 'ObjectExpression' && returnStatement.argument.properties.length === 2) {
100+
if (t.isObjectExpression(returnStatement.argument) && returnStatement.argument.properties.length === 2) {
99101
const properties = returnStatement.argument.properties
100-
if (properties[0].type === 'ObjectProperty' && properties[1].type === 'ObjectProperty') {
101-
const keyMatched = properties[0].key.type === 'Identifier' && properties[0].key.name === 'postcssPlugin'
102-
const pluginsMatched = properties[1].key.type === 'Identifier' && properties[1].key.name === 'plugins'
102+
if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) {
103+
const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === 'postcssPlugin'
104+
const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === 'plugins'
103105
if (
104106
pluginsMatched &&
105107
keyMatched &&
106-
properties[1].value.type === 'CallExpression' &&
107-
properties[1].value.callee.type === 'MemberExpression' &&
108-
properties[1].value.callee.object.type === 'ArrayExpression'
108+
t.isCallExpression(properties[1].value) &&
109+
t.isMemberExpression(properties[1].value.callee) &&
110+
t.isArrayExpression(properties[1].value.callee.object)
109111
) {
110112
const pluginsCode = properties[1].value.callee.object.elements
111-
if (pluginsCode[1] && pluginsCode[1].type === 'FunctionExpression') {
113+
if (pluginsCode[1] && t.isFunctionExpression(pluginsCode[1])) {
112114
const targetBlockStatement = pluginsCode[1].body
113115

114116
const lastStatement = targetBlockStatement.body[targetBlockStatement.body.length - 1]
115-
if (lastStatement.type === 'ExpressionStatement') {
117+
if (t.isExpressionStatement(lastStatement)) {
116118
// contextRef.value.push((0, _processTailwindFeatures.default)(context)(root, result));
117119
const newExpressionStatement = t.expressionStatement(
118120
t.callExpression(
@@ -128,14 +130,14 @@ export function inspectPostcssPlugin(content: string) {
128130
targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement
129131
}
130132

131-
const ifIdx = targetBlockStatement.body.findIndex((x) => x.type === 'IfStatement')
133+
const ifIdx = targetBlockStatement.body.findIndex((x) => t.isIfStatement(x))
132134
if (ifIdx > -1) {
133135
const ifRoot = <t.IfStatement>targetBlockStatement.body[ifIdx]
134-
if (ifRoot.consequent.type === 'BlockStatement' && ifRoot.consequent.body[1] && ifRoot.consequent.body[1].type === 'ForOfStatement') {
136+
if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) {
135137
const forOf: t.ForOfStatement = ifRoot.consequent.body[1]
136-
if (forOf.body.type === 'BlockStatement' && forOf.body.body.length === 1 && forOf.body.body[0].type === 'IfStatement') {
138+
if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) {
137139
const if2: t.IfStatement = forOf.body.body[0]
138-
if (if2.consequent.type === 'BlockStatement' && if2.consequent.body.length === 1 && if2.consequent.body[0].type === 'ExpressionStatement') {
140+
if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) {
139141
const target = if2.consequent.body[0]
140142
// contextRef.value.push((0, _processTailwindFeatures.default)(context)(root1, result));
141143
const newExpressionStatement = t.expressionStatement(

packages/tailwindcss-patch/test/inspector.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import fs from 'fs'
33
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '../src/inspector'
44
const tailwindcssCasePath = path.resolve(__dirname, 'fixtures')
5-
const twltsLibPath = path.resolve(tailwindcssCasePath, 'versions/lts/lib')
5+
const twltsLibPath = path.resolve(tailwindcssCasePath, 'versions/3.3.1/lib')
66

77
describe('inspector', () => {
88
it('inspectPostcssPlugin patch snap', () => {

0 commit comments

Comments
 (0)