Skip to content

Commit

Permalink
ESLint Plugin: Add fixer implementation for dependency-group
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Feb 8, 2019
1 parent b3edc7f commit ce8af4f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
15 changes: 15 additions & 0 deletions packages/eslint-plugin/rules/__tests__/dependencies-docblocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,28 @@ import edit from './edit';`,
{
code: `
import { get } from 'lodash';
import classnames from 'classnames';
import { Component } from '@wordpress/element';
import edit from './edit';`,
errors: [
{ message: 'Expected preceding "External dependencies" comment block' },
{ message: 'Expected preceding "WordPress dependencies" comment block' },
{ message: 'Expected preceding "Internal dependencies" comment block' },
],
output: `
/**
* External dependencies
*/
import { get } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import edit from './edit';`,
},
],
} );
59 changes: 39 additions & 20 deletions packages/eslint-plugin/rules/dependency-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
url: 'https://github.com/WordPress/gutenberg/blob/master/packages/eslint-plugin/docs/rules/dependency-group.md',
},
schema: [],
fixable: true,
},
create( context ) {
const comments = context.getSourceCode().getAllComments();
Expand Down Expand Up @@ -92,33 +93,26 @@ module.exports = {
} );
}

/**
* Given an import source string and node occurrence from which the
* source is derived, tests that the package source is satisfied by a
* preceding comment block of appropriate locality.
*
* @param {string} source Import source string.
* @param {espree.Node} node Node from which import source derived.
*/
function testPackage( source, node ) {
const locality = getPackageLocality( source );
if ( ! isPrecededByDependencyBlock( node, locality ) ) {
context.report( {
node,
message: `Expected preceding "${ locality } dependencies" comment block`,
} );
}
}

return {
Program( node ) {
/**
* The set of package localities which have been reported for
* the current program. Each locality is reported at most one
* time, since otherwise the fixer would insert a comment
* block for each individual import statement.
*
* @type {Set<WPPackageLocality>}
*/
const reported = new Set();

// Since we only care to enforce imports which occur at the
// top-level scope, match on Program and test its children,
// rather than matching the import nodes directly.
node.body.forEach( ( child ) => {
let source;
switch ( child.type ) {
case 'ImportDeclaration':
testPackage( child.source.value, child );
source = child.source.value;
break;

case 'CallExpression':
Expand All @@ -129,10 +123,35 @@ module.exports = {
args[ 0 ].type === 'Literal' &&
typeof args[ 0 ].value === 'string'
) {
testPackage( args[ 0 ].value, child );
source = args[ 0 ].value;
}
break;
}

if ( ! source ) {
return;
}

const locality = getPackageLocality( source );
if (
reported.has( locality ) ||
isPrecededByDependencyBlock( child, locality )
) {
return;
}

reported.add( locality );

context.report( {
node: child,
message: `Expected preceding "${ locality } dependencies" comment block`,
fix( fixer ) {
return fixer.insertTextBefore(
child,
`/**\n * ${ locality } dependencies\n */\n`
);
},
} );
} );
},
};
Expand Down

0 comments on commit ce8af4f

Please sign in to comment.