Skip to content

Commit

Permalink
add lint rule for toFixed, consolidate bad-text implementations, phet…
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 22, 2024
1 parent 29442cf commit ef4c0eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
26 changes: 9 additions & 17 deletions eslint/rules/bad-sim-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
/* eslint-disable */

/**
* NOTE: this file is a duplicate of bad-text.js, but it is only run on sim specific code, if you are looking for
* adding general bad text, see `./bad-text.js`
*
* Lint detector for invalid text. Checks the entire file and does not correctly report line number.
* Lint is disabled for this file so the bad texts aren't themselves flagged.
*
Expand All @@ -14,6 +11,8 @@
module.exports = function( context ) {
'use strict';

const getBadTextTester = require( './getBadTextTester' );

var badTextsForSimCode = [

'Math.round',
Expand All @@ -25,24 +24,17 @@ module.exports = function( context ) {

// IE doesn't support:
'Number.parseInt()',
'Array.prototype.find'
'Array.prototype.find',

// support regex with english names this way
{
name: '.toFixed(',
regex: new RegExp( '(?<!Util)\\.toFixed\\(' )
},
];

// NOTE: this code is duplicated in `bad-text.js`, don't edit this without updating there too
return {
Program: function( node ) {
var sourceCode = context.getSourceCode();
var text = sourceCode.text;
badTextsForSimCode.forEach( function( badText ) {
if ( text.indexOf( badText ) >= 0 ) {
context.report( {
node: node,
message: 'File contains bad text: \'' + badText + '\''
} );
}
} )
}
Program: getBadTextTester( badTextsForSimCode, context )
};
};

Expand Down
19 changes: 3 additions & 16 deletions eslint/rules/bad-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* Lint detector for invalid text. Checks the entire file and does not correctly report line number.
* Lint is disabled for this file so the bad texts aren't themselves flagged.
*
* NOTE: this lint rule is applied to all lintable files in the project. If you are looking to add rules that only apply
* to sim specific code, see `./bad-sim-text.js`
*
* @author Sam Reid (PhET Interactive Simulations)
*/
module.exports = function( context ) {
'use strict';

const getBadTextTester = require( './getBadTextTester' );

var badTexts = [

// Proper casing for *boxes
Expand All @@ -35,20 +34,8 @@ module.exports = function( context ) {
'@return '
];

// NOTE: this code is duplicated in `bad-text.js`, don't edit this without updating there too
return {
Program: function( node ) {
var sourceCode = context.getSourceCode();
var text = sourceCode.text;
badTexts.forEach( function( badText ) {
if ( text.indexOf( badText ) >= 0 ) {
context.report( {
node: node,
message: 'File contains bad text: \'' + badText + '\''
} );
}
} )
}
Program: getBadTextTester( badTexts, context )
};
};

Expand Down
29 changes: 29 additions & 0 deletions eslint/rules/getBadTextTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019, University of Colorado Boulder
/* eslint-disable */

/**
* Bad text testing function for testing bad text in the project. Supports bad text as string or regex
* @param {Array.<string|Object>} badTexts - if object, should look like {name: {string}, regex: {Regexp}}
* @param {Object} context - eslinting context given from the engine
*/
module.exports = ( badTexts, context ) => {
'use strict';

return node => {
const sourceCode = context.getSourceCode();
const text = sourceCode.text;
badTexts.forEach( badText => {
let failedText = null;
if ( badText.regex instanceof RegExp && badText.regex.test( text ) ) {
failedText = badText.name;
}
if ( text.indexOf( badText ) >= 0 ) {
failedText = badText;
}
failedText && context.report( {
node: node,
message: 'File contains bad text: \'' + failedText + '\''
} );
} );
};
};

0 comments on commit ef4c0eb

Please sign in to comment.