-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move transformers into their own files (#116)
- Loading branch information
Showing
5 changed files
with
144 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
export default function transformImportCall(t, nodePath, mapper, state, cwd) { | ||
const source = nodePath.get('source'); | ||
if (source.type === 'StringLiteral') { | ||
const modulePath = mapper(source.node.value, state.file.opts.filename, state.opts, cwd); | ||
if (modulePath) { | ||
source.replaceWith(t.stringLiteral(modulePath)); | ||
} | ||
} | ||
} |
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,36 @@ | ||
export default function transformJestCalls(t, nodePath, mapper, state, cwd) { | ||
const calleePath = nodePath.get('callee'); | ||
|
||
const jestMethods = [ | ||
'genMockFromModule', | ||
'mock', | ||
'unmock', | ||
'doMock', | ||
'dontMock', | ||
]; | ||
|
||
if (!( | ||
t.isMemberExpression(calleePath.node) && | ||
t.isIdentifier(calleePath.node.object, { name: 'jest' }) && | ||
jestMethods.some(methodName => t.isIdentifier(calleePath.node.property, { name: methodName })) | ||
)) { | ||
return; | ||
} | ||
|
||
const args = nodePath.get('arguments'); | ||
if (!args.length) { | ||
return; | ||
} | ||
|
||
const moduleArg = args[0]; | ||
if (moduleArg.node.type === 'StringLiteral') { | ||
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd); | ||
if (modulePath) { | ||
const newArgs = [...args].map(a => a.node); | ||
newArgs[0] = t.stringLiteral(modulePath); | ||
nodePath.replaceWith(t.callExpression( | ||
calleePath.node, newArgs, | ||
)); | ||
} | ||
} | ||
} |
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,27 @@ | ||
export default function transformRequireCall(t, nodePath, mapper, state, cwd) { | ||
const calleePath = nodePath.get('callee'); | ||
if ( | ||
!t.isIdentifier(calleePath.node, { name: 'require' }) && | ||
!( | ||
t.isMemberExpression(calleePath.node) && | ||
t.isIdentifier(calleePath.node.object, { name: 'require' }) | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const args = nodePath.get('arguments'); | ||
if (!args.length) { | ||
return; | ||
} | ||
|
||
const moduleArg = args[0]; | ||
if (moduleArg.node.type === 'StringLiteral') { | ||
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd); | ||
if (modulePath) { | ||
nodePath.replaceWith(t.callExpression( | ||
calleePath.node, [t.stringLiteral(modulePath)], | ||
)); | ||
} | ||
} | ||
} |
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,26 @@ | ||
export default function transformSystemImportCall(t, nodePath, mapper, state, cwd) { | ||
const calleePath = nodePath.get('callee'); | ||
|
||
if (!( | ||
t.isMemberExpression(calleePath.node) && | ||
t.isIdentifier(calleePath.node.object, { name: 'System' }) && | ||
t.isIdentifier(calleePath.node.property, { name: 'import' }) | ||
)) { | ||
return; | ||
} | ||
|
||
const args = nodePath.get('arguments'); | ||
if (!args.length) { | ||
return; | ||
} | ||
|
||
const moduleArg = args[0]; | ||
if (moduleArg.node.type === 'StringLiteral') { | ||
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd); | ||
if (modulePath) { | ||
nodePath.replaceWith(t.callExpression( | ||
calleePath.node, [t.stringLiteral(modulePath)], | ||
)); | ||
} | ||
} | ||
} |