-
Notifications
You must be signed in to change notification settings - Fork 10
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly #270
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
Merged
Merged
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly #270
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb2bff6
Update the tooltip eslint rule as we now export the new one from main…
broccolinisoup d56bb69
oops
broccolinisoup 295439e
just make the code a lil better
broccolinisoup 2f0c66b
Create gorgeous-frogs-cover.md
broccolinisoup db3bc20
Merge branch 'main' into update-tooltip-rule-main
broccolinisoup b663b9d
reuse rootImport and fix method
broccolinisoup c8ca93d
Update .changeset/gorgeous-frogs-cover.md
broccolinisoup 4cb5e97
improve the rule docs so that it can be used as a migration guide
broccolinisoup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"eslint-plugin-primer-react": patch | ||
--- | ||
|
||
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
const rule = require('../a11y-use-accessible-tooltip') | ||
const {RuleTester} = require('eslint') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('a11y-use-accessible-tooltip', rule, { | ||
valid: [`import {Tooltip} from '@primer/react';`], | ||
invalid: [ | ||
// Single import from deprecated | ||
{ | ||
code: `import {Tooltip} from '@primer/react/deprecated';`, | ||
errors: [{messageId: 'useAccessibleTooltip'}], | ||
output: `import {Tooltip} from '@primer/react';`, | ||
}, | ||
// Multiple imports from deprecated | ||
{ | ||
code: `import {Tooltip, Button} from '@primer/react/deprecated';`, | ||
errors: [{messageId: 'useAccessibleTooltip'}], | ||
output: `import {Button} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`, | ||
}, | ||
// Multiple imports from deprecated | ||
{ | ||
code: `import {Button, Tooltip, Stack} from '@primer/react/deprecated';`, | ||
errors: [{messageId: 'useAccessibleTooltip'}], | ||
output: `import {Button, Stack} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`, | ||
}, | ||
// Single import from deprecated with an existing import from @primer/react | ||
{ | ||
code: `import {Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`, | ||
errors: [{messageId: 'useAccessibleTooltip', line: 1}], | ||
output: `import {ActionList, ActionMenu, Tooltip} from '@primer/react';`, | ||
}, | ||
// Multiple imports from deprecated with an existing import from @primer/react | ||
{ | ||
code: `import {Dialog, Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`, | ||
errors: [{messageId: 'useAccessibleTooltip', line: 1}], | ||
output: `import {Dialog} from '@primer/react/deprecated';import {ActionList, ActionMenu, Tooltip} from '@primer/react';`, | ||
}, | ||
{ | ||
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react/deprecated';\n<Tooltip aria-label="tooltip text" noDelay={true} wrap={true} align="left"><Button>Button</Button></Tooltip>`, | ||
errors: [ | ||
{messageId: 'useAccessibleTooltip', line: 1}, | ||
{messageId: 'useTextProp', line: 2}, | ||
{messageId: 'noDelayRemoved', line: 2}, | ||
{messageId: 'wrapRemoved', line: 2}, | ||
{messageId: 'alignRemoved', line: 2}, | ||
], | ||
output: `import {ActionList, ActionMenu, Button} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';\n<Tooltip text="tooltip text" ><Button>Button</Button></Tooltip>`, | ||
}, | ||
], | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,148 @@ | ||
'use strict' | ||
const url = require('../url') | ||
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute') | ||
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'recommends the use of @primer/react Tooltip component', | ||
category: 'Best Practices', | ||
recommended: true, | ||
url: url(module), | ||
}, | ||
fixable: true, | ||
schema: [], | ||
messages: { | ||
useAccessibleTooltip: 'Please use @primer/react Tooltip component that has accessibility improvements', | ||
useTextProp: 'Please use the text prop instead of aria-label', | ||
noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default', | ||
wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default', | ||
alignRemoved: 'align prop is removed. Please use the direction prop instead.', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value !== '@primer/react/deprecated') { | ||
return | ||
} | ||
const hasTooltip = node.specifiers.some( | ||
specifier => specifier.imported && specifier.imported.name === 'Tooltip', | ||
) | ||
|
||
if (!hasTooltip) { | ||
return | ||
} | ||
|
||
const hasOtherImports = node.specifiers.length > 1 | ||
|
||
const sourceCode = context.getSourceCode() | ||
// Checking to see if there is an existing root (@primer/react) import | ||
// Assuming there is one root import per file | ||
const rootImport = sourceCode.ast.body.find(statement => { | ||
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react' | ||
}) | ||
|
||
const tooltipSpecifier = node.specifiers.find( | ||
specifier => specifier.imported && specifier.imported.name === 'Tooltip', | ||
) | ||
|
||
const hasRootImport = rootImport !== undefined | ||
|
||
context.report({ | ||
node, | ||
messageId: 'useAccessibleTooltip', | ||
fix(fixer) { | ||
const fixes = [] | ||
if (!hasOtherImports) { | ||
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement | ||
if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`)) | ||
if (hasRootImport) { | ||
// remove the entire import statement | ||
fixes.push(fixer.remove(node)) | ||
// find the last specifier in the existing @primer/react import and insert Tooltip after that | ||
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1] | ||
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`)) | ||
} | ||
} else { | ||
// There are other imports from the deprecated bundle but no existing @primer/react import, so remove the Tooltip import and add a new import statement with the correct path. | ||
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier) | ||
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier) | ||
const hasTrailingComma = nextToken && nextToken.value === ',' | ||
const hasLeadingComma = previousToken && previousToken.value === ',' | ||
|
||
let rangeToRemove | ||
|
||
if (hasTrailingComma) { | ||
rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1] | ||
} else if (hasLeadingComma) { | ||
rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]] | ||
} else { | ||
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]] | ||
} | ||
// Remove Tooltip from the import statement | ||
fixes.push(fixer.removeRange(rangeToRemove)) | ||
|
||
if (!hasRootImport) { | ||
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`)) | ||
} else { | ||
// find the last specifier in the existing @primer/react import and insert Tooltip after that | ||
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1] | ||
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`)) | ||
} | ||
} | ||
return fixes | ||
}, | ||
}) | ||
}, | ||
JSXOpeningElement(node) { | ||
const openingElName = getJSXOpeningElementName(node) | ||
if (openingElName !== 'Tooltip') { | ||
return | ||
} | ||
const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label') | ||
if (ariaLabel !== undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'useTextProp', | ||
fix(fixer) { | ||
return fixer.replaceText(ariaLabel.name, 'text') | ||
}, | ||
}) | ||
} | ||
const noDelay = getJSXOpeningElementAttribute(node, 'noDelay') | ||
if (noDelay !== undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'noDelayRemoved', | ||
fix(fixer) { | ||
return fixer.remove(noDelay) | ||
}, | ||
}) | ||
} | ||
const wrap = getJSXOpeningElementAttribute(node, 'wrap') | ||
if (wrap !== undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'wrapRemoved', | ||
fix(fixer) { | ||
return fixer.remove(wrap) | ||
}, | ||
}) | ||
} | ||
const align = getJSXOpeningElementAttribute(node, 'align') | ||
if (align !== undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'alignRemoved', | ||
fix(fixer) { | ||
return fixer.remove(align) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.