-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(project): sort prop types in alphabetical order (#6620)
* feat(eslint-config-carbon): enable sort rule * feat(project): add codemods package * refactor(project): sort prop types in alphabetical order * chore(project): sync offline mirror * chore(project): update lint command * chore(project): remove redundant lockfile * chore(colors): remove scss from source control * refactor(codemods): update from switch statement Co-authored-by: TJ Egan <[email protected]>
- Loading branch information
Showing
150 changed files
with
2,246 additions
and
2,653 deletions.
There are no files selected for viewing
Binary file added
BIN
+1.74 KB
.yarn/offline-mirror/@babel-helper-skip-transparent-expression-wrappers-7.11.0.tgz
Binary file not shown.
Binary file added
BIN
+1.86 KB
.yarn/offline-mirror/@babel-plugin-proposal-nullish-coalescing-operator-7.10.4.tgz
Binary file not shown.
Binary file added
BIN
+2.76 KB
.yarn/offline-mirror/@babel-plugin-proposal-optional-chaining-7.11.0.tgz
Binary file not shown.
Binary file added
BIN
+3.21 KB
.yarn/offline-mirror/@babel-plugin-transform-modules-commonjs-7.10.4.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,4 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
**/examples/** | ||
**/tasks/** |
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,43 @@ | ||
# @carbon/codemods | ||
|
||
> An internal package for codemods for the Carbon Design System | ||
## Usage | ||
|
||
You can run a specific transform for a path in the project by running: | ||
|
||
```bash | ||
yarn jscodeshift -t codemods/transform/<name>.js 'path/to/file' | ||
``` | ||
|
||
If you want to preview changes that are being made, you should enable the `dry` | ||
and `print` options: | ||
|
||
```bash | ||
yarn jscodeshift -d -p -t codemods/transform/<name>.js 'path/to/file' | ||
``` | ||
|
||
## Writing transforms | ||
|
||
Transforms are written in the `transforms` directory with their tests and | ||
fixtures written in the `__tests__` and `__testfixtures__` directories, | ||
accordingly. | ||
|
||
As an example, to add a transform called `sort-prop-types` oone would create the | ||
following files: | ||
|
||
``` | ||
- codemods | ||
- transforms | ||
- __testfixtures__ | ||
- sort-prop-types.input.js | ||
- sort-prop-types.output.js | ||
- __tests__ | ||
- sort-prop-types-test.js | ||
- sort-prop-types.js | ||
``` | ||
|
||
In this structure, the test file will determine which test fixtures get run. | ||
Test fixtures are written with a `*.input.js` and `*.output.js` convention. | ||
Files that end with `*.input.js` are given to your transform and the test runner | ||
will assert that the output matches what is available in `*.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,19 @@ | ||
{ | ||
"name": "@carbon/codemods", | ||
"private": true, | ||
"description": "Codemods for the Carbon monorepo", | ||
"version": "0.0.0", | ||
"license": "Apache-2.0", | ||
"repository": "https://github.com/carbon-design-system/carbon/tree/master/codemods", | ||
"bugs": "https://github.com/carbon-design-system/carbon/issues", | ||
"keywords": [ | ||
"ibm", | ||
"carbon", | ||
"carbon-design-system", | ||
"components", | ||
"react" | ||
], | ||
"dependencies": { | ||
"jscodeshift": "^0.10.0" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
codemods/transforms/__testfixtures__/sort-prop-types.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,7 @@ | ||
function MyComponent() {} | ||
|
||
MyComponent.propTypes = { | ||
a: PropTypes.string, | ||
c: PropTypes.string, | ||
b: PropTypes.string, | ||
}; |
7 changes: 7 additions & 0 deletions
7
codemods/transforms/__testfixtures__/sort-prop-types.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,7 @@ | ||
function MyComponent() {} | ||
|
||
MyComponent.propTypes = { | ||
a: PropTypes.string, | ||
b: PropTypes.string, | ||
c: PropTypes.string, | ||
}; |
7 changes: 7 additions & 0 deletions
7
codemods/transforms/__testfixtures__/sort-prop-types2.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,7 @@ | ||
class A extends React.Component { | ||
static propTypes = { | ||
a: PropTypes.string, | ||
c: PropTypes.string, | ||
b: PropTypes.string, | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
codemods/transforms/__testfixtures__/sort-prop-types2.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,7 @@ | ||
class A extends React.Component { | ||
static propTypes = { | ||
a: PropTypes.string, | ||
b: PropTypes.string, | ||
c: PropTypes.string, | ||
}; | ||
} |
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 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { defineTest } = require('jscodeshift/dist/testUtils'); | ||
|
||
defineTest(__dirname, 'sort-prop-types'); | ||
defineTest(__dirname, 'sort-prop-types', null, 'sort-prop-types2'); |
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,88 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const defaultOptions = { | ||
quote: 'single', | ||
trailingComma: true, | ||
}; | ||
|
||
function transform(fileInfo, api, options) { | ||
const printOptions = options.printOptions || defaultOptions; | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
root | ||
.find(j.AssignmentExpression, { | ||
left: { | ||
type: 'MemberExpression', | ||
property: { | ||
name: 'propTypes', | ||
}, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
path.node.right.properties.sort((a, b) => { | ||
if (a.type === 'Property' && b.type === 'Property') { | ||
if (getPropName(a) > getPropName(b)) { | ||
return 1; | ||
} | ||
return -1; | ||
} | ||
|
||
if (a.type === 'SpreadElement' && b.type === 'SpreadElement') { | ||
if (getPropName(a) > getPropName(b)) { | ||
return 1; | ||
} | ||
return -1; | ||
} | ||
|
||
if (a.type === 'SpreadElement') { | ||
return -1; | ||
} | ||
|
||
return 1; | ||
}); | ||
}); | ||
|
||
root | ||
.find(j.ClassProperty, { | ||
key: { | ||
name: 'propTypes', | ||
}, | ||
}) | ||
.forEach((path) => { | ||
path.node.value.properties.sort((a, b) => { | ||
if (getPropName(a) > getPropName(b)) { | ||
return 1; | ||
} | ||
return -1; | ||
}); | ||
}); | ||
|
||
function getPropName(node) { | ||
if (node.type === 'SpreadElement') { | ||
return node.argument.name; | ||
} | ||
|
||
if (node.type === 'Property') { | ||
if (node.key.type === 'Identifier') { | ||
return node.key.name; | ||
} | ||
if (node.key.type === 'Literal') { | ||
return node.key.value; | ||
} | ||
} | ||
|
||
throw new Error(`Unknown node of type: ${node.type}`); | ||
} | ||
|
||
return root.toSource(printOptions); | ||
} | ||
|
||
module.exports = transform; |
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 @@ | ||
scss |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.