Skip to content

Commit

Permalink
Merge pull request #269 from oceanbase/dengfuping-codemod
Browse files Browse the repository at this point in the history
fix(codemod): should import ~@oceanbase/design/es/theme/index.less for existed token
  • Loading branch information
dengfuping authored Nov 7, 2023
2 parents e810269 + 6bd7b5a commit 30b2f58
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@import '~@alipay/ob-ui/es/theme/index.less';

.container {
color: @colorInfo;
background: @colorSuccess;
background-color: @colorWarning;
border-color: @colorError;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import '~@oceanbase/design/es/theme/index.less';
.container {
color: @colorInfo;
background: @colorSuccess;
background-color: @colorWarning;
border-color: @colorError;
}
8 changes: 7 additions & 1 deletion packages/codemod/transforms/__tests__/less-to-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
16 changes: 10 additions & 6 deletions packages/codemod/transforms/less-to-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'",
Expand Down

0 comments on commit 30b2f58

Please sign in to comment.