-
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.
Minor improvements from a few more translation examples (#90)
- Loading branch information
Showing
16 changed files
with
223 additions
and
26 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
7 changes: 7 additions & 0 deletions
7
packages/shopify-codemod/test/fixtures/global-identifier-to-import/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 @@ | ||
'expose Foo.Bar'; | ||
|
||
[].map(_.identity); | ||
window.moment.fromDate(Date.now()); | ||
foo.$.find('.bar'); | ||
window.React.renderToString(<div />); | ||
jstz(Date.now()); |
11 changes: 11 additions & 0 deletions
11
packages/shopify-codemod/test/fixtures/global-identifier-to-import/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,11 @@ | ||
'expose Foo.Bar'; | ||
|
||
import jstz from 'jstimezone/jstz'; | ||
import moment from 'moment'; | ||
import _ from 'lodash'; | ||
|
||
[].map(_.identity); | ||
moment.fromDate(Date.now()); | ||
foo.$.find('.bar'); | ||
window.React.renderToString(<div />); | ||
jstz(Date.now()); |
25 changes: 25 additions & 0 deletions
25
packages/shopify-codemod/test/fixtures/remove-useless-return-from-test/iife.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,25 @@ | ||
suite('a', () => { | ||
beforeEach(function() { | ||
return (function() { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
})(); | ||
}); | ||
|
||
afterEach(() => { | ||
return (() => { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
})(); | ||
}); | ||
|
||
it(() => { | ||
return ((i) => { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
})(i); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/shopify-codemod/test/fixtures/remove-useless-return-from-test/iife.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,21 @@ | ||
suite('a', () => { | ||
beforeEach(function() { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
}); | ||
|
||
it(() => { | ||
((i) => { | ||
for (var i in [1, 2, 3]) { | ||
assert(typeof i === 'number'); | ||
} | ||
})(i); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/shopify-codemod/test/transforms/global-identifier-to-import.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,14 @@ | ||
import 'test-helper'; | ||
import globalIdentifierToImport from 'global-identifier-to-import'; | ||
|
||
describe('globalIdentifierToImport', () => { | ||
it('transforms basic setup variables', () => { | ||
expect(globalIdentifierToImport).to.transform('global-identifier-to-import/basic', { | ||
globalIdentifiers: { | ||
_: 'lodash', | ||
moment: 'moment', | ||
jstz: 'jstimezone/jstz', | ||
}, | ||
}); | ||
}); | ||
}); |
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: 1 addition & 3 deletions
4
packages/shopify-codemod/transforms/coffeescript-range-output-to-helper.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
49 changes: 49 additions & 0 deletions
49
packages/shopify-codemod/transforms/global-identifier-to-import.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,49 @@ | ||
import {insertAfterDirectives} from './utils'; | ||
|
||
export default function globalIdentifierToImport({source}, {jscodeshift: j}, {printOptions = {}, globalIdentifiers = {}}) { | ||
return j(source) | ||
.find(j.Program) | ||
.forEach((path) => { | ||
const imports = new Set(); | ||
|
||
function isGlobalIdentifierName(name) { | ||
return globalIdentifiers.hasOwnProperty(name); | ||
} | ||
|
||
function isWindow(windowPath) { | ||
return windowPath.get('name').value === 'window' && !isNestedInMemberExpression(windowPath); | ||
} | ||
|
||
function isNestedInMemberExpression(aPath) { | ||
const parentNode = aPath.parentPath.node; | ||
return j.MemberExpression.check(parentNode) && parentNode.property === aPath.node; | ||
} | ||
|
||
function isGlobalIdentifier(identifierPath) { | ||
return !isNestedInMemberExpression(identifierPath) || isWindow(identifierPath.parentPath.get('object')); | ||
} | ||
|
||
j(path) | ||
.find(j.Identifier, {name: isGlobalIdentifierName}) | ||
.filter(isGlobalIdentifier) | ||
.forEach((identifierPath) => { | ||
imports.add(identifierPath.node.name); | ||
|
||
// can only happen for window.globalIdentifier | ||
if (isNestedInMemberExpression(identifierPath)) { | ||
identifierPath.parentPath.replace(identifierPath.node); | ||
} | ||
}); | ||
|
||
const {node: {body}} = path; | ||
for (const anImport of imports.values()) { | ||
insertAfterDirectives( | ||
body, | ||
j.importDeclaration([ | ||
j.importDefaultSpecifier(j.identifier(anImport)), | ||
], j.literal(globalIdentifiers[anImport])) | ||
); | ||
} | ||
}) | ||
.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
9 changes: 1 addition & 8 deletions
9
packages/shopify-codemod/transforms/mocha-context-to-global-reference.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
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