From 6bd7b5a902838a501bef7578d0f3a46e22efb36d Mon Sep 17 00:00:00 2001 From: dengfuping Date: Tue, 7 Nov 2023 10:52:04 +0800 Subject: [PATCH] fix(codemod): should import ~@oceanbase/design/es/theme/index.less for existed token --- .../obui-less-token-to-token.input.less | 8 ++++++++ .../obui-less-token-to-token.output.less | 7 +++++++ .../transforms/__tests__/less-to-token.test.ts | 8 +++++++- packages/codemod/transforms/less-to-token.js | 16 ++++++++++------ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.input.less create mode 100644 packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.output.less diff --git a/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.input.less b/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.input.less new file mode 100644 index 000000000..0c1b439e0 --- /dev/null +++ b/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.input.less @@ -0,0 +1,8 @@ +@import '~@alipay/ob-ui/es/theme/index.less'; + +.container { + color: @colorInfo; + background: @colorSuccess; + background-color: @colorWarning; + border-color: @colorError; +} diff --git a/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.output.less b/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.output.less new file mode 100644 index 000000000..9d654fd79 --- /dev/null +++ b/packages/codemod/transforms/__testfixtures__/less-to-token/obui-less-token-to-token.output.less @@ -0,0 +1,7 @@ +@import '~@oceanbase/design/es/theme/index.less'; +.container { + color: @colorInfo; + background: @colorSuccess; + background-color: @colorWarning; + border-color: @colorError; +} diff --git a/packages/codemod/transforms/__tests__/less-to-token.test.ts b/packages/codemod/transforms/__tests__/less-to-token.test.ts index 842b27bc5..32f91be98 100644 --- a/packages/codemod/transforms/__tests__/less-to-token.test.ts +++ b/packages/codemod/transforms/__tests__/less-to-token.test.ts @@ -3,7 +3,13 @@ import path from 'path'; import { transform } from '../less-to-token'; const testUnit = 'less-to-token'; -const tests = ['antd-v4-less-to-token', 'obui-less-to-token', 'case-insensitive', 'mixin']; +const tests = [ + 'antd-v4-less-to-token', + 'obui-less-to-token', + 'case-insensitive', + 'mixin', + 'obui-less-token-to-token', +]; describe(testUnit, () => { tests.forEach(test => { diff --git a/packages/codemod/transforms/less-to-token.js b/packages/codemod/transforms/less-to-token.js index cd474a88e..7ca6adefa 100644 --- a/packages/codemod/transforms/less-to-token.js +++ b/packages/codemod/transforms/less-to-token.js @@ -37,14 +37,18 @@ async function transform(file) { const { root: ast } = await postcss([]).process(content, { syntax: postcssLess, }); - let modified = false; + let hasToken = false; let tokenLessImported = false; // 遍历 AST ast.walk(node => { - const { key, token, formattedValue } = tokenParse(node.value); - if (node.type === 'decl' && token) { - node.value = formattedValue.replace(key, `@${token}`); - modified = true; + if (node.type === 'decl') { + const { key, token, formattedValue } = tokenParse(node.value); + if (token) { + node.value = formattedValue.replace(key, `@${token}`); + hasToken = true; + } else if (node.value?.includes('@')) { + hasToken = true; + } } else if (node.type === 'atrule' && node.name === 'import') { if (node.params === "'~@oceanbase/design/es/theme/index.less'") { tokenLessImported = true; @@ -54,7 +58,7 @@ async function transform(file) { } }); // prepend @import '~@oceanbase/design/es/theme/index.less'; - if (modified && !tokenLessImported) { + if (hasToken && !tokenLessImported) { ast.prepend({ name: 'import', params: "'~@oceanbase/design/es/theme/index.less'",