-
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
Helper documentation/restructure POC #811
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Rule } from "eslint"; | ||
import { JSXElement } from "estree-jsx"; | ||
import { removeElement, removeEmptyLineAfter } from "../index"; | ||
|
||
/** | ||
* Use inline within the context.report's fix method to remove entire JSX elements. This can be returned on its own or spread as part of a fixes array. | ||
* @param context The context object from the top level create method of the rule being written. | ||
* @param fixer The fixer parameter from the fix method of the context.report object. | ||
* @param elementsToRemove An array of JSXElement objects to remove. | ||
* @returns An array of fixers to apply when the rule is ran with the fix flag. | ||
*/ | ||
export const getRemoveElementFixes = ( | ||
context: Rule.RuleContext, | ||
fixer: Rule.RuleFixer, | ||
elementsToRemove: JSXElement[] | ||
) => { | ||
return elementsToRemove | ||
.map((element) => [ | ||
...removeElement(fixer, element), | ||
...removeEmptyLineAfter(context, fixer, element), | ||
]) | ||
.flat(); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./getRemoveSpecifierFixes"; | ||
export * from "./getRemoveElementFixes"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ImportSpecifier } from "estree-jsx"; | ||
import { Rule, SourceCode } from "eslint"; | ||
import { ImportDefaultSpecifierWithParent } from "./interfaces"; | ||
|
||
/** | ||
* Use inline to check whether an import specifier has a data-codemod comment, and conditionally run logic based on its existence or absence. | ||
* @param context The context object from the ancestor create method. | ||
* @param importSpecifier The specifier in which to find whether the data-codemod comment exists. | ||
* @param commentToFind The string to check whether is included in a comment. By default this is "data-codemods", but can be customized for more fine-tuned logic. | ||
* @returns The first Comment object if the data-codemod string was found, or undefined otherwise. | ||
*/ | ||
export function getCodeModDataComment( | ||
sourceCode: SourceCode, | ||
importSpecifier: ImportSpecifier | ImportDefaultSpecifierWithParent, | ||
commentToFind: string = "data-codemods" | ||
) { | ||
const { leading, trailing } = sourceCode.getComments(importSpecifier); | ||
|
||
return [...leading, ...trailing].find((comment) => | ||
comment.value.includes(commentToFind) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import { ImportDeclaration, ImportSpecifier } from "estree-jsx"; | ||
import { pfPackageMatches } from "../pfPackageMatches"; | ||
import { ImportDefaultSpecifierWithParent } from "../interfaces"; | ||
|
||
function checkSpecifierExists( | ||
node: ImportDeclaration, | ||
importSpecifier: ImportSpecifier | ||
importSpecifier: ImportSpecifier | ImportDefaultSpecifierWithParent | ||
) { | ||
return node.specifiers.some( | ||
(specifier) => | ||
specifier.type === "ImportSpecifier" && | ||
specifier.imported.name === importSpecifier.imported.name | ||
(specifier.type === "ImportSpecifier" && | ||
specifier.imported.name === | ||
(importSpecifier as ImportSpecifier).imported.name) || | ||
(specifier.type === "ImportDefaultSpecifier" && | ||
specifier.local.name === importSpecifier.local.name) | ||
); | ||
} | ||
|
||
/** Used to check whether the current ImportDeclaration node matches at least 1 of the import specifiers. */ | ||
/** | ||
* Use inline at the start of an ImportDeclaration block to early return if no specifiers are found, or only run logic if at least 1 specifier is found. | ||
* @param node - The node to check for any import specifiers. | ||
* @param imports - A single import specifier or an array of specifiers to find within the import declaration. | ||
* @param packageName - The package name to check against the import declaration source. | ||
* @returns A boolean depending on whether the import declaration source matches the packageName, or whether at least 1 import specifier is found. | ||
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. Maybe I'd rewrite this to |
||
*/ | ||
export function checkMatchingImportDeclaration( | ||
node: ImportDeclaration, | ||
imports: ImportSpecifier | ImportSpecifier[], | ||
imports: | ||
| ImportSpecifier | ||
| ImportDefaultSpecifierWithParent | ||
| (ImportSpecifier | ImportDefaultSpecifierWithParent)[], | ||
packageName: string = "@patternfly/react-core" | ||
) { | ||
if (!pfPackageMatches(packageName, node.source.value)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./checkMatchingImportDeclaration"; | ||
export * from "./checkMatchingJSXOpeningElement"; |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
import { TestErrors } from "./testingTypes"; | ||||||
|
||||||
/** | ||||||
* Use inline in a test file to push a test object to an invalid test array. | ||||||
* @param code The invalid code snippet that should trigger an error. | ||||||
* @param output The code snippet that should be output upon the fixer being ran. If no fixer exists, this shoujld be the same as the code parameter. | ||||||
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. nit: typo
Suggested change
|
||||||
* @param errors An array of error objects, each consisting of the error message and the node type that should trigger the error. | ||||||
* @returns An invalid test object. | ||||||
*/ | ||||||
export function createInvalidTest( | ||||||
code: string, | ||||||
output: string, | ||||||
errors: TestErrors | ||||||
) { | ||||||
return { | ||||||
code, | ||||||
output, | ||||||
errors, | ||||||
}; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Use inline in a test file to push a test object to a valid test array. | ||
* @param code The valid code snippet. | ||
* @returns A valid test object. | ||
*/ | ||
export function createValidTest(code: string) { | ||
return { | ||
code, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./testingTypes"; | ||
export * from "./createValidTest"; | ||
export * from "./createInvalidTest"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { RuleTester } from "eslint"; | ||
|
||
export type ValidTests = Array<string | RuleTester.ValidTestCase>; | ||
export type InvalidTests = RuleTester.InvalidTestCase[]; | ||
export type TestErrors = { | ||
message: string; | ||
type: string; | ||
}[]; |
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.
👍 👍