Skip to content

Commit b068236

Browse files
authored
code: Move joinLiterals to utils and share between two rules (#324)
1 parent 1c58586 commit b068236

File tree

3 files changed

+27
-37
lines changed

3 files changed

+27
-37
lines changed

src/rules/no-parse-html-literal.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,6 @@ const rsingleTag = /^<([a-z][^/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)
99
const rsingleTagMinimal = /^<([a-z][^/\0>:\x20\t\r\n\f]*)>$/i;
1010
const rsingleTagSelfClosing = /^<([a-z][^/\0>:\x20\t\r\n\f]*)\/>$/i;
1111

12-
function allLiteral( node ) {
13-
if ( node.type === 'BinaryExpression' ) {
14-
return allLiteral( node.left ) && allLiteral( node.right );
15-
} else {
16-
return node.type === 'Literal';
17-
}
18-
}
19-
20-
function joinLiterals( node ) {
21-
if ( node.type === 'BinaryExpression' ) {
22-
return joinLiterals( node.left ) + joinLiterals( node.right );
23-
}
24-
/* istanbul ignore else */
25-
if ( node.type === 'Literal' ) {
26-
return node.value;
27-
}
28-
/* istanbul ignore next */
29-
throw new Error( 'Non-literal node passed to joinLiteral' );
30-
}
31-
3212
module.exports = {
3313
meta: {
3414
type: 'suggestion',
@@ -94,7 +74,7 @@ module.exports = {
9474
let expectedTag;
9575
const arg = node.arguments[ 0 ];
9676
if ( allowSingle ) {
97-
const value = arg && allLiteral( arg ) && joinLiterals( arg );
77+
const value = arg && utils.allLiteral( arg ) && utils.joinLiterals( arg );
9878
if ( !( typeof value === 'string' && value ) || !rquickExpr.exec( value ) ) {
9979
// Empty or non-string, or non-HTML
10080
return;
@@ -122,7 +102,7 @@ module.exports = {
122102
return;
123103
}
124104
}
125-
} else if ( !( arg && allLiteral( arg ) ) ) {
105+
} else if ( !( arg && utils.allLiteral( arg ) ) ) {
126106
// Non literals passed to $.parseHTML
127107
return;
128108
}

src/rules/no-sizzle.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
const utils = require( '../utils.js' );
44

5-
function collectLiterals( node ) {
6-
if ( node.type === 'BinaryExpression' ) {
7-
return collectLiterals( node.left ) + collectLiterals( node.right );
8-
} else if ( node.type === 'Literal' ) {
9-
return node.value;
10-
} else if ( node.type === 'Identifier' ) {
11-
// Dummy value for regex matching
12-
return 'A0';
13-
} else {
14-
return '';
15-
}
16-
}
17-
185
module.exports = {
196
meta: {
207
type: 'suggestion',
@@ -79,7 +66,7 @@ module.exports = {
7966
context.options[ 0 ].allowPositional;
8067
const allowOther = context.options[ 0 ] &&
8168
context.options[ 0 ].allowOther;
82-
const value = collectLiterals( node.arguments[ 0 ] );
69+
const value = utils.joinLiterals( node.arguments[ 0 ] );
8370

8471
if ( !allowPositional && forbiddenPositional.test( value ) ) {
8572
context.report( {

src/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,27 @@ function eventShorthandFixer( node, context, fixer ) {
541541
}
542542
}
543543

544+
function allLiteral( node ) {
545+
if ( node.type === 'BinaryExpression' ) {
546+
return allLiteral( node.left ) && allLiteral( node.right );
547+
} else {
548+
return node.type === 'Literal';
549+
}
550+
}
551+
552+
function joinLiterals( node ) {
553+
if ( node.type === 'BinaryExpression' ) {
554+
return joinLiterals( node.left ) + joinLiterals( node.right );
555+
} else if ( node.type === 'Literal' ) {
556+
return node.value;
557+
} else if ( node.type === 'Identifier' ) {
558+
// Dummy value for regex matching
559+
return 'A0';
560+
} else {
561+
return '';
562+
}
563+
}
564+
544565
module.exports = {
545566
isjQuery,
546567
isjQueryConstructor,
@@ -552,5 +573,7 @@ module.exports = {
552573
createCollectionOrUtilMethodRule,
553574
eventShorthandFixer,
554575
jQueryCollectionLink,
555-
jQueryGlobalLink
576+
jQueryGlobalLink,
577+
allLiteral,
578+
joinLiterals
556579
};

0 commit comments

Comments
 (0)