UNPKG

eslint-plugin-unicorn

Version:
36 lines (29 loc) 1.36 kB
import isShorthandPropertyValue from '../utils/is-shorthand-property-value.js'; import isShorthandPropertyAssignmentPatternLeft from '../utils/is-shorthand-property-assignment-pattern-left.js'; import isShorthandImportLocal from '../utils/is-shorthand-import-local.js'; import isShorthandExportLocal from '../utils/is-shorthand-export-local.js'; export default function replaceReferenceIdentifier(identifier, replacement, context, fixer) { if ( isShorthandPropertyValue(identifier) || isShorthandPropertyAssignmentPatternLeft(identifier) ) { return fixer.replaceText(identifier, `${identifier.name}: ${replacement}`); } if (isShorthandImportLocal(identifier, context)) { return fixer.replaceText(identifier, `${identifier.name} as ${replacement}`); } if (isShorthandExportLocal(identifier, context)) { return fixer.replaceText(identifier, `${replacement} as ${identifier.name}`); } if (identifier.typeAnnotation) { const {sourceCode} = context; if (sourceCode.getCommentsInside(identifier).length > 0) { return fixer.replaceText(sourceCode.getFirstToken(identifier), replacement); } return fixer.replaceTextRange( [sourceCode.getRange(identifier)[0], sourceCode.getRange(identifier.typeAnnotation)[0]], `${replacement}${identifier.optional ? '?' : ''}`, ); } return fixer.replaceText(identifier, replacement); }