Skip to content

Commit

Permalink
add global flag to forbiddenText schema, #808
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Nov 1, 2019
1 parent decad2f commit 9f69f9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
6 changes: 3 additions & 3 deletions eslint/rules/bad-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ module.exports = function( context ) {
'phetio element', // use "phet-io element" or "PhET-iO element"
'Phet-iO',

'@return ',
'@return '

// TODO: this isn't yet supported with current getBadTextTester.js
' => { return ' // if on a one line arrow function returning something, prefer instead `() => theReturn`, see https://github.com/phetsims/chipper/issues/790
// TODO: uncomment once ESP(B) lint errors fixed, see https://github.com/phetsims/energy-skate-park/issues/140
// { id: ' => { return ', global: true } // if on a one line arrow function returning something, prefer instead `() => theReturn`, see https://github.com/phetsims/chipper/issues/790
];

return {
Expand Down
57 changes: 32 additions & 25 deletions eslint/rules/getBadTextTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,33 @@ module.exports = ( badTexts, context ) => {
// no need to iterate through tokens if it isn't anywhere in the source code
if ( text.indexOf( forbiddenText.id ) >= 0 ) {

// search through the tokenized code for the forbidden code tokens, like [ 'Math', '.', 'round' ],
testCodeTokens( forbiddenText );

// look through comments
!forbiddenText.codeOnly && commentTokens.forEach( token => {
if ( token.value.indexOf( forbiddenText.id ) >= 0 ) {
context.report( {
loc: token.loc.start,
message: `bad comment text: "${forbiddenText.id}"`
} );
}
} );

// TODO: support REGEX
// if ( forbiddenText.regex instanceof RegExp && forbiddenText.regex.test( token.value ) ) {
// failedText = forbiddenText.id;
// }
// If marked as global, then report the global failure based on the source code indexOf check
if ( forbiddenText.global ) {
context.report( {
node: node,
message: 'File contains bad text: \'' + forbiddenText.id + '\''
} );
}
else {

// search through the tokenized code for the forbidden code tokens, like [ 'Math', '.', 'round' ],
testCodeTokens( forbiddenText );

// look through comments
!forbiddenText.codeOnly && commentTokens.forEach( token => {
if ( token.value.indexOf( forbiddenText.id ) >= 0 ) {
context.report( {
loc: token.loc.start,
message: `bad comment text: "${forbiddenText.id}"`
} );
}
} );

// // If a specific process couldn't find the error, then error out for the whole Node.
// !foundError && context.report( {
// node: node,
// message: 'File contains bad text: \'' + forbiddenText.id + '\''
// } );
// TODO: support REGEX
// if ( forbiddenText.regex instanceof RegExp && forbiddenText.regex.test( token.value ) ) {
// failedText = forbiddenText.id;
// }
}
}
};

Expand All @@ -113,7 +117,7 @@ module.exports = ( badTexts, context ) => {
badText = { id: badText, codeTokens: [ badText ] };
}
assert( typeof badText.id === 'string', 'id required' );
assert( Array.isArray( badText.codeTokens ), 'code tokens expected' );
assert( Array.isArray( badText.codeTokens ) || badText.global, 'code tokens or global flag expected' );
testBadText( badText );
} );
};
Expand All @@ -122,10 +126,13 @@ module.exports = ( badTexts, context ) => {
* @typedef {Object} ForbiddenTextObject
* @property {string} id - the "string-form" id of the bad text. should occur in the source code. Also what is
* displayed on error. Used when checking for bad text in comments.
* @property {Array.<string>} codeTokens - a list of the tokenized, ordered code sections that make up the bad text
* @property {Array.<string>} [codeTokens] - a list of the tokenized, ordered code sections that make up the bad text
* within the javascript code (not used for checking comments). If there
* is only one codeToken, then it will also be checked as a substring of each
* code tokens.
* code tokens. Required unless specifying "global".
* @property {boolean} [codeOnly] - if true, this object will not be checked for in comments, only in code.
* @property {boolean} [global] - if true, then ignore comment and code token checks, and just error out based on the
* presence of the `id` in the source code for the file. If provided, then codeTokens
* is not needed
*/
};

0 comments on commit 9f69f9e

Please sign in to comment.