Skip to content
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

feat(Chip) replace with Label #621

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Rule } from 'eslint';
import { ImportDeclaration } from 'estree-jsx';

export function getImportDeclarations(
context: Rule.RuleContext,
packageName: string
) {
const astBody = context.getSourceCode().ast.body;

const importDeclarationsFromPackage = astBody.filter(
(node) =>
node.type === 'ImportDeclaration' && node.source.value === packageName
) as ImportDeclaration[];

return importDeclarationsFromPackage;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./findAncestor";
export * from "./helpers";
export * from "./getImportDeclaration";
export * from "./getFromPackage";
export * from "./getText";
export * from "./includesImport";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### chip-replace-with-label [(#10049)](https://github.com/patternfly/patternfly-react/pull/10049)

Chip has been deprecated. Running the fix flag will replace Chip and ChipGroup components with Label and LabelGroup components respectively.

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
const ruleTester = require('../../ruletester');
import * as rule from './chip-replace-with-label';

const chipImportError = {
message: `Chip has been deprecated. Running the fix flag will replace Chip and ChipGroup components with Label and LabelGroup components respectively.`,
type: 'ImportDeclaration',
};

ruleTester.run('chip-replace-with-label', rule, {
valid: [
{
code: `<Chip />`,
},
{
code: `<ChipGroup />`,
},
// with import not from the deprecated package
{
code: `import { Chip, ChipGroup } from '@patternfly/react-core'; <ChipGroup><Chip /></ChipGroup>`,
},
],
invalid: [
{
code: `import { Chip } from '@patternfly/react-core/deprecated'; <Chip badge={identifier}onClick={handleOnClick} someOtherProp>This is a chip</Chip>`,
output: `import { Label } from '@patternfly/react-core'; <Label variant="outline" onClose={handleOnClick} someOtherProp>This is a chip {identifier}</Label>`,
errors: [chipImportError],
},
{
code: `import { ChipGroup } from '@patternfly/react-core/deprecated'; <ChipGroup someOtherProp>This is a chipgroup</ChipGroup>`,
output: `import { LabelGroup } from '@patternfly/react-core'; <LabelGroup someOtherProp>This is a chipgroup</LabelGroup>`,
errors: [chipImportError],
},
// with Chip nested in ChipGroup
{
code: `import { Chip, ChipGroup } from '@patternfly/react-core/deprecated';
<ChipGroup>
<Chip badge={identifier}onClick={handleOnClick} someOtherProp>This is a chip</Chip>
</ChipGroup>`,
output: `import { Label, LabelGroup } from '@patternfly/react-core';
<LabelGroup>
<Label variant="outline" onClose={handleOnClick} someOtherProp>This is a chip {identifier}</Label>
</LabelGroup>`,
errors: [chipImportError],
},
// with aliased Chip and ChipGroup
{
code: `import { Chip as PFChip, ChipGroup as PFChipGroup } from '@patternfly/react-core/deprecated';
<PFChipGroup>
<PFChip>This is a chip</PFChip>
</PFChipGroup>`,
output: `import { Label, LabelGroup } from '@patternfly/react-core';
<LabelGroup>
<Label variant="outline">This is a chip</Label>
</LabelGroup>`,
errors: [chipImportError],
},
// with other import specifiers from the deprecated package
{
code: `import { Chip, Select } from '@patternfly/react-core/deprecated';
<Chip badge={identifier} onClick={handleOnClick} someOtherProp>
This is a chip
</Chip>`,
output: `import { Select } from '@patternfly/react-core/deprecated';import { Label } from '@patternfly/react-core';
<Label variant="outline" onClose={handleOnClick} someOtherProp>
This is a chip
{identifier}</Label>`,
errors: [chipImportError],
},
// with Label import already included
{
code: `import { Label } from '@patternfly/react-core';
import { Chip } from '@patternfly/react-core/deprecated';
<>
<Chip badge={identifier} onClick={handleOnClick} someOtherProp>
This is a chip
</Chip>
<Label>
This is a label
</Label>
</>`,
output: `import { Label } from '@patternfly/react-core';

<>
<Label variant="outline" onClose={handleOnClick} someOtherProp>
This is a chip
{identifier}</Label>
<Label>
This is a label
</Label>
</>`,
errors: [chipImportError],
},
// with LabelGroup import already included
{
code: `import { LabelGroup } from '@patternfly/react-core';
import { Chip, ChipGroup } from '@patternfly/react-core/deprecated';
<>
<ChipGroup>
<Chip badge={identifier} onClick={handleOnClick} someOtherProp>
This is a chip
</Chip>
</ChipGroup>
<LabelGroup>
This is a label group
</LabelGroup>
</>`,
output: `import { LabelGroup, Label } from '@patternfly/react-core';

<>
<LabelGroup>
<Label variant="outline" onClose={handleOnClick} someOtherProp>
This is a chip
{identifier}</Label>
</LabelGroup>
<LabelGroup>
This is a label group
</LabelGroup>
</>`,
errors: [chipImportError],
},
// with Label and LabelGroup imports already included with aliases
{
code: `import { Label as PFLabel, LabelGroup as PFLabelGroup } from '@patternfly/react-core';
import { Chip, ChipGroup } from '@patternfly/react-core/deprecated';
<>
<ChipGroup>
<Chip>
This is a chip
</Chip>
</ChipGroup>
<PFLabelGroup>
<PFLabel>
This is a label
</PFLabel>
</PFLabelGroup>
</>`,
output: `import { Label as PFLabel, LabelGroup as PFLabelGroup } from '@patternfly/react-core';

<>
<PFLabelGroup>
<PFLabel variant="outline">
This is a chip
</PFLabel>
</PFLabelGroup>
<PFLabelGroup>
<PFLabel>
This is a label
</PFLabel>
</PFLabelGroup>
</>`,
errors: [chipImportError],
},
],
});
Loading
Loading