Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

feat(rule): Add new rule - react-a11y-media-captions #850

Merged
merged 12 commits into from
Jul 29, 2019

Conversation

noamyogev84
Copy link
Contributor

@noamyogev84 noamyogev84 commented Mar 15, 2019

PR checklist

Overview of change:

Added new rule react-a11y-media-captions. Rule will verify that video and audio elements have captions and descriptions. (example below)

    <video>
        <source src="sitting_and_smiling_234.mp4" type="video/mp4"/>
        <track src="sitting_and_smiling_234.vtt" kind="captions" srclang="en" label="English"/>
        <track src="myvideo_en.vtt" kind="description" srclang="en" label="English"/>
    </video>;
    <audio>
        <source src="beck_loser.mp3" type="video/mp4"/>
        <track src="beck_loser.vtt" kind="captions" srclang="en" label="English"/>
    </audio>;

@noamyogev84
Copy link
Contributor Author

@JoshuaKGoldberg @IllusionMH Hey guys, would like your review on this.
Again, I have no luck with those builds failing (local build + tests are fine!) 🤦‍♂️
Any clue what's going on there???

@IllusionMH
Copy link
Contributor

@noamyogev84 thanks for PR, will try to review it this weekend.

CI fails on Node 6, where npm does support package-lock.json and installs versions of TSLint with new rule that fails. PR #846 will fix CI errors.

@noamyogev84
Copy link
Contributor Author

@IllusionMH great, thanks man!

Copy link
Contributor

@IllusionMH IllusionMH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule code look good. Handles case that I've missed. 👍

However, two paths doesn't have failing and successful test, which will be really helpful.

Could you please rebase your branch, and then run npm install and npm test? There are some unexpected changes in tslint-warnings.csv and new entry should be added to configs/latest.json.

