diff --git a/README.md b/README.md index cf79b11..6a763d3 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ import { RemoveWrapperFunction } from 'unplugin-ast/transformers' /** * Removes wrapper function. e.g `defineComponent`, `defineConfig`... * @param functionNames - function names to remove + * + * @example defineComponent() + * @example tw`text-red-500 ${expr}` */ declare const RemoveWrapperFunction: ( functionNames: Arrayable, diff --git a/package.json b/package.json index 8e8b48d..cb3f552 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "@babel/generator": "^7.24.5", "@babel/parser": "^7.24.5", "@rollup/pluginutils": "^5.1.0", - "ast-kit": "^1.0.0", + "ast-kit": "^1.3.1", "magic-string-ast": "^0.6.0", "unplugin": "^1.10.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6b23cbb..58b124e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,8 +21,8 @@ importers: specifier: ^5.1.0 version: 5.1.0(rollup@4.17.2) ast-kit: - specifier: ^1.0.0 - version: 1.0.0 + specifier: ^1.3.1 + version: 1.3.1 magic-string-ast: specifier: ^0.6.0 version: 0.6.0 @@ -35,7 +35,7 @@ importers: version: 7.24.5 '@sxzz/eslint-config': specifier: ^4.1.1 - version: 4.1.1(@typescript-eslint/eslint-plugin@7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5) + version: 4.1.1(@typescript-eslint/eslint-plugin@8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5) '@sxzz/prettier-config': specifier: ^2.0.2 version: 2.0.2 @@ -97,10 +97,18 @@ packages: resolution: {integrity: sha512-2ofRCjnnA9y+wk8b9IAREroeUP02KHp431N2mhKniy2yKIDKpbrHv9eXwm8cBeWQYcJmzv5qKCu65P47eCF7CQ==} engines: {node: '>=6.9.0'} + '@babel/helper-string-parser@7.25.9': + resolution: {integrity: sha512-4A/SCr/2KLd5jrtOMFzaKjVtAei3+2r/NChoBNoZ3EyP/+GlhoaEGoWOZUmFmoITP7zOJyHIMm+DYRd8o3PvHA==} + engines: {node: '>=6.9.0'} + '@babel/helper-validator-identifier@7.24.5': resolution: {integrity: sha512-3q93SSKX2TWCG30M2G2kwaKeTYgEUp5Snjuj8qm729SObL6nbtUldAi37qbxkD5gg3xnBio+f9nqpSepGZMvxA==} engines: {node: '>=6.9.0'} + '@babel/helper-validator-identifier@7.25.9': + resolution: {integrity: sha512-Ed61U6XJc3CVRfkERJWDz4dJwKe7iLmmJsbOGu9wSloNSFttHV0I8g6UAgb7qnK5ly5bGLPd4oXZlxCdANBOWQ==} + engines: {node: '>=6.9.0'} + '@babel/highlight@7.24.5': resolution: {integrity: sha512-8lLmua6AVh/8SLJRRVD6V8p73Hir9w5mJrhE+IPpILG31KKlI9iz5zmBYKcWPS59qSfgP9RaSBQSHHE81WKuEw==} engines: {node: '>=6.9.0'} @@ -110,8 +118,8 @@ packages: engines: {node: '>=6.0.0'} hasBin: true - '@babel/parser@7.24.7': - resolution: {integrity: sha512-9uUYRm6OqQrCqQdG1iCBwBPZgN8ciDBro2nIOFaiRz1/BCxaI7CNvQbDHvsArAC7Tw9Hda/B3U+6ui9u4HWXPw==} + '@babel/parser@7.26.2': + resolution: {integrity: sha512-DWMCZH9WA4Maitz2q21SRKHo9QXZxkDsbNZoVD62gusNtNBBqDg9i7uOhASfTfIGNzW+O+r7+jAlM8dwphcJKQ==} engines: {node: '>=6.0.0'} hasBin: true @@ -119,6 +127,10 @@ packages: resolution: {integrity: sha512-6mQNsaLeXTw0nxYUYu+NSa4Hx4BlF1x1x8/PMFbiR+GBSr+2DkECc69b8hgy2frEodNcvPffeH8YfWd3LI6jhQ==} engines: {node: '>=6.9.0'} + '@babel/types@7.26.0': + resolution: {integrity: sha512-Z/yiTPj+lDVnF7lWeKCIJzaIkI0vYO87dMpZ4bg4TDrFe4XXLFWL1TbXU27gBP3QccxV9mZICCrnjnYlJjXHOA==} + engines: {node: '>=6.9.0'} + '@es-joy/jsdoccomment@0.43.1': resolution: {integrity: sha512-I238eDtOolvCuvtxrnqtlBaw0BwdQuYqK7eA6XIonicMdOOOb75mqdIzkGDUbS04+1Di007rgm9snFRNeVrOog==} engines: {node: '>=16'} @@ -773,23 +785,9 @@ packages: '@types/normalize-package-data@2.4.4': resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==} - '@types/semver@7.5.8': - resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==} - '@types/unist@3.0.3': resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==} - '@typescript-eslint/eslint-plugin@7.8.0': - resolution: {integrity: sha512-gFTT+ezJmkwutUPmB0skOj3GZJtlEGnlssems4AjkVweUPGj7jRwwqg0Hhg7++kPGJqKtTYx+R05Ftww372aIg==} - engines: {node: ^18.18.0 || >=20.0.0} - peerDependencies: - '@typescript-eslint/parser': ^7.0.0 - eslint: ^8.56.0 - typescript: '*' - peerDependenciesMeta: - typescript: - optional: true - '@typescript-eslint/eslint-plugin@8.3.0': resolution: {integrity: sha512-FLAIn63G5KH+adZosDYiutqkOkYEx0nvcwNNfJAf+c7Ae/H35qWwTYvPZUKFj5AS+WfHG/WJJfWnDnyNUlp8UA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -811,24 +809,10 @@ packages: typescript: optional: true - '@typescript-eslint/scope-manager@7.8.0': - resolution: {integrity: sha512-viEmZ1LmwsGcnr85gIq+FCYI7nO90DVbE37/ll51hjv9aG+YZMb4WDE2fyWpUR4O/UrhGRpYXK/XajcGTk2B8g==} - engines: {node: ^18.18.0 || >=20.0.0} - '@typescript-eslint/scope-manager@8.3.0': resolution: {integrity: sha512-mz2X8WcN2nVu5Hodku+IR8GgCOl4C0G/Z1ruaWN4dgec64kDBabuXyPAr+/RgJtumv8EEkqIzf3X2U5DUKB2eg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@typescript-eslint/type-utils@7.8.0': - resolution: {integrity: sha512-H70R3AefQDQpz9mGv13Uhi121FNMh+WEaRqcXTX09YEDky21km4dV1ZXJIp8QjXc4ZaVkXVdohvWDzbnbHDS+A==} - engines: {node: ^18.18.0 || >=20.0.0} - peerDependencies: - eslint: ^8.56.0 - typescript: '*' - peerDependenciesMeta: - typescript: - optional: true - '@typescript-eslint/type-utils@8.3.0': resolution: {integrity: sha512-wrV6qh//nLbfXZQoj32EXKmwHf4b7L+xXLrP3FZ0GOUU72gSvLjeWUl5J5Ue5IwRxIV1TfF73j/eaBapxx99Lg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -846,15 +830,6 @@ packages: resolution: {integrity: sha512-y6sSEeK+facMaAyixM36dQ5NVXTnKWunfD1Ft4xraYqxP0lC0POJmIaL/mw72CUMqjY9qfyVfXafMeaUj0noWw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@typescript-eslint/typescript-estree@7.8.0': - resolution: {integrity: sha512-5pfUCOwK5yjPaJQNy44prjCwtr981dO8Qo9J9PwYXZ0MosgAbfEMB008dJ5sNo3+/BN6ytBPuSvXUg9SAqB0dg==} - engines: {node: ^18.18.0 || >=20.0.0} - peerDependencies: - typescript: '*' - peerDependenciesMeta: - typescript: - optional: true - '@typescript-eslint/typescript-estree@8.3.0': resolution: {integrity: sha512-Mq7FTHl0R36EmWlCJWojIC1qn/ZWo2YiWYc1XVtasJ7FIgjo0MVv9rZWXEE7IK2CGrtwe1dVOxWwqXUdNgfRCA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -864,22 +839,12 @@ packages: typescript: optional: true - '@typescript-eslint/utils@7.8.0': - resolution: {integrity: sha512-L0yFqOCflVqXxiZyXrDr80lnahQfSOfc9ELAAZ75sqicqp2i36kEZZGuUymHNFoYOqxRT05up760b4iGsl02nQ==} - engines: {node: ^18.18.0 || >=20.0.0} - peerDependencies: - eslint: ^8.56.0 - '@typescript-eslint/utils@8.3.0': resolution: {integrity: sha512-F77WwqxIi/qGkIGOGXNBLV7nykwfjLsdauRB/DOFPdv6LTF3BHHkBpq81/b5iMPSF055oO2BiivDJV4ChvNtXA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: ^8.57.0 || ^9.0.0 - '@typescript-eslint/visitor-keys@7.8.0': - resolution: {integrity: sha512-q4/gibTNBQNA0lGyYQCmWRS5D15n8rXh4QjK3KV+MBPlTYHpfBUT3D3PaPR/HeNiI9W6R7FvlkcGhNyAoP+caA==} - engines: {node: ^18.18.0 || >=20.0.0} - '@typescript-eslint/visitor-keys@8.3.0': resolution: {integrity: sha512-RmZwrTbQ9QveF15m/Cl28n0LXD6ea2CjkhH5rQ55ewz3H24w+AMCJHPVYaZ8/0HoG8Z3cLLFFycRXxeO2tz9FA==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -973,8 +938,8 @@ packages: resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==} engines: {node: '>=12'} - ast-kit@1.0.0: - resolution: {integrity: sha512-Jv5Zs4DhU4QEYPvfVrEmdMuxCRMxsIVNfj4uqsBWyNM5wOaNMIfOwu55jH2DWnmr05iyCxPjbYGND1PNU40CuQ==} + ast-kit@1.3.1: + resolution: {integrity: sha512-3bIRV4s/cNAee2rKjuvYdoG+0CMqtOIgCvWrJL6zG8R0fDyMwYzStspX5JqXPbdMzM+qxHZ6g2rMHKhr3HkPlQ==} engines: {node: '>=16.14.0'} balanced-match@1.0.2: @@ -2675,8 +2640,12 @@ snapshots: '@babel/helper-string-parser@7.24.1': {} + '@babel/helper-string-parser@7.25.9': {} + '@babel/helper-validator-identifier@7.24.5': {} + '@babel/helper-validator-identifier@7.25.9': {} + '@babel/highlight@7.24.5': dependencies: '@babel/helper-validator-identifier': 7.24.5 @@ -2688,9 +2657,9 @@ snapshots: dependencies: '@babel/types': 7.24.5 - '@babel/parser@7.24.7': + '@babel/parser@7.26.2': dependencies: - '@babel/types': 7.24.5 + '@babel/types': 7.26.0 '@babel/types@7.24.5': dependencies: @@ -2698,6 +2667,11 @@ snapshots: '@babel/helper-validator-identifier': 7.24.5 to-fast-properties: 2.0.0 + '@babel/types@7.26.0': + dependencies: + '@babel/helper-string-parser': 7.25.9 + '@babel/helper-validator-identifier': 7.25.9 + '@es-joy/jsdoccomment@0.43.1': dependencies: '@types/eslint': 8.56.12 @@ -3081,7 +3055,7 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.17.2': optional: true - '@sxzz/eslint-config@4.1.1(@typescript-eslint/eslint-plugin@7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5)': + '@sxzz/eslint-config@4.1.1(@typescript-eslint/eslint-plugin@8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5)': dependencies: '@eslint/js': 9.9.1 '@eslint/markdown': 6.0.0(eslint@9.1.1) @@ -3101,7 +3075,7 @@ snapshots: eslint-plugin-prettier: 5.2.1(@types/eslint@9.6.1)(eslint-config-prettier@9.1.0(eslint@9.1.1))(eslint@9.1.1)(prettier@3.3.3) eslint-plugin-regexp: 2.6.0(eslint@9.1.1) eslint-plugin-unicorn: 55.0.0(eslint@9.1.1) - eslint-plugin-unused-imports: 4.1.3(@typescript-eslint/eslint-plugin@7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1) + eslint-plugin-unused-imports: 4.1.3(@typescript-eslint/eslint-plugin@8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1) eslint-plugin-vue: 9.27.0(eslint@9.1.1) eslint-plugin-yml: 1.14.0(eslint@9.1.1) globals: 15.9.0 @@ -3155,32 +3129,8 @@ snapshots: '@types/normalize-package-data@2.4.4': {} - '@types/semver@7.5.8': - optional: true - '@types/unist@3.0.3': {} - '@typescript-eslint/eslint-plugin@7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5)': - dependencies: - '@eslint-community/regexpp': 4.10.0 - '@typescript-eslint/parser': 8.3.0(eslint@9.1.1)(typescript@5.4.5) - '@typescript-eslint/scope-manager': 7.8.0 - '@typescript-eslint/type-utils': 7.8.0(eslint@9.1.1)(typescript@5.4.5) - '@typescript-eslint/utils': 7.8.0(eslint@9.1.1)(typescript@5.4.5) - '@typescript-eslint/visitor-keys': 7.8.0 - debug: 4.3.5 - eslint: 9.1.1 - graphemer: 1.4.0 - ignore: 5.3.1 - natural-compare: 1.4.0 - semver: 7.6.0 - ts-api-utils: 1.3.0(typescript@5.4.5) - optionalDependencies: - typescript: 5.4.5 - transitivePeerDependencies: - - supports-color - optional: true - '@typescript-eslint/eslint-plugin@8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5)': dependencies: '@eslint-community/regexpp': 4.10.0 @@ -3212,30 +3162,11 @@ snapshots: transitivePeerDependencies: - supports-color - '@typescript-eslint/scope-manager@7.8.0': - dependencies: - '@typescript-eslint/types': 7.8.0 - '@typescript-eslint/visitor-keys': 7.8.0 - optional: true - '@typescript-eslint/scope-manager@8.3.0': dependencies: '@typescript-eslint/types': 8.3.0 '@typescript-eslint/visitor-keys': 8.3.0 - '@typescript-eslint/type-utils@7.8.0(eslint@9.1.1)(typescript@5.4.5)': - dependencies: - '@typescript-eslint/typescript-estree': 7.8.0(typescript@5.4.5) - '@typescript-eslint/utils': 7.8.0(eslint@9.1.1)(typescript@5.4.5) - debug: 4.3.5 - eslint: 9.1.1 - ts-api-utils: 1.3.0(typescript@5.4.5) - optionalDependencies: - typescript: 5.4.5 - transitivePeerDependencies: - - supports-color - optional: true - '@typescript-eslint/type-utils@8.3.0(eslint@9.1.1)(typescript@5.4.5)': dependencies: '@typescript-eslint/typescript-estree': 8.3.0(typescript@5.4.5) @@ -3252,22 +3183,6 @@ snapshots: '@typescript-eslint/types@8.3.0': {} - '@typescript-eslint/typescript-estree@7.8.0(typescript@5.4.5)': - dependencies: - '@typescript-eslint/types': 7.8.0 - '@typescript-eslint/visitor-keys': 7.8.0 - debug: 4.3.5 - globby: 11.1.0 - is-glob: 4.0.3 - minimatch: 9.0.4 - semver: 7.6.0 - ts-api-utils: 1.3.0(typescript@5.4.5) - optionalDependencies: - typescript: 5.4.5 - transitivePeerDependencies: - - supports-color - optional: true - '@typescript-eslint/typescript-estree@8.3.0(typescript@5.4.5)': dependencies: '@typescript-eslint/types': 8.3.0 @@ -3283,21 +3198,6 @@ snapshots: transitivePeerDependencies: - supports-color - '@typescript-eslint/utils@7.8.0(eslint@9.1.1)(typescript@5.4.5)': - dependencies: - '@eslint-community/eslint-utils': 4.4.0(eslint@9.1.1) - '@types/json-schema': 7.0.15 - '@types/semver': 7.5.8 - '@typescript-eslint/scope-manager': 7.8.0 - '@typescript-eslint/types': 7.8.0 - '@typescript-eslint/typescript-estree': 7.8.0(typescript@5.4.5) - eslint: 9.1.1 - semver: 7.6.0 - transitivePeerDependencies: - - supports-color - - typescript - optional: true - '@typescript-eslint/utils@8.3.0(eslint@9.1.1)(typescript@5.4.5)': dependencies: '@eslint-community/eslint-utils': 4.4.0(eslint@9.1.1) @@ -3309,12 +3209,6 @@ snapshots: - supports-color - typescript - '@typescript-eslint/visitor-keys@7.8.0': - dependencies: - '@typescript-eslint/types': 7.8.0 - eslint-visitor-keys: 3.4.3 - optional: true - '@typescript-eslint/visitor-keys@8.3.0': dependencies: '@typescript-eslint/types': 8.3.0 @@ -3422,9 +3316,9 @@ snapshots: assertion-error@2.0.1: {} - ast-kit@1.0.0: + ast-kit@1.3.1: dependencies: - '@babel/parser': 7.24.7 + '@babel/parser': 7.26.2 pathe: 1.1.2 balanced-match@1.0.2: {} @@ -3890,11 +3784,11 @@ snapshots: semver: 7.6.3 strip-indent: 3.0.0 - eslint-plugin-unused-imports@4.1.3(@typescript-eslint/eslint-plugin@7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1): + eslint-plugin-unused-imports@4.1.3(@typescript-eslint/eslint-plugin@8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1): dependencies: eslint: 9.1.1 optionalDependencies: - '@typescript-eslint/eslint-plugin': 7.8.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5) + '@typescript-eslint/eslint-plugin': 8.3.0(@typescript-eslint/parser@8.3.0(eslint@9.1.1)(typescript@5.4.5))(eslint@9.1.1)(typescript@5.4.5) eslint-plugin-vue@9.27.0(eslint@9.1.1): dependencies: diff --git a/src/core/transformers/remove-wrapper-function.ts b/src/core/transformers/remove-wrapper-function.ts index ccaea82..e35ca3a 100644 --- a/src/core/transformers/remove-wrapper-function.ts +++ b/src/core/transformers/remove-wrapper-function.ts @@ -1,5 +1,6 @@ import { type Arrayable, toArray } from '@antfu/utils' -import type { CallExpression } from '@babel/types' +import { isCallOf, isTaggedFunctionCallOf } from 'ast-kit' +import type { CallExpression, TaggedTemplateExpression } from '@babel/types' import type { Transformer } from '../types' /** @@ -9,13 +10,13 @@ import type { Transformer } from '../types' */ export const RemoveWrapperFunction = ( functionNames: Arrayable, -): Transformer => ({ +): Transformer => ({ onNode: (node) => - node.type === 'CallExpression' && - node.callee.type === 'Identifier' && - toArray(functionNames).includes(node.callee.name), + isCallOf(node, toArray(functionNames)) || + isTaggedFunctionCallOf(node, toArray(functionNames)), transform(node) { + if (node.type === 'TaggedTemplateExpression') return node.quasi return node.arguments[0] }, }) diff --git a/tests/remove-wrapper-function.test.ts b/tests/remove-wrapper-function.test.ts index 67e24d7..7e4b754 100644 --- a/tests/remove-wrapper-function.test.ts +++ b/tests/remove-wrapper-function.test.ts @@ -11,10 +11,18 @@ test('remove wrapper function', async () => { }) console.log(mutable({} as const)) console.log(() => mutable({} as const)) + + const css = tw\`text-center \${expr}\` ` + const options: Pick = { transformer: [ - RemoveWrapperFunction(['defineComponent', 'mutable', 'definePropType']), + RemoveWrapperFunction([ + 'defineComponent', + 'mutable', + 'definePropType', + 'tw', + ]), ], parserOptions: {}, } @@ -27,6 +35,8 @@ test('remove wrapper function', async () => { }) console.log(({} as const)) console.log(() => ({} as const)) + + const css = \`text-center \${expr}\` " `) })