-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(generators): made enhancements to generator files #719
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function betterStringSort(a: string, b: string) { | ||
return a.toLowerCase().localeCompare(b.toLowerCase()); | ||
} | ||
|
||
module.exports = { betterStringSort }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,44 +22,34 @@ export async function genericRule({ | |
componentName, | ||
propName, | ||
ruleName, | ||
referenceRepo, | ||
referencePR, | ||
message, | ||
}: Answers) { | ||
// the formatting for content here looks weird, but that's to preserve indentation in the written file | ||
const content = `import { Rule } from "eslint"; | ||
import { JSXOpeningElement } from "estree-jsx"; | ||
import { getFromPackage } from "../../helpers"; | ||
import { getAllImportsFromPackage, checkMatchingJSXOpeningElement, getAttribute } from "../../helpers"; | ||
|
||
// https://github.com/patternfly/patternfly-react/pull/${referencePR} | ||
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR} | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: function (context: Rule.RuleContext) { | ||
const { imports } = getFromPackage(context, "@patternfly/react-core"); | ||
|
||
const componentImports = imports.filter( | ||
(specifier) => specifier.imported.name === "${componentName}" | ||
); | ||
const basePackage = "@patternfly/react-core"; | ||
const componentImports = getAllImportsFromPackage(context, basePackage, ["${componentName}"]); | ||
|
||
return !componentImports.length | ||
? {} | ||
: { | ||
JSXOpeningElement(node: JSXOpeningElement) { | ||
if ( | ||
node.name.type === "JSXIdentifier" && | ||
componentImports | ||
.map((imp) => imp.local.name) | ||
.includes(node.name.name) | ||
) { | ||
const attribute = node.attributes.find( | ||
(attr) => | ||
attr.type === "JSXAttribute" && attr.name.name === "${propName}" | ||
); | ||
if (attribute) { | ||
if (checkMatchingJSXOpeningElement(node, componentImports)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question - do we want to also care about default imports? It was a concern in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it isn't something we need in most cases, but is there any reason we wouldn't want to handle default imports by default though? 🤔 Doesn't have to be part of this PR though. |
||
const ${propName}Prop = getAttribute(node, "${propName}"); | ||
if (${propName}Prop) { | ||
context.report({ | ||
node, | ||
message: "${message}", | ||
fix(fixer) { | ||
return fixer.replaceText(attribute, ""); | ||
return fixer.replaceText(${propName}Prop, ""); | ||
}, | ||
}); | ||
} | ||
|
@@ -76,11 +66,12 @@ export async function addEventCBRule({ | |
componentName, | ||
propName, | ||
ruleName, | ||
referenceRepo, | ||
referencePR, | ||
}: Answers) { | ||
const content = `const { addCallbackParam } = require("../../helpers"); | ||
|
||
// https://github.com/patternfly/patternfly-react/pull/${referencePR} | ||
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR} | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: addCallbackParam(["${componentName}"], { ${propName}: "_event" }), | ||
|
@@ -93,11 +84,12 @@ export async function swapCBRule({ | |
componentName, | ||
propName, | ||
ruleName, | ||
referenceRepo, | ||
referencePR, | ||
}: Answers) { | ||
const content = `const { addCallbackParam } = require("../../helpers"); | ||
|
||
// https://github.com/patternfly/patternfly-react/pull/${referencePR} | ||
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR} | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: addCallbackParam(["${componentName}"], { ${propName}: { defaultParamName: "_event", previousParamIndex: 1, otherMatchers: /^_?(ev\\w*|e$)/ } }), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
JSXOpeningElement, | ||
ImportSpecifier, | ||
ImportDefaultSpecifier, | ||
} from "estree-jsx"; | ||
|
||
export function checkMatchingJSXOpeningElement( | ||
node: JSXOpeningElement, | ||
imports: | ||
| ImportSpecifier | ||
| ImportDefaultSpecifier | ||
| (ImportSpecifier | ImportDefaultSpecifier)[] | ||
) { | ||
if (Array.isArray(imports)) { | ||
return ( | ||
node.name.type === "JSXIdentifier" && | ||
imports.map((imp) => imp.local.name).includes(node.name.name) | ||
); | ||
} | ||
|
||
return ( | ||
node.name.type === "JSXIdentifier" && imports.local.name === node.name.name | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { getFromPackage } from "../../helpers"; | ||
import { getFromPackage, checkMatchingJSXOpeningElement } from "../../helpers"; | ||
import { Rule } from "eslint"; | ||
import { JSXOpeningElement } from "estree-jsx"; | ||
|
||
|
@@ -17,10 +17,7 @@ module.exports = { | |
? {} | ||
: { | ||
JSXOpeningElement(node: JSXOpeningElement) { | ||
if ( | ||
node.name.type === "JSXIdentifier" && | ||
accordionItemImport.local.name === node.name.name | ||
) { | ||
if (checkMatchingJSXOpeningElement(node, accordionItemImport)) { | ||
context.report({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only updated the one file for now. I could update the rest of the files as part of this or we can do that as a followup at some other point, I don't mind either way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also good either way you want to go about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to refactor our existing codemods, since they are a "one time use" code and we probably won't copy the code from them in the future. So IMO updating the |
||
node, | ||
message: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe have this be optional, with it defaulting to pf-react since that is what most of our mods are for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamviktora wdyt?
For context there was some discussion on Slack:
patternfly-react
, but have the ability to input custom text if you wantThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the enum as it is now. When it is on
patternfly-react
as default, it is just oneenter
press, same as for the regular input. And I don't think we will need to specify many more custom repos in the future. And if so, there will probably be more codemods for that repo, so that's why I think updating an enum once and then picking an option is easier than always having to write a repo name manually.