Skip to content

Commit

Permalink
feat: add the fixer function to eslint/rules/no-empty-comments
Browse files Browse the repository at this point in the history
PR-URL: #3340

Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
Signed-off-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
headlessNode and Planeshifter authored Dec 7, 2024
1 parent fdee038 commit b47ab4f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ function main(context) {
context.report({
'node': null,
'message': 'Empty comments are not allowed',
'loc': comment.loc
'loc': comment.loc,
'fix': fix
});

/**
* Fixes the lint error.
*
* @private
* @param {Function} fixer - ESLint fixer
* @returns {(Object|null)} fix or null
*/
function fix( fixer ) {
if ( comment.type === 'Block' || comment.type === 'Line' ) {
return fixer.removeRange( comment.range );
}
return null;
}
}

/**
Expand Down Expand Up @@ -98,11 +113,12 @@ function main(context) {

rule = {
'meta': {
'type': 'layout',
'docs': {
'description': 'enforce that comments are not empty'
},
'schema': [],
'fixable': null
'fixable': 'code',
'schema': []
},
'create': main
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ test = {
'message': 'Empty comments are not allowed',
'type': null
}
]
],
'output': [
'function pow2( x ) {',
' ',
' return x*x;',
'}'
].join( '\n' )
};
invalid.push( test );

Expand All @@ -55,7 +61,19 @@ test = {
'message': 'Empty comments are not allowed',
'type': null
}
]
],
'output': [
'function fizzBuzz() {',
' var out;',
' var i;',
'',
' for ( i = 1; i <= 100; i++ ) {',
' out = ( i % 5 === 0 ) ? "Buzz" : ( i % 3 === 0 ) ? "Fizz" : i;',
' ',
' console.log( out );',
' }',
'}'
].join( '\n' )
};
invalid.push( test );

Expand All @@ -81,7 +99,19 @@ test = {
'message': 'Empty comments are not allowed',
'type': null
}
]
],
'output': [
'function makePerson() {',
' var person = {',
' ',
' \'title\': \'engineer\',',
'',
' ',
' \'name\': \'Susan\'',
' };',
' return person;',
'}'
].join( '\n' )
};
invalid.push( test );

Expand All @@ -102,7 +132,18 @@ test = {
'message': 'Empty comments are not allowed',
'type': null
}
]
],
'output': [
'function square( x ) {',
' var out;',
' var x;',
'',
' out = x*x;',
' ',
'',
' return out;',
'}'
].join( '\n' )
};
invalid.push( test );

Expand Down

1 comment on commit b47ab4f

@kgryte
Copy link
Member

@kgryte kgryte commented on b47ab4f Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@headlessNode Nice! Thanks for adding this fixer!

Please sign in to comment.