Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Last active January 1, 2024 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsoncodehk/1ea329e54fb89e3caa638991008f3d4f to your computer and use it in GitHub Desktop.
Save johnsoncodehk/1ea329e54fb89e3caa638991008f3d4f to your computer and use it in GitHub Desktop.
/**
*
* @type {import('@tsslint/config').Rule}
*/
const noConsoleRule = ({ typescript: ts, sourceFile, reportWarning }) => {
ts.forEachChild(sourceFile, function walk(node) {
if (
ts.isPropertyAccessExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === 'console'
) {
reportWarning(
`Calls to 'console.x' are not allowed.`,
node.parent.getStart(sourceFile),
node.parent.getEnd()
).withFix(
`Remove 'console.${node.name.text}'`,
() => [{
fileName: sourceFile.fileName,
textChanges: [{
newText: '/* deleted */',
span: {
start: node.parent.getStart(sourceFile),
length: node.parent.getEnd() - node.parent.getStart(sourceFile),
},
}],
}]
);
}
ts.forEachChild(node, walk);
});
};
export default noConsoleRule;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment