From 8f7f9a63811d371820b31620359ec3e3737d8d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Wed, 26 Jul 2023 16:53:24 +0800 Subject: [PATCH] Revert "Revert "fix: camel case match (#632)" (#634)" This reverts commit 33b05c931a1064c43d0489caf4de9cfeba042497. --- src/Plugin.js | 7 +++++-- test/fixtures/qr-code/actual.js | 8 ++++++++ test/fixtures/qr-code/expected.js | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) 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 8afe921..a74f88f 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) { 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