-
Notifications
You must be signed in to change notification settings - Fork 19
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
1 parent
9faaa83
commit 3da9dd1
Showing
7 changed files
with
1,147 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...slint-plugin-pf-codemods/src/rules/v6/tokensPrefixWithT/tokens-prefix-with-t.md
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,18 @@ | ||
### tokens-prefix-with-t [(#11002)](https://github.com/patternfly/patternfly-react/pull/11002) | ||
|
||
React tokens, whose value is a Patternfly token variable (with prefix --pf-t), are now prefixed with t_ | ||
|
||
#### Examples | ||
|
||
In: | ||
|
||
```jsx | ||
%inputExample% | ||
``` | ||
|
||
Out: | ||
|
||
```jsx | ||
%outputExample% | ||
``` | ||
|
132 changes: 132 additions & 0 deletions
132
...ges/eslint-plugin-pf-codemods/src/rules/v6/tokensPrefixWithT/tokens-prefix-with-t.test.ts
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,132 @@ | ||
const ruleTester = require("../../ruletester"); | ||
import * as rule from "./tokens-prefix-with-t"; | ||
|
||
const message = `React tokens, whose value is a Patternfly token variable (has prefix --pf-t), are now prefixed with t_`; | ||
|
||
ruleTester.run("tokens-prefix-with-t", rule, { | ||
valid: [ | ||
// tokens wich are not "--pf-t" variables | ||
{ | ||
code: `import c_about_modal_box from '@patternfly/react-tokens/dist/esm/c_about_modal_box';`, | ||
}, | ||
{ | ||
code: `import { c_about_modal_box } from '@patternfly/react-tokens';`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { global_font_size_100 } from "@patternfly/react-tokens"; | ||
global_font_size_100;`, | ||
output: `import { t_global_font_size_100 } from "@patternfly/react-tokens"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// with alias | ||
code: `import { global_font_size_100 as customFontSize } from "@patternfly/react-tokens"; | ||
customFontSize;`, | ||
output: `import { t_global_font_size_100 as customFontSize } from "@patternfly/react-tokens"; | ||
customFontSize;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportSpecifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// named import - esm | ||
code: `import { global_font_size_100 } from '@patternfly/react-tokens/dist/esm/global_font_size_100'; | ||
global_font_size_100;`, | ||
output: `import { t_global_font_size_100 } from "@patternfly/react-tokens/dist/esm/t_global_font_size_100"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// named import - js | ||
code: `import { global_font_size_100 } from '@patternfly/react-tokens/dist/js/global_font_size_100'; | ||
global_font_size_100;`, | ||
output: `import { t_global_font_size_100 } from "@patternfly/react-tokens/dist/js/t_global_font_size_100"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// default import - esm | ||
code: `import global_font_size_100 from '@patternfly/react-tokens/dist/esm/global_font_size_100'; | ||
global_font_size_100;`, | ||
output: `import t_global_font_size_100 from "@patternfly/react-tokens/dist/esm/t_global_font_size_100"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportDefaultSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// default import - js | ||
code: `import global_font_size_100 from '@patternfly/react-tokens/dist/js/global_font_size_100'; | ||
global_font_size_100;`, | ||
output: `import t_global_font_size_100 from "@patternfly/react-tokens/dist/js/t_global_font_size_100"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportDefaultSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
// default import with custom name | ||
code: `import customFontSize from '@patternfly/react-tokens/dist/esm/global_font_size_100'; | ||
customFontSize;`, | ||
output: `import t_global_font_size_100 from "@patternfly/react-tokens/dist/esm/t_global_font_size_100"; | ||
t_global_font_size_100;`, | ||
errors: [ | ||
{ | ||
message, | ||
type: "ImportDefaultSpecifier", | ||
}, | ||
{ | ||
message, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
134 changes: 134 additions & 0 deletions
134
packages/eslint-plugin-pf-codemods/src/rules/v6/tokensPrefixWithT/tokens-prefix-with-t.ts
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,134 @@ | ||
import { Rule } from "eslint"; | ||
import { | ||
IdentifierWithParent, | ||
ImportDefaultSpecifierWithParent, | ||
ImportSpecifierWithParent, | ||
getDefaultImportsFromPackage, | ||
getFromPackage, | ||
getImportPath, | ||
} from "../../helpers"; | ||
import { Identifier, ImportSpecifier } from "estree-jsx"; | ||
import { tTokens } from "../../../tokenLists"; | ||
|
||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: function (context: Rule.RuleContext) { | ||
const tokensPackage = "@patternfly/react-tokens"; | ||
|
||
const { imports: tokenSpecifiers } = getFromPackage(context, tokensPackage); | ||
|
||
const defaultTokenSpecifiers = getDefaultImportsFromPackage( | ||
context, | ||
tokensPackage | ||
) | ||
.map((specifier) => ({ | ||
specifier, | ||
path: getImportPath(specifier), | ||
})) | ||
.filter(({ path }) => path !== undefined) | ||
.map(({ specifier, path }) => ({ | ||
specifier, | ||
token: (path as string).split("/").pop() as string, | ||
})); | ||
|
||
const getTokenPathFix = ( | ||
fixer: Rule.RuleFixer, | ||
node: ImportSpecifierWithParent | ImportDefaultSpecifierWithParent, | ||
oldToken: string, | ||
newToken: string | ||
) => { | ||
const packagePath = getImportPath(node); | ||
if (packagePath && packagePath.includes(oldToken)) { | ||
const newPath = packagePath.replace(oldToken, newToken); | ||
return fixer.replaceText(node.parent?.source!, `"${newPath}"`); | ||
} | ||
}; | ||
|
||
const replaceToken = ( | ||
node: | ||
| ImportSpecifierWithParent | ||
| ImportDefaultSpecifierWithParent | ||
| Identifier, | ||
oldToken: string | ||
) => { | ||
const newToken = `t_${oldToken}`; | ||
|
||
context.report({ | ||
node, | ||
message: `React tokens, whose value is a Patternfly token variable (has prefix --pf-t), are now prefixed with t_`, | ||
fix(fixer) { | ||
if (node.type === "Identifier") { | ||
return fixer.replaceText(node, newToken); | ||
} | ||
|
||
const tokenPathFix = getTokenPathFix(fixer, node, oldToken, newToken); | ||
|
||
return [ | ||
fixer.replaceText( | ||
node.type === "ImportSpecifier" ? node.imported : node, | ||
newToken | ||
), | ||
...(tokenPathFix ? [tokenPathFix] : []), | ||
]; | ||
}, | ||
}); | ||
}; | ||
|
||
return { | ||
ImportSpecifier(node: ImportSpecifier) { | ||
if (tokenSpecifiers.includes(node)) { | ||
const token = node.imported.name; | ||
if (tTokens.includes(token)) { | ||
replaceToken(node, token); | ||
} | ||
} | ||
}, | ||
ImportDefaultSpecifier(node: ImportDefaultSpecifierWithParent) { | ||
const specifierWithToken = defaultTokenSpecifiers.find( | ||
({ specifier }) => node === specifier | ||
); | ||
|
||
if (!specifierWithToken) { | ||
return; | ||
} | ||
|
||
const { token } = specifierWithToken; | ||
|
||
if (tTokens.includes(token)) { | ||
replaceToken(node, token); | ||
} | ||
}, | ||
Identifier(node: Identifier) { | ||
const parentType = (node as IdentifierWithParent).parent?.type; | ||
// handle ImportSpecifier and ImportDefaultSpecifier separately | ||
if ( | ||
parentType === "ImportSpecifier" || | ||
parentType === "ImportDefaultSpecifier" | ||
) { | ||
return; | ||
} | ||
|
||
const specifierWithToken = defaultTokenSpecifiers.find( | ||
({ specifier }) => node.name === specifier.local.name | ||
); | ||
|
||
if (specifierWithToken) { | ||
const { token } = specifierWithToken; | ||
if (tTokens.includes(token)) { | ||
replaceToken(node, token); | ||
} | ||
} | ||
|
||
const unaliasedTokenSpecifier = tokenSpecifiers.find( | ||
(specifier) => | ||
specifier.local.name === specifier.imported.name && | ||
node.name === specifier.local.name | ||
); | ||
|
||
if (unaliasedTokenSpecifier && tTokens.includes(node.name)) { | ||
replaceToken(node, node.name); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/eslint-plugin-pf-codemods/src/rules/v6/tokensPrefixWithT/tokensPrefixWithTInput.tsx
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,3 @@ | ||
import color_green_10 from "@patternfly/react-tokens/dist/esm/color_green_10"; | ||
|
||
color_green_10; |
3 changes: 3 additions & 0 deletions
3
...ages/eslint-plugin-pf-codemods/src/rules/v6/tokensPrefixWithT/tokensPrefixWithTOutput.tsx
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,3 @@ | ||
import t_color_green_10 from "@patternfly/react-tokens/dist/esm/t_color_green_10"; | ||
|
||
t_color_green_10; |
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
Oops, something went wrong.