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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from bitpshr/feat/jsx-a11y-iframe-has-title
feat: add jsx-a11y/iframe-has-title lint rule
- Loading branch information
Showing
6 changed files
with
338 additions
and
0 deletions.
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; | ||
}, | ||
}; |