From 976269d8672f89f035336fbfe4cfdfb5a0b8cc45 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Tue, 29 Nov 2022 21:30:17 +0800 Subject: [PATCH] refactor: tidy up --- src/transform.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/transform.ts b/src/transform.ts index 0bb22d44..01974c1a 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -19,17 +19,12 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => { const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href)) const { type } = parseQuery(search) - // vue files - if (pathname.endsWith('.vue') && (type === 'script' || !search)) { - return true - } - - // js files - if (pathname.match(/\.((c|m)?j|t)sx?$/g)) { - return true - } - - return false + return !!( + ( + (pathname.endsWith('.vue') && (type === 'script' || !search)) || // vue files + pathname.match(/\.((c|m)?j|t)sx?$/g) + ) // js files + ) }, transform(code, id) { if (!code.includes('magic-regexp')) return @@ -62,17 +57,17 @@ export const MagicRegExpTransformPlugin = createUnplugin(() => { walk(this.parse(code), { enter(_node) { - if (_node.type !== 'CallExpression') return const node = _node as SimpleCallExpression if ( + _node.type !== 'CallExpression' || // Normal call - !wrapperNames.includes((node.callee as any).name) && - // Namespaced call - (node.callee.type !== 'MemberExpression' || - node.callee.object.type !== 'Identifier' || - node.callee.object.name !== namespace || - node.callee.property.type !== 'Identifier' || - node.callee.property.name !== 'createRegExp') + (!wrapperNames.includes((node.callee as any).name) && + // Namespaced call + (node.callee.type !== 'MemberExpression' || + node.callee.object.type !== 'Identifier' || + node.callee.object.name !== namespace || + node.callee.property.type !== 'Identifier' || + node.callee.property.name !== 'createRegExp')) ) { return }