Skip to content

Commit

Permalink
Update lint rule to handle type definitions involving angle brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Aug 5, 2018
1 parent e584036 commit b0946a1
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ function main( context ) {
// Need to go down one more level for optional parameters:
type = type.expression;
}
if ( type.type === 'TypeApplication' ) {
// Case: type definiton with angle brackets such as Array<number>
typedef = type.expression.name;
replacement = correctTypedef( typedef );
if ( replacement ) {
report( typedef, replacement, loc );
}
typedef = type.applications[ 0 ].name;
replacement = correctTypedef( typedef );
if ( replacement ) {
report( typedef, replacement, loc );
}
}
if (
type.type === 'AllLiteral' ||
type.type === 'NullLiteral' ||
Expand All @@ -137,7 +150,19 @@ function main( context ) {
// Check individual types of type unions (e.g., `(Date|string)`)...
for ( j = 0; j < type.elements.length; j++ ) {
elem = type.elements[ j ];
if (
if ( elem.type === 'TypeApplication' ) {
typedef = elem.expression.name;
replacement = correctTypedef( typedef );
if ( replacement ) {
report( typedef, replacement, loc );
}
typedef = elem.applications[ 0 ].name;
replacement = correctTypedef( typedef );
if ( replacement ) {
report( typedef, replacement, loc );
}
}
else if (
elem.type !== 'AllLiteral' &&
elem.type !== 'NullLiteral' &&
elem.type !== 'UndefinedLiteral'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,204 @@ test = {
};
invalid.push( test );

test = {
'code': [
'/**',
'* Splits values into two groups according to a predicate function and outputs results as element indices.',
'*',
'* @private',
'* @param {Collection} collection - input collection',
'* @param {Options} opts - function options',
'* @param {*} [opts.thisArg] - execution context',
'* @param {Function} predicate - predicate function specifying which group an element in the input collection belongs to',
'* @returns {(Array<nmber>|Array)} results',
'*',
'* @example',
'* function predicate( v ) {',
'* return v[ 0 ] === \'b\';',
'* }',
'* var arr = [ \'beep\', \'boop\', \'foo\', \'bar\' ];',
'*',
'* var out = bifurcateBy( arr, {}, predicate );',
'* // returns [ [ 0, 1, 3 ], [ 2 ] ]',
'*/',
'function bifurcateBy( collection, opts, predicate ) {',
' var thisArg;',
' var bool;',
' var out;',
' var len;',
' var i;',
'',
' thisArg = opts.thisArg;',
' len = collection.length;',
' if ( len === 0 ) {',
' return [];',
' }',
' out = [ [], [] ];',
' for ( i = 0; i < len; i++ ) {',
' bool = predicate.call( thisArg, collection[ i ], i );',
' if ( bool ) {',
' out[ 0 ].push( i );',
' } else {',
' out[ 1 ].push( i );',
' }',
' }',
' return out;',
'}'
].join( '\n' ),
'errors': [
{
'message': '"nmber" type definition should be "number"',
'type': null
}
]
};
invalid.push( test );

test = {
'code': [
'/**',
'* Splits values into two groups according to a predicate function and outputs results as element indices.',
'*',
'* @private',
'* @param {Collection} collection - input collection',
'* @param {Options} opts - function options',
'* @param {*} [opts.thisArg] - execution context',
'* @param {Function} predicate - predicate function specifying which group an element in the input collection belongs to',
'* @returns {(array<nmber>|Array)} results',
'*',
'* @example',
'* function predicate( v ) {',
'* return v[ 0 ] === \'b\';',
'* }',
'* var arr = [ \'beep\', \'boop\', \'foo\', \'bar\' ];',
'*',
'* var out = bifurcateBy( arr, {}, predicate );',
'* // returns [ [ 0, 1, 3 ], [ 2 ] ]',
'*/',
'function bifurcateBy( collection, opts, predicate ) {',
' var thisArg;',
' var bool;',
' var out;',
' var len;',
' var i;',
'',
' thisArg = opts.thisArg;',
' len = collection.length;',
' if ( len === 0 ) {',
' return [];',
' }',
' out = [ [], [] ];',
' for ( i = 0; i < len; i++ ) {',
' bool = predicate.call( thisArg, collection[ i ], i );',
' if ( bool ) {',
' out[ 0 ].push( i );',
' } else {',
' out[ 1 ].push( i );',
' }',
' }',
' return out;',
'}'
].join( '\n' ),
'errors': [
{
'message': '"array" type definition should be "Array"',
'type': null
},
{
'message': '"nmber" type definition should be "number"',
'type': null
}
]
};
invalid.push( test );

test = {
'code': [
'/**',
'* Calculates the "rank" of a `require` path.',
'*',
'* @private',
'* @param {StringArray} order - require statement order',
'* @param {Array<Regexp>} regexps - regular expression array for detecting custom require statement types',
'* @param {string} name - require path',
'* @returns {integer} path rank',
'*/',
'function rank( order, regexps, name ) {',
' var type;',
' var i;',
'',
' type = \'external\';',
' if ( isNodeBuiltin( name ) ) {',
' type = \'builtin\';',
' } else if (',
' startsWith( name, \'./\' ) ||',
' startsWith( name, \'/\' ) ||',
' startsWith( name, \'../\' )',
' ) {',
' type = \'path\';',
' }',
' for ( i = 0; i < regexps.length; i++ ) {',
' if ( regexps[ i ].test( name ) ) {',
' type = \'/\'+regexps[ i ].source+\'/\';',
' break;',
' }',
' }',
' return indexOf( order, type );',
'}'
].join( '\n' ),
'errors': [
{
'message': '"Regexp" type definition should be "RegExp"',
'type': null
}
]
};
invalid.push( test );

test = {
'code': [
'/**',
'* Calculates the "rank" of a `require` path.',
'*',
'* @private',
'* @param {StringArray} order - require statement order',
'* @param {Aray<RegExp>} regexps - regular expression array for detecting custom require statement types',
'* @param {string} name - require path',
'* @returns {integer} path rank',
'*/',
'function rank( order, regexps, name ) {',
' var type;',
' var i;',
'',
' type = \'external\';',
' if ( isNodeBuiltin( name ) ) {',
' type = \'builtin\';',
' } else if (',
' startsWith( name, \'./\' ) ||',
' startsWith( name, \'/\' ) ||',
' startsWith( name, \'../\' )',
' ) {',
' type = \'path\';',
' }',
' for ( i = 0; i < regexps.length; i++ ) {',
' if ( regexps[ i ].test( name ) ) {',
' type = \'/\'+regexps[ i ].source+\'/\';',
' break;',
' }',
' }',
' return indexOf( order, type );',
'}'
].join( '\n' ),
'errors': [
{
'message': '"Aray" type definition should be "Array"',
'type': null
}
]
};
invalid.push( test );


// EXPORTS //

Expand Down
Loading

0 comments on commit b0946a1

Please sign in to comment.