diff --git a/eslint/.eslintrc.js b/eslint/.eslintrc.js index 5940a59c..de6237d0 100644 --- a/eslint/.eslintrc.js +++ b/eslint/.eslintrc.js @@ -76,6 +76,9 @@ module.exports = { // Custom rule for avoiding instanceof Array. 'no-instanceof-array': 2, + // Custom rule for keeping import statements on a single line. + 'single-line-import': 2, + // disallow declaration of variables that are not used in the code (recommended) // Overriden to allow unused args 'no-unused-vars': [ diff --git a/eslint/rules/single-line-import.js b/eslint/rules/single-line-import.js new file mode 100644 index 00000000..a92a4b4e --- /dev/null +++ b/eslint/rules/single-line-import.js @@ -0,0 +1,51 @@ +// Copyright 2016-2020, University of Colorado Boulder +/** + * @fileoverview Rule to check that import statements are on single lines. Automated tools + * and processes at PhET assume that imports are on a single line so this is important to enforce. + * + * @author Jesse Greenberg (PhET Interactive Simulations) + * @copyright 2020 University of Colorado Boulder + */ + +/* eslint-env node */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = context => { + + return { + + ImportDeclaration: node => { + if ( node.loc.start.line !== node.loc.end.line ) { + + // AST JSON might look something like: + // { + // "type": "ImportDeclaration", + // "specifiers": [ + // { + // "type": "ImportDefaultSpecifier", + // "local": { + // "type": "Identifier", + // "name": "EnergySkateParkColorScheme", + // } + // } + // ] + // } + node.specifiers.forEach( specifier => { + context.report( { + node: node, + loc: node.loc, + message: specifier.local.name + ': import statement should be on a single line.' + } ); + } ); + } + } + }; +}; + +module.exports.schema = [ + // JSON Schema for rule options goes here +]; \ No newline at end of file