import type { TSESLint , TSESTree } from '@typescript-eslint/experimental-utils' ; import docsUrl from '../utils/docs-url' ; const rule = { meta : { messages : { useExistingOperator : 'Was "{{operator}}=" meant instead?' , } , schema : [ ] , type : 'problem' , docs : { description : 'Non-existent operators "=+", "=-" and "=!" should not be used' , recommended : 'error' , url : docsUrl ( __filename ) , } , } , create ( context ) { return { AssignmentExpression ( node ) { const assignmentExpression = node as TSESTree . AssignmentExpression ; if ( assignmentExpression . operator === '=' ) { checkOperator ( context , assignmentExpression . right ) ; } } , VariableDeclarator ( node ) { const variableDeclarator = node as TSESTree . VariableDeclarator ; checkOperator ( context , variableDeclarator . init ) ; } , } ; } , } ; function checkOperator ( context , unaryNode ? ) { if ( unaryNode && unaryNode . type === 'UnaryExpression' && isUnaryOperatorOfInterest ( unaryNode . operator ) ) { const sourceCode = context . getSourceCode ( ) ; const assignmentOperatorToken = sourceCode . getTokenBefore ( unaryNode , token => token . value === '=' ) ; const unaryOperatorToken = sourceCode . getFirstToken ( unaryNode ) ; const expressionFirstToken = sourceCode . getFirstToken ( unaryNode . argument ) ; if ( assignmentOperatorToken != null && unaryOperatorToken != null && expressionFirstToken != null && areAdjacent ( assignmentOperatorToken , unaryOperatorToken ) && ! areAdjacent ( unaryOperatorToken , expressionFirstToken ) ) { context . report ( { messageId : 'useExistingOperator' , data : { operator : unaryNode . operator , } , loc : { start : assignmentOperatorToken . loc . start , end : unaryOperatorToken . loc . end } , } ) ; } } } function isUnaryOperatorOfInterest ( operator ) { return operator === '-' || operator === '+' || operator === '!' ; } function areAdjacent ( first , second ) { return ( first . loc . end . column === second . loc . start . column && first . loc . end . line === second . loc . start . line ) ; } export = rule ;