-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: can't detect Promise in the function
- Loading branch information
Showing
3 changed files
with
90 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ rules: | |
|
||
max-len: 1 | ||
arrow-body-style: 1 | ||
no-param-reassign: 0 |
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 |
---|---|---|
@@ -1,52 +1,63 @@ | ||
// private | ||
// target | ||
const defineName = 'Promise'; | ||
const methods = [ | ||
'resolve', | ||
'reject', | ||
'all', | ||
'race', | ||
]; | ||
|
||
/** | ||
* @module babel-plugin-transform-bluebird | ||
* @returns {babelPlugin} unknown | ||
*/ | ||
export default ({ template }) => { | ||
return ({ | ||
visitor: { | ||
Program: { | ||
exit(path) { | ||
let injectBluebird = false; | ||
for (const node of path.node.body) { | ||
const expression = node.expression || {}; | ||
const specifiers = node.specifiers || []; | ||
const declarations = node.declarations || []; | ||
export default () => ({ | ||
visitor: { | ||
Program: { | ||
enter(path, file) { | ||
file.__UNUSE_BLUEBIRD__ = false; | ||
for (const node of path.node.body) { | ||
// found "import Promise" | ||
const specifiers = node.specifiers || []; | ||
const localNames = specifiers.map((specifier) => specifier.local.name); | ||
if (localNames.indexOf(defineName) > -1) { | ||
file.__UNUSE_BLUEBIRD__ = true; | ||
return; | ||
} | ||
|
||
// found "new Promise" or "Promise.method()" | ||
const calleeName = expression.callee && (expression.callee.name || expression.callee.object.name); | ||
if (calleeName === defineName) { | ||
injectBluebird = true; | ||
} | ||
// found "var Promise" | ||
const declarations = node.declarations || []; | ||
const declarationIds = declarations.map((declaration) => declaration.id.name); | ||
if (declarationIds.indexOf(defineName) > -1) { | ||
file.__UNUSE_BLUEBIRD__ = true; | ||
return; | ||
} | ||
} | ||
}, | ||
}, | ||
|
||
// found "var foo = new Promise" or "var foo = Promise.resolve()" | ||
const declarationInits = declarations.map((declaration) => declaration.init.callee.name || declaration.init.callee.object.name); | ||
if (declarationInits.indexOf(defineName) > -1) { | ||
injectBluebird = true; | ||
} | ||
// found "new Promise" | ||
NewExpression(path, file) { | ||
if (file.__UNUSE_BLUEBIRD__) { | ||
return; | ||
} | ||
|
||
// found "import Promise" | ||
const localNames = specifiers.map((specifier) => specifier.local.name); | ||
if (localNames.indexOf(defineName) > -1) { | ||
return; | ||
} | ||
if (path.get('callee').node.name === defineName) { | ||
path.node.callee = file.addImport('bluebird', 'default'); | ||
} | ||
}, | ||
|
||
// found "var Promise" | ||
const declarationIds = declarations.map((declaration) => declaration.id.name); | ||
if (declarationIds.indexOf(defineName) > -1) { | ||
return; | ||
} | ||
} | ||
// found "Promise.methods()" | ||
CallExpression(path, file) { | ||
if (file.__UNUSE_BLUEBIRD__) { | ||
return; | ||
} | ||
|
||
if (injectBluebird) { | ||
const topNodes = [template(`var ${defineName} = require("bluebird")`)()]; | ||
path.unshiftContainer('body', topNodes); | ||
} | ||
}, | ||
}, | ||
methods.forEach((method) => { | ||
if (path.get('callee').matchesPattern(`${defineName}.${method}`)) { | ||
path.node.callee = file.addImport('bluebird', method); | ||
} | ||
}); | ||
}, | ||
}); | ||
}; | ||
}, | ||
}); |
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