-
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.
- Loading branch information
Showing
12 changed files
with
159 additions
and
56 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
transforms/__testfixtures__/v3-LocaleProvider-to-v4-ConfigProvider/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,9 @@ | ||
import { LocaleProvider as MyProvider } from 'antd'; | ||
|
||
const App = () => { | ||
return ( | ||
<MyProvider {...yourConfig}> | ||
<Main /> | ||
</MyProvider> | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
transforms/__testfixtures__/v3-LocaleProvider-to-v4-ConfigProvider/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,9 @@ | ||
import { ConfigProvider as MyProvider } from 'antd'; | ||
|
||
const App = () => { | ||
return ( | ||
<MyProvider {...yourConfig}> | ||
<Main /> | ||
</MyProvider> | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
transforms/__testfixtures__/v3-LocaleProvider-to-v4-ConfigProvider/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,9 @@ | ||
import { LocaleProvider } from 'antd'; | ||
|
||
const App = () => { | ||
return ( | ||
<LocaleProvider {...yourConfig}> | ||
<Main /> | ||
</LocaleProvider> | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
transforms/__testfixtures__/v3-LocaleProvider-to-v4-ConfigProvider/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,9 @@ | ||
import { ConfigProvider } from 'antd'; | ||
|
||
const App = () => { | ||
return ( | ||
<ConfigProvider {...yourConfig}> | ||
<Main /> | ||
</ConfigProvider> | ||
); | ||
} |
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
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
11 changes: 11 additions & 0 deletions
11
transforms/__tests__/v3-LocaleProvider-to-v4-ConfigProvider.test.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,11 @@ | ||
const tests = ['basic', 'alias-import']; | ||
|
||
const defineTest = require('jscodeshift/dist/testUtils').defineTest; | ||
|
||
const testUnit = 'v3-LocaleProvider-to-v4-ConfigProvider'; | ||
|
||
describe(testUnit, () => { | ||
tests.forEach(test => | ||
defineTest(__dirname, testUnit, null, `${testUnit}/${test}`), | ||
); | ||
}); |
File renamed without changes.
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
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
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
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,65 @@ | ||
const { printOptions } = require('./utils/config'); | ||
const { removeEmptyModuleImport, addSubmoduleImport } = require('./utils'); | ||
|
||
module.exports = (file, api, options) => { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
let localComponentName = 'LocaleProvider'; | ||
|
||
// remove old LocaleProvider imports | ||
function removeAntdLocaleProviderImport(j, root) { | ||
let hasChanged = false; | ||
|
||
// import { LocaleProvider } from 'antd'; | ||
root | ||
.find(j.Identifier) | ||
.filter( | ||
path => | ||
path.node.name === 'LocaleProvider' && | ||
path.parent.node.type === 'ImportSpecifier' && | ||
path.parent.parent.node.source.value === 'antd', | ||
) | ||
.forEach(path => { | ||
hasChanged = true; | ||
localComponentName = path.parent.node.local.name; | ||
|
||
const importDeclaration = path.parent.parent.node; | ||
importDeclaration.specifiers = importDeclaration.specifiers.filter( | ||
specifier => | ||
!specifier.imported || specifier.imported.name !== 'LocaleProvider', | ||
); | ||
}); | ||
|
||
return hasChanged; | ||
} | ||
|
||
// step1. remove LocaleProvider import from antd | ||
// step2. add ConfigProvider import from antd | ||
// step3. cleanup antd import if empty | ||
let hasChanged = false; | ||
hasChanged = removeAntdLocaleProviderImport(j, root) || hasChanged; | ||
|
||
if (hasChanged) { | ||
if (localComponentName === 'LocaleProvider') { | ||
root | ||
.findJSXElements(localComponentName) | ||
.find(j.JSXIdentifier, { | ||
name: localComponentName, | ||
}) | ||
.forEach(nodePath => { | ||
nodePath.node.name = 'ConfigProvider'; | ||
|
||
addSubmoduleImport(j, root, 'antd', 'ConfigProvider'); | ||
}); | ||
} else { | ||
addSubmoduleImport(j, root, 'antd', 'ConfigProvider', localComponentName); | ||
} | ||
|
||
removeEmptyModuleImport(j, root, 'antd'); | ||
} | ||
|
||
return hasChanged | ||
? root.toSource(options.printOptions || printOptions) | ||
: null; | ||
}; |