Skip to content

Commit

Permalink
eslint rule to make sure imports are on a single line, see phetsims/p…
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg authored and zepumph committed Oct 22, 2024
1 parent 70635ae commit 2f7c839
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions eslint/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
51 changes: 51 additions & 0 deletions eslint/rules/single-line-import.js
Original file line number Diff line number Diff line change
@@ -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
];

0 comments on commit 2f7c839

Please sign in to comment.