-
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
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly #270
Changes from 5 commits
bb2bff6
d56bb69
295439e
2f0c66b
db3bc20
b663b9d
c8ca93d
4cb5e97
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,5 @@ | ||
--- | ||
"eslint-plugin-primer-react": patch | ||
--- | ||
|
||
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly |
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
'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 | ||
const rootImport = sourceCode.ast.body.filter(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.length >= 1 | ||
|
||
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 rootImport = sourceCode.ast.body.find(statement => { | ||
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react' | ||
}) | ||
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. Wondering if there's a reason we can't reuse 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. We can re-use! I missed that due to using the wrong method (filter vs find.) I updated the code 🤗 Thanks for catching 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 rootImport = sourceCode.ast.body.find(statement => { | ||
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react' | ||
}) | ||
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) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Uh oh!
There was an error while loading. Please reload this page.