Last active
January 1, 2024 19:50
-
-
Save johnsoncodehk/1ea329e54fb89e3caa638991008f3d4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @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