Skip to content

Commit 4c7e781

Browse files
committed
[Robustness] use safe-regex-test
1 parent 2d8d557 commit 4c7e781

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"minimatch": "^3.1.2",
9090
"object.entries": "^1.1.7",
9191
"object.fromentries": "^2.0.7",
92+
"safe-regex-test": "^1.0.1",
9293
"string.prototype.includes": "^2.0.0"
9394
},
9495
"peerDependencies": {

src/rules/accessible-emoji.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import emojiRegex from 'emoji-regex';
1111
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
12+
import safeRegexTest from 'safe-regex-test';
1213
import { generateObjSchema } from '../util/schemas';
1314
import getElementType from '../util/getElementType';
1415
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
@@ -29,11 +30,13 @@ export default {
2930

3031
create: (context) => {
3132
const elementType = getElementType(context);
33+
34+
const testEmoji = safeRegexTest(emojiRegex());
3235
return {
3336
JSXOpeningElement: (node) => {
3437
const literalChildValue = node.parent.children.find((child) => child.type === 'Literal' || child.type === 'JSXText');
3538

36-
if (literalChildValue && emojiRegex().test(literalChildValue.value)) {
39+
if (literalChildValue && testEmoji(literalChildValue.value)) {
3740
const elementIsHidden = isHiddenFromScreenReader(elementType(node), node.attributes);
3841
if (elementIsHidden) {
3942
return; // emoji is decorative

src/rules/anchor-is-valid.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import { getProp, getPropValue } from 'jsx-ast-utils';
1212
import type { JSXOpeningElement } from 'ast-types-flow';
13+
import safeRegexTest from 'safe-regex-test';
1314
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
1415
import { generateObjSchema, arraySchema, enumArraySchema } from '../util/schemas';
1516
import getElementType from '../util/getElementType';
@@ -39,6 +40,8 @@ export default ({
3940

4041
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
4142
const elementType = getElementType(context);
43+
const testJShref = safeRegexTest(/^\W*?javascript:/);
44+
4245
return {
4346
JSXOpeningElement: (node: JSXOpeningElement): void => {
4447
const { attributes } = node;
@@ -98,7 +101,7 @@ export default ({
98101
.filter((value) => (
99102
value != null
100103
&& (typeof value === 'string' && (
101-
!value.length || value === '#' || /^\W*?javascript:/.test(value)
104+
!value.length || value === '#' || testJShref(value)
102105
))
103106
));
104107
if (invalidHrefValues.length !== 0) {

src/rules/img-redundant-alt.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { getProp, getLiteralPropValue } from 'jsx-ast-utils';
1111
import includes from 'array-includes';
1212
import stringIncludes from 'string.prototype.includes';
13+
import safeRegexTest from 'safe-regex-test';
1314
import { generateObjSchema, arraySchema } from '../util/schemas';
1415
import getElementType from '../util/getElementType';
1516
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
@@ -27,11 +28,12 @@ const schema = generateObjSchema({
2728
words: arraySchema,
2829
});
2930

31+
const isASCII = safeRegexTest(/[\x20-\x7F]+/);
32+
3033
function containsRedundantWord(value, redundantWords) {
3134
const lowercaseRedundantWords = redundantWords.map((redundantWord) => redundantWord.toLowerCase());
32-
const isASCII = /[\x20-\x7F]+/.test(value);
3335

34-
if (isASCII) {
36+
if (isASCII(value)) {
3537
return value.split(/\s+/).some((valueWord) => includes(lowercaseRedundantWords, valueWord.toLowerCase()));
3638
}
3739
return lowercaseRedundantWords.some((redundantWord) => stringIncludes(value.toLowerCase(), redundantWord));

0 commit comments

Comments
 (0)