This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
feat: add jsx-a11y/iframe-has-title lint rule #437
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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
249 changes: 249 additions & 0 deletions
249
packages/@romejs/js-compiler/lint/rules/react/jsxA11yIframeHasTitle.test.md
This file contains 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,249 @@ | ||
# `jsxA11yIframeHasTitle.test.ts` | ||
|
||
**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/lint/rules/react/jsxA11yIframeHasTitle.test.ts --update-snapshots` to update. | ||
|
||
## `require a title attribute on <iframe> JSX elements` | ||
|
||
### `0` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe /> | ||
^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `0: formatted` | ||
|
||
``` | ||
<iframe />; | ||
|
||
``` | ||
|
||
### `1` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe {...props} /> | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `10` | ||
|
||
``` | ||
✔ No known problems! | ||
|
||
``` | ||
|
||
### `10: formatted` | ||
|
||
``` | ||
<iframe title={title}></iframe>; | ||
|
||
``` | ||
|
||
### `1: formatted` | ||
|
||
``` | ||
<iframe {...props} />; | ||
|
||
``` | ||
|
||
### `2` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title="" /> | ||
^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `2: formatted` | ||
|
||
``` | ||
<iframe title="" />; | ||
|
||
``` | ||
|
||
### `3` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={""} /> | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `3: formatted` | ||
|
||
``` | ||
<iframe title={''} />; | ||
|
||
``` | ||
|
||
### `4` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={``} /> | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `4: formatted` | ||
|
||
``` | ||
<iframe title={``} />; | ||
|
||
``` | ||
|
||
### `5` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={undefined} /> | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `5: formatted` | ||
|
||
``` | ||
<iframe title={undefined} />; | ||
|
||
``` | ||
|
||
### `6` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={false} /> | ||
^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `6: formatted` | ||
|
||
``` | ||
<iframe title={false} />; | ||
|
||
``` | ||
|
||
### `7` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={true} /> | ||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `7: formatted` | ||
|
||
``` | ||
<iframe title={true} />; | ||
|
||
``` | ||
|
||
### `8` | ||
|
||
``` | ||
|
||
unknown:1 lint/jsxA11yIframeHasTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ iframe elements should have a title prop. | ||
|
||
<iframe title={42} /> | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
✖ Found 1 problem | ||
|
||
``` | ||
|
||
### `8: formatted` | ||
|
||
``` | ||
<iframe title={42} />; | ||
|
||
``` | ||
|
||
### `9` | ||
|
||
``` | ||
✔ No known problems! | ||
|
||
``` | ||
|
||
### `9: formatted` | ||
|
||
``` | ||
<iframe title="title" />; | ||
|
||
``` |
35 changes: 35 additions & 0 deletions
35
packages/@romejs/js-compiler/lint/rules/react/jsxA11yIframeHasTitle.test.ts
This file contains 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,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {test} from 'rome'; | ||
|
||
import {testLintMultiple} from '../testHelpers'; | ||
|
||
test( | ||
'require a title attribute on <iframe> JSX elements', | ||
async (t) => { | ||
await testLintMultiple( | ||
t, | ||
[ | ||
// INVALID | ||
'<iframe />', | ||
'<iframe {...props} />', | ||
'<iframe title="" />', | ||
'<iframe title={""} />', | ||
'<iframe title={``} />', | ||
'<iframe title={undefined} />', | ||
'<iframe title={false} />', | ||
'<iframe title={true} />', | ||
'<iframe title={42} />', | ||
// VALID | ||
'<iframe title="title" />', | ||
'<iframe title={title} >', | ||
], | ||
{category: 'lint/jsxA11yIframeHasTitle'}, | ||
); | ||
}, | ||
); |
47 changes: 47 additions & 0 deletions
47
packages/@romejs/js-compiler/lint/rules/react/jsxA11yIframeHasTitle.ts
This file contains 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,47 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {descriptions} from '@romejs/diagnostics'; | ||
import {AnyNode} from '@romejs/js-ast'; | ||
import {Path} from '@romejs/js-compiler'; | ||
|
||
function jsxIframeMissingTitle(node: AnyNode) { | ||
return ( | ||
node.type === 'JSXElement' && | ||
node.name.type === 'JSXIdentifier' && | ||
node.name.name === 'iframe' && | ||
!node.attributes.some((attribute) => | ||
attribute.type === 'JSXAttribute' && | ||
attribute.name.name === 'title' && | ||
attribute.value && | ||
((attribute.value.type === 'StringLiteral' && | ||
attribute.value.value.length > 0) || | ||
(attribute.value.type === 'JSXExpressionContainer' && | ||
((attribute.value.expression.type === 'ReferenceIdentifier' && | ||
attribute.value.expression.name !== 'undefined') || | ||
(attribute.value.expression.type === 'StringLiteral' && | ||
attribute.value.expression.value.length > 0)))) | ||
) | ||
); | ||
} | ||
|
||
export default { | ||
name: 'jsxA11yIframeHasTitle', | ||
|
||
enter(path: Path): AnyNode { | ||
const {node} = path; | ||
|
||
if (jsxIframeMissingTitle(node)) { | ||
path.context.addNodeDiagnostic( | ||
node, | ||
descriptions.LINT.REACT_JSX_A11Y_IFRAME_HAS_TITLE, | ||
); | ||
} | ||
|
||
return node; | ||
}, | ||
}; |
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.
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.
We should abstract some of the attribute checking out into a util method since it seems to be a common pattern for JSX rules.