diff --git a/eslint/rules/bad-sim-text.js b/eslint/rules/bad-sim-text.js new file mode 100644 index 00000000..464379a3 --- /dev/null +++ b/eslint/rules/bad-sim-text.js @@ -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 +]; \ No newline at end of file diff --git a/eslint/rules/bad-text.js b/eslint/rules/bad-text.js index 261a5e66..267c09dc 100644 --- a/eslint/rules/bad-text.js +++ b/eslint/rules/bad-text.js @@ -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 ) { @@ -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(); diff --git a/eslint/sim_es6_eslintrc.js b/eslint/sim_es6_eslintrc.js index b5bd62ef..28c0908a 100644 --- a/eslint/sim_es6_eslintrc.js +++ b/eslint/sim_es6_eslintrc.js @@ -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 } diff --git a/eslint/sim_eslintrc.js b/eslint/sim_eslintrc.js new file mode 100644 index 00000000..8edef041 --- /dev/null +++ b/eslint/sim_eslintrc.js @@ -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 + } +}; \ No newline at end of file