-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support v4 icon without outlined (#4)
- Loading branch information
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
transforms/__testfixtures__/v4-Icon-Outlined/alias-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Smile as SmileO, SmileFilled, SmileTwoTone } from '@ant-design/icons'; | ||
|
||
const Component = () => { | ||
return <SmileO />; | ||
}; | ||
|
||
const Component1 = props => { | ||
return <SmileFilled />; | ||
}; | ||
|
||
const Component2 = props => { | ||
return <SmileTwoTone />; | ||
}; |
13 changes: 13 additions & 0 deletions
13
transforms/__testfixtures__/v4-Icon-Outlined/alias-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SmileFilled, SmileOutlined as SmileO, SmileTwoTone } from '@ant-design/icons'; | ||
|
||
const Component = () => { | ||
return <SmileO />; | ||
}; | ||
|
||
const Component1 = props => { | ||
return <SmileFilled />; | ||
}; | ||
|
||
const Component2 = props => { | ||
return <SmileTwoTone />; | ||
}; |
13 changes: 13 additions & 0 deletions
13
transforms/__testfixtures__/v4-Icon-Outlined/basic.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Smile, SmileFilled, SmileTwoTone } from '@ant-design/icons'; | ||
|
||
const Component = () => { | ||
return <Smile />; | ||
}; | ||
|
||
const Component1 = props => { | ||
return <SmileFilled />; | ||
}; | ||
|
||
const Component2 = props => { | ||
return <SmileTwoTone />; | ||
}; |
13 changes: 13 additions & 0 deletions
13
transforms/__testfixtures__/v4-Icon-Outlined/basic.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SmileFilled, SmileOutlined, SmileTwoTone } from '@ant-design/icons'; | ||
|
||
const Component = () => { | ||
return <SmileOutlined />; | ||
}; | ||
|
||
const Component1 = props => { | ||
return <SmileFilled />; | ||
}; | ||
|
||
const Component2 = props => { | ||
return <SmileTwoTone />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const tests = ['basic', 'alias-import']; | ||
|
||
const defineTest = require('jscodeshift/dist/testUtils').defineTest; | ||
|
||
const testUnit = 'v4-Icon-Outlined'; | ||
|
||
describe(testUnit, () => { | ||
tests.forEach(test => | ||
defineTest(__dirname, testUnit, null, `${testUnit}/${test}`), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const allIcons = require('@ant-design/icons/lib/icons'); | ||
|
||
const { printOptions } = require('./utils/config'); | ||
const { removeEmptyModuleImport, addSubmoduleImport } = require('./utils'); | ||
|
||
const outlinedIcons = Object.keys(allIcons) | ||
.filter(n => n.endsWith('Outlined')) | ||
.map(n => n.replace(/Outlined$/, '')); | ||
|
||
module.exports = (file, api, options) => { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
// remove old LocaleProvider imports | ||
function renameV4IconWithoutOutlinedImport(j, root) { | ||
let hasChanged = false; | ||
|
||
// import { LocaleProvider } from 'antd'; | ||
root | ||
.find(j.Identifier) | ||
.filter( | ||
path => | ||
outlinedIcons.includes(path.node.name) && | ||
path.parent.node.type === 'ImportSpecifier' && | ||
path.parent.parent.node.source.value === '@ant-design/icons', | ||
) | ||
.forEach(path => { | ||
hasChanged = true; | ||
const localComponentName = path.parent.node.local.name; | ||
const importComponentName = path.parent.node.imported.name; | ||
|
||
const importDeclaration = path.parent.parent.node; | ||
importDeclaration.specifiers = importDeclaration.specifiers.filter( | ||
specifier => | ||
!specifier.imported || | ||
specifier.imported.name !== importComponentName, | ||
); | ||
|
||
const outlinedIconName = importComponentName + 'Outlined'; | ||
|
||
if (localComponentName === importComponentName) { | ||
addSubmoduleImport(j, root, '@ant-design/icons', outlinedIconName); | ||
if (localComponentName === importComponentName) { | ||
root | ||
.findJSXElements(localComponentName) | ||
.find(j.JSXIdentifier, { | ||
name: localComponentName, | ||
}) | ||
.forEach(nodePath => { | ||
nodePath.node.name = outlinedIconName; | ||
}); | ||
} | ||
} else { | ||
addSubmoduleImport( | ||
j, | ||
root, | ||
'@ant-design/icons', | ||
outlinedIconName, | ||
localComponentName, | ||
); | ||
} | ||
}); | ||
|
||
return hasChanged; | ||
} | ||
|
||
// step1. remove LocaleProvider import from antd | ||
// step2. add ConfigProvider import from antd | ||
// step3. cleanup antd import if empty | ||
let hasChanged = false; | ||
hasChanged = renameV4IconWithoutOutlinedImport(j, root) || hasChanged; | ||
|
||
if (hasChanged) { | ||
removeEmptyModuleImport(j, root, 'antd'); | ||
} | ||
|
||
return hasChanged | ||
? root.toSource(options.printOptions || printOptions) | ||
: null; | ||
}; |