Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the fixer function to eslint/rules/no-empty-comments #3340

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,22 @@ 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 by removing empty comments.
* @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 +112,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
Loading