From f9339c4bb1459c9a9644f861c2ee7efcdcd3221f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 26 Jul 2023 11:36:57 +0800 Subject: [PATCH 1/2] fix: camel case match --- src/Plugin.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Plugin.js b/src/Plugin.js index 8afe921..6c2fed6 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -2,8 +2,11 @@ import { join } from 'path'; import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports'; function transCamel(_str, symbol) { - const str = _str[0].toLowerCase() + _str.substr(1); - return str.replace(/([A-Z])/g, $1 => `${symbol}${$1.toLowerCase()}`); + // e.g. QRCode + // First match: QR + // Second match: Code + const cells = _str.match(/([A-Z]+(?=[A-Z]))|([A-Z][^A-Z]+)/g); + return cells.map(c => c.toLowerCase()).join(symbol); } function winPath(path) { From 85cb5a2454f4e3ce65f9eefdb96e28362fb63214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 26 Jul 2023 11:49:40 +0800 Subject: [PATCH 2/2] test: update test case --- src/Plugin.js | 2 +- test/fixtures/qr-code/actual.js | 8 ++++++++ test/fixtures/qr-code/expected.js | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/qr-code/actual.js create mode 100644 test/fixtures/qr-code/expected.js diff --git a/src/Plugin.js b/src/Plugin.js index 6c2fed6..a74f88f 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -5,7 +5,7 @@ function transCamel(_str, symbol) { // e.g. QRCode // First match: QR // Second match: Code - const cells = _str.match(/([A-Z]+(?=[A-Z]))|([A-Z][^A-Z]+)/g); + const cells = _str.match(/([A-Z]+(?=[A-Z]))|([A-Z]?[^A-Z]+)/g) || []; return cells.map(c => c.toLowerCase()).join(symbol); } diff --git a/test/fixtures/qr-code/actual.js b/test/fixtures/qr-code/actual.js new file mode 100644 index 0000000..62c1d3b --- /dev/null +++ b/test/fixtures/qr-code/actual.js @@ -0,0 +1,8 @@ +import { QRCode } from 'antd'; + +switch(QRCode){ + case QRCode: + console.log('foo'); + default: + console.log('bar') +} diff --git a/test/fixtures/qr-code/expected.js b/test/fixtures/qr-code/expected.js new file mode 100644 index 0000000..1f747ba --- /dev/null +++ b/test/fixtures/qr-code/expected.js @@ -0,0 +1,7 @@ +import _QRCode from "antd/lib/qr-code"; +switch (_QRCode) { + case _QRCode: + console.log('foo'); + default: + console.log('bar'); +} \ No newline at end of file