README.md Outdated Show resolved Hide resolved
src/reactA11yMediaCaptionsRule.ts Outdated Show resolved Hide resolved
src/reactA11yMediaCaptionsRule.ts Outdated Show resolved Hide resolved
function getAttributeText(attribute: ts.JsxAttribute): string | undefined {
if (attribute && attribute.initializer) {
if (tsutils.isJsxExpression(attribute.initializer)) {
return attribute.initializer.expression ? attribute.initializer.expression.getText() : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are no tests for this line.

src/reactA11yMediaCaptionsRule.ts Show resolved Hide resolved
@@ -156,6 +156,7 @@ no-redundant-jsdoc,Forbids JSDoc which duplicates TypeScript functionality.,TSLI
no-reference-import,Don't `<reference types="foo" />` if you import `foo` anyway.,TSLINT178RR3K,tslint,Non-SDL,Warning,Important,Opportunity for Excellence,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,710,"CWE 710 - Coding Standards Violation"
no-regex-spaces,Do not use multiple spaces in a regular expression literal. Similar to the ESLint no-regex-spaces rule,TSLINT1T5TJ80,tslint,Non-SDL,Error,Critical,Opportunity for Excellence,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,,
no-reserved-keywords,"Do not use reserved keywords as names of local variables, fields, functions, or other identifiers.",TSLINT14J77I2,tslint,SDL,Error,Critical,Mandatory,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,398,"CWE 398 - Indicator of Poor Code Quality"
no-restricted-globals,Disallow specific global variables.,TSLINTGKU2KB,tslint,,,,,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected change, if correct version of packages is installed for this branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pulled latest master version + run npm install,
still this entry is there. 😮

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pinging. Will check what's with this line today evening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Looks like these strings were not added during update of tslint package.

@JoshuaKGoldberg JoshuaKGoldberg added the PR: Waiting for Author Changes have been requested that the pull request author should address. label Mar 21, 2019
@IllusionMH
Copy link
Contributor

Fixes for lint erros in Node 6 are in master.

@JoshuaKGoldberg
Copy link

Ping @noamyogev84, do you still think you'll have time to get to these changes?

@noamyogev84
Copy link
Contributor Author

@JoshuaKGoldberg I'll continue today. 🍻

@noamyogev84
Copy link
Contributor Author

@IllusionMH @JoshuaKGoldberg
Did some work here, i'll appreciate your review 🍺

@JoshuaKGoldberg JoshuaKGoldberg added PR: Waiting for Reviewer A repository maintainer should take a look at the pull request soon! and removed PR: Waiting for Author Changes have been requested that the pull request author should address. labels Apr 5, 2019
Copy link
Contributor

@IllusionMH IllusionMH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for delay with review, it was tought weekend.
I think that better to ckeck strings values instead of variable names inside JsxAttribute's.

Also would be nice to avoid erros if there are spread attributes, however this one is optional and up to you.

const SomeComponent = () =>
<video width="320" height="240">
<source src="sitting_and_smiling_232.mp4" type="video/mp4"/>
<track src="subtitles_en.vtt" kind={subtitles} srclang="en" label="English"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... Looks like I've missed what will be checked inside JsxExpression.
I would expect simple check for string kind={'subtitles'} but not for variable name.
Without type info (I don't think it is needed for initial implementation) it would be great to check only strings inside JsxExpression.

Also would be nice to skip errors if there are spread attributes on track elements (see reasoning here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @IllusionMH
I'm sorry for being away for so long... 🙏

changed expression check according to your comments.
also, added Spread Attribute detection on track elements, so the following test should pass:

    <video width="320" height="240">
        <source src="sitting_and_smiling_260.mp4" type="video/mp4"/>
        <track {...props} />
        <track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English"/>
    </video>;```

@JoshuaKGoldberg JoshuaKGoldberg added PR: Waiting for Author Changes have been requested that the pull request author should address. and removed PR: Waiting for Reviewer A repository maintainer should take a look at the pull request soon! labels Apr 15, 2019
text to look only for string literals
add spread attributes logic to track elements
@IllusionMH IllusionMH added PR: Waiting for Reviewer A repository maintainer should take a look at the pull request soon! and removed PR: Waiting for Author Changes have been requested that the pull request author should address. labels Jul 9, 2019
@IllusionMH
Copy link
Contributor

@noamyogev84 thanks for following up on this PR!
Test cases look good, however as I understand - passing variable instead of string literal to kind attribute expression will trigger an error. Not sure if potential error here aligns with similar rules, but need to check first.
I will get to PC and finish reviewie tomorrow.

@@ -149,7 +149,6 @@ CWE 710 - Coding Standards Violation"
no-misused-new,Warns on apparent attempts to define constructors for interfaces or `new` for classes.,TSLINTL96MA6,tslint,Non-SDL,Warning,Important,Opportunity for Excellence,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,,
no-multiline-string,Do not declare multiline strings,TSLINT10K5P9U,tslint,Non-SDL,Warning,Low,Opportunity for Excellence,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,710,"CWE 710 - Coding Standards Violation"
no-non-null-assertion,Disallows non-null assertions using the `!` postfix operator.,TSLINTNO75FN,tslint,Non-SDL,Warning,Important,Opportunity for Excellence,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,710,"CWE 710 - Coding Standards Violation"
no-null-undefined-union,Disallows explicitly declared or implicitly returned union types with both `null` and `undefined` as members. ,TSLINT1JAMNL7,tslint,,,,,See description on the tslint or tslint-microsoft-contrib website,TSLint Procedure,,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this line was deleted - it is added back on my PC.
Could please run npm install (with npm 5+) and then npm test again?

Copy link
Contributor

@IllusionMH IllusionMH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good and failures are lint failures are false positives from TSLint rule only on Node v6. 🎉

Looks like other rules are using checks with getStringLiteral that will "fail" if variable is used in JSX expression. Example
However this case should be in tests so there would be no questions if it is intended or not.

@JoshuaKGoldberg do you think we should be consistent with existing rules and report errors if non-string value is used (variable or other expression) + ignore checks if spread is present?

return elementName === AUDIO_ELEMENT_NAME;
}

function getAttributeText(attribute: ts.JsxAttribute): string | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this function can be replaced with existing utility getStringLiteral from src/utils/JsxAttribute.ts

Which has similar signature and results I think

@noamyogev84
Copy link
Contributor Author

noamyogev84 commented Jul 19, 2019

However this case should be in tests so there would be no questions if it is intended or not.

Not sure if I understand.

@IllusionMH
Copy link
Contributor

@noamyogev84 I mean test that will clearly state that expressions that contain variable with correct data will be still counted as error. Something like this:

const captionsKind = 'captions';
const SomeComponent = () => 
    <audio>
        <source src="beck_loser.mp3" type="video/mp4"/>
        <track src="beck_loser.vtt" kind={ captionsKind } srclang="en" label="English"/>
    </audio>;

@noamyogev84
Copy link
Contributor Author

@IllusionMH @JoshuaKGoldberg
Hey guys, anything else is needed here?

Copy link
Contributor

@IllusionMH IllusionMH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good now! The only problem as usually - npm which messed up again with package-lock.json.

It would be great if you could revert only that file. If not - I will merge this PR and regenerate it.

package-lock.json Outdated Show resolved Hide resolved
@IllusionMH IllusionMH changed the title Add new rule - react-a11y-media-captions feat(rule): Add new rule - react-a11y-media-captions Jul 21, 2019
@IllusionMH IllusionMH removed the PR: Waiting for Reviewer A repository maintainer should take a look at the pull request soon! label Jul 29, 2019
@IllusionMH
Copy link
Contributor

@noamyogev84 everything looks great! 👍
Thank you for contribution and patience during review!

@IllusionMH IllusionMH merged commit 8f86920 into microsoft:master Jul 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new rule: react-a11y-media
3 participants