securityos/node_modules/eslint-plugin-unicorn/rules/fix/replace-reference-identifie...

36 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
'use strict';
const isShorthandPropertyValue = require('../utils/is-shorthand-property-value.js');
const isShorthandPropertyAssignmentPatternLeft = require('../utils/is-shorthand-property-assignment-pattern-left.js');
const isShorthandImportLocal = require('../utils/is-shorthand-import-local.js');
const isShorthandExportLocal = require('../utils/is-shorthand-export-local.js');
function replaceReferenceIdentifier(identifier, replacement, fixer) {
if (
isShorthandPropertyValue(identifier)
|| isShorthandPropertyAssignmentPatternLeft(identifier)
) {
return fixer.replaceText(identifier, `${identifier.name}: ${replacement}`);
}
if (isShorthandImportLocal(identifier)) {
return fixer.replaceText(identifier, `${identifier.name} as ${replacement}`);
}
if (isShorthandExportLocal(identifier)) {
return fixer.replaceText(identifier, `${replacement} as ${identifier.name}`);
}
// `typeAnnotation`
if (identifier.typeAnnotation) {
return fixer.replaceTextRange(
[identifier.range[0], identifier.typeAnnotation.range[0]],
`${replacement}${identifier.optional ? '?' : ''}`,
);
}
return fixer.replaceText(identifier, replacement);
}
module.exports = replaceReferenceIdentifier;