Skip to content

Commit

Permalink
Extract renameIdentifier to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 16, 2020
1 parent 077a3e8 commit 14de276
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 65 deletions.
71 changes: 6 additions & 65 deletions rules/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const getDocumentationUrl = require('./utils/get-documentation-url');
const avoidCapture = require('./utils/avoid-capture');
const cartesianProductSamples = require('./utils/cartesian-product-samples');
const isSameNode = require('./utils/is-same-node');
const isShorthandPropertyIdentifier = require('./utils/is-shorthand-property-identifier');
const isAssignmentPatternShorthandPropertyIdentifier = require('./utils/is-assignment-pattern-shorthand-property-identifier');
const isShorthandImportIdentifier = require('./utils/is-shorthand-import-identifier');
const isShorthandExportIdentifier = require('./utils/is-shorthand-export-identifier');
const renameIdentifier = require('./utils/rename-identifier')

const isUpperCase = string => string === string.toUpperCase();
const isUpperFirst = string => isUpperCase(string[0]);
Expand Down Expand Up @@ -398,70 +403,6 @@ const shouldFix = variable => {
return !variableIdentifiers(variable).some(isExportedIdentifier);
};

const isShorthandPropertyIdentifier = identifier => {
return (
identifier.parent.type === 'Property' &&
identifier.parent.shorthand &&
isSameNode(identifier, identifier.parent.key)
);
};

const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
return (
identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
isSameNode(identifier, identifier.parent.parent.key) &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand
);
};

const isShorthandImportIdentifier = identifier => {
return (
identifier.parent.type === 'ImportSpecifier' &&
identifier.parent.imported.name === identifier.name &&
identifier.parent.local.name === identifier.name
);
};

const isShorthandExportIdentifier = identifier => {
return (
identifier.parent.type === 'ExportSpecifier' &&
identifier.parent.exported.name === identifier.name &&
identifier.parent.local.name === identifier.name
);
};

const fixIdentifier = (fixer, replacement, sourceCode) => identifier => {
if (
isShorthandPropertyIdentifier(identifier) ||
isAssignmentPatternShorthandPropertyIdentifier(identifier)
) {
return fixer.replaceText(identifier, `${identifier.name}: ${replacement}`);
}

if (isShorthandImportIdentifier(identifier)) {
return fixer.replaceText(identifier, `${identifier.name} as ${replacement}`);
}

if (isShorthandExportIdentifier(identifier)) {
return fixer.replaceText(identifier, `${replacement} as ${identifier.name}`);
}

// `TypeParameter` default value
if (identifier.default) {
return fixer.replaceText(identifier, `${replacement} = ${sourceCode.getText(identifier.default)}`);
}

// `typeAnnotation`
if (identifier.typeAnnotation) {
return fixer.replaceText(identifier, `${replacement}${sourceCode.getText(identifier.typeAnnotation)}`);
}

return fixer.replaceText(identifier, replacement);
};

const isDefaultOrNamespaceImportName = identifier => {
if (
identifier.parent.type === 'ImportDefaultSpecifier' &&
Expand Down Expand Up @@ -694,7 +635,7 @@ const create = context => {

problem.fix = fixer => {
return variableIdentifiers(variable)
.map(fixIdentifier(fixer, replacement, sourceCode));
.map(identifier => renameIdentifier(identifier, replacement, fixer, sourceCode));
};
}

Expand Down
11 changes: 11 additions & 0 deletions rules/utils/is-assignment-pattern-shorthand-property-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const isSameNode = require('./is-same-node');

module.exports = identifier =>
identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
isSameNode(identifier, identifier.parent.parent.key) &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand
6 changes: 6 additions & 0 deletions rules/utils/is-shorthand-export-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = identifier =>
identifier.parent.type === 'ExportSpecifier' &&
identifier.parent.exported.name === identifier.name &&
identifier.parent.local.name === identifier.name
8 changes: 8 additions & 0 deletions rules/utils/is-shorthand-import-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const isSameNode = require('./is-same-node');

module.exports = identifier =>
identifier.parent.type === 'ImportSpecifier' &&
identifier.parent.imported.name === identifier.name &&
identifier.parent.local.name === identifier.name
8 changes: 8 additions & 0 deletions rules/utils/is-shorthand-property-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const isSameNode = require('./is-same-node');

module.exports = identifier =>
identifier.parent.type === 'Property' &&
identifier.parent.shorthand &&
isSameNode(identifier, identifier.parent.key);
37 changes: 37 additions & 0 deletions rules/utils/rename-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 14de276

Please sign in to comment.