Skip to content

Commit

Permalink
add support for sim specific bad text, phetsims/chipper#728
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 22, 2024
1 parent e521a62 commit 937d2b6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
51 changes: 51 additions & 0 deletions eslint/rules/bad-sim-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2019, University of Colorado Boulder
/* 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.
*
* @author Sam Reid (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
*/
module.exports = function( context ) {
'use strict';

var badTextsForSimCode = [

'Math.round',
'Math.random',
'_.shuffle',
'_.sample',
'_.random',
'new Random()',

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

];

// 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 + '\''
} );
}
} )
}
};
};

module.exports.schema = [
// JSON Schema for rule options goes here
];
6 changes: 5 additions & 1 deletion eslint/rules/bad-text.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright 2018, University of Colorado Boulder
// Copyright 2018-2019, University of Colorado Boulder
/* eslint-disable */

/**
* 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 ) {
Expand Down Expand Up @@ -32,6 +35,7 @@ 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();
Expand Down
2 changes: 1 addition & 1 deletion eslint/sim_es6_eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Eslint config applied only to sims that are completely written in es6, with no es5 code.
*/
module.exports = {
extends: './.eslintrc.js',
extends: './sim_eslintrc.js',
rules: {
'no-var': 2
}
Expand Down
15 changes: 15 additions & 0 deletions eslint/sim_eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018, University of Colorado Boulder
// @author Michael Kauzmann

/* eslint-env node */
'use strict';

/**
* Eslint config applied only to sims that are completely written in es6, with no es5 code.
*/
module.exports = {
extends: './.eslintrc.js',
rules: {
'bad-sim-text': 2
}
};

0 comments on commit 937d2b6

Please sign in to comment.