-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rename-identifier and rename-property (#139)
- Loading branch information
Showing
13 changed files
with
169 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/shopify-codemod/test/fixtures/rename-identifier/basic.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
foo(); | ||
foo.baz = 42; | ||
const foo = 'bar'; | ||
something.foo(); |
4 changes: 4 additions & 0 deletions
4
packages/shopify-codemod/test/fixtures/rename-identifier/basic.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
bar(); | ||
bar.baz = 42; | ||
const bar = 'bar'; | ||
something.foo(); |
7 changes: 7 additions & 0 deletions
7
packages/shopify-codemod/test/fixtures/rename-property/basic.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
_.something(); | ||
_.first(); | ||
_.head(); | ||
_.each.bind(); | ||
foo._.each(); | ||
foo.each(); | ||
_[each](); |
7 changes: 7 additions & 0 deletions
7
packages/shopify-codemod/test/fixtures/rename-property/basic.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
_.something(); | ||
_.head(); | ||
_.head(); | ||
_.forEach.bind(); | ||
foo._.each(); | ||
foo.each(); | ||
_[each](); |
8 changes: 8 additions & 0 deletions
8
packages/shopify-codemod/test/transforms/rename-identifier.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'test-helper'; | ||
import transform from 'rename-identifier'; | ||
|
||
describe('renameIdentifier', () => { | ||
it('renames root identifiers', () => { | ||
expect(transform).to.transform('rename-identifier/basic', {renameIdentifiers: {foo: 'bar'}}); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/shopify-codemod/test/transforms/rename-property.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'test-helper'; | ||
import transform from 'rename-property'; | ||
|
||
describe('renameProperty', () => { | ||
it('transformed object-property pairs to new names', () => { | ||
expect(transform).to.transform('rename-property/basic', { | ||
renameProperties: { | ||
_: { | ||
first: 'head', | ||
each: 'forEach', | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {pathIsFirstMember} from './utils'; | ||
|
||
export default function renameIdentifier({source}, {jscodeshift: j}, {printOptions = {quote: 'single'}, renameIdentifiers = {}}) { | ||
|
||
if (Object.keys(renameIdentifiers).length === 0) { return null; } | ||
|
||
return j(source) | ||
.find(j.Identifier, {name: (name) => renameIdentifiers.hasOwnProperty(name)}) | ||
.filter(pathIsFirstMember) | ||
.replaceWith(({node: {name}}) => j.identifier(renameIdentifiers[name])) | ||
.toSource(printOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {pathIsFirstMember} from './utils'; | ||
|
||
export default function renameProperty({source}, {jscodeshift: j}, {printOptions = {quote: 'single'}, renameProperties = {}}) { | ||
function hasPropertyThatShouldBeRenamed({node: {computed, object, property}}) { | ||
return !computed && renameProperties[object.name].hasOwnProperty(property.name); | ||
} | ||
|
||
if (Object.keys(renameProperties).length === 0) { return null; } | ||
|
||
return j(source) | ||
.find(j.MemberExpression, {object: {name: (name) => renameProperties.hasOwnProperty(name)}}) | ||
.filter((path) => pathIsFirstMember(path) && hasPropertyThatShouldBeRenamed(path)) | ||
.forEach((path) => { | ||
const {node: {object, property}} = path; | ||
path.get('property').replace(j.identifier(renameProperties[object.name][property.name])); | ||
}) | ||
.toSource(printOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters