-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
rename-identifier.js
37 lines (29 loc) · 1.26 KB
/
rename-identifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const isShorthandPropertyIdentifier = require('./is-shorthand-property-identifier');
const isAssignmentPatternShorthandPropertyIdentifier = require('./is-assignment-pattern-shorthand-property-identifier');
const isShorthandImportIdentifier = require('./is-shorthand-import-identifier');
const isShorthandExportIdentifier = require('./is-shorthand-export-identifier');
function renameIdentifier(identifier, name, fixer, sourceCode) {
if (
isShorthandPropertyIdentifier(identifier) ||
isAssignmentPatternShorthandPropertyIdentifier(identifier)
) {
return fixer.replaceText(identifier, `${identifier.name}: ${name}`);
}
if (isShorthandImportIdentifier(identifier)) {
return fixer.replaceText(identifier, `${identifier.name} as ${name}`);
}
if (isShorthandExportIdentifier(identifier)) {
return fixer.replaceText(identifier, `${name} as ${identifier.name}`);
}
// `TypeParameter` default value
if (identifier.default) {
return fixer.replaceText(identifier, `${name} = ${sourceCode.getText(identifier.default)}`);
}
// `typeAnnotation`
if (identifier.typeAnnotation) {
return fixer.replaceText(identifier, `${name}${sourceCode.getText(identifier.typeAnnotation)}`);
}
return fixer.replaceText(identifier, name);
}
module.exports = renameIdentifier;