babel-plugin-aff-mock
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -9,40 +9,45 @@ const fs = require("fs"); | ||
CallExpression(path) { | ||
if (!api.types.isMemberExpression(path.node.callee)) { | ||
return; | ||
try { | ||
if (!api.types.isMemberExpression(path.node.callee)) { | ||
return; | ||
} | ||
const comments = path.node.leadingComments || []; | ||
const comment = comments.filter((item) => item.value.includes("aff-mock:"))[0]; | ||
if (!comment) { | ||
return; | ||
} | ||
const data = comment.value.split(":"); | ||
const pathUrl = data[1].trim(); | ||
if (!fs.existsSync(pathLib.join(process.cwd(), pathUrl))) { | ||
return; | ||
} | ||
const type = pathLib.basename(pathUrl).replace(pathLib.extname(pathUrl), "").split(".").pop(); | ||
if (!type || (type !== "reject" && type !== "resolve")) { | ||
return; | ||
} | ||
const mockData = JSON.stringify(Function(`return ${fs.readFileSync(pathLib.join(process.cwd(), pathUrl)).toString()}`)()); | ||
let replaceExpression = ""; | ||
if (type === "resolve") { | ||
replaceExpression = api.template.ast(`Promise.resolve((function(){console.error("Mocking ... Mocking");console.error("Mocking ... Mocking");return ${mockData};})())`); | ||
} | ||
if (type === "reject") { | ||
replaceExpression = api.template.ast(`Promise.reject((function(){console.error("Mocking ... Mocking");console.error("Mocking ... Mocking");return ${mockData};})())`); | ||
} | ||
if (!replaceExpression) { | ||
return; | ||
} | ||
path.replaceWith(replaceExpression); | ||
path.skip(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
const comments = path.node.leadingComments || []; | ||
const comment = comments.filter((item) => item.value.includes("aff-mock:"))[0]; | ||
if (!comment) { | ||
return; | ||
} | ||
const data = comment.value.split(":"); | ||
const type = data[1].toLocaleLowerCase(); | ||
if (!type || (type !== "reject" && type !== "resolve")) { | ||
return; | ||
} | ||
const pathUrl = data[2].trim(); | ||
if (!fs.existsSync(pathLib.join(process.cwd(), pathUrl))) { | ||
return; | ||
} | ||
const mockData = JSON.stringify(Function(`return ${fs.readFileSync(pathLib.join(process.cwd(), pathUrl)).toString()}`)()); | ||
let replaceExpression = ""; | ||
if (type === "resolve") { | ||
replaceExpression = api.template.ast(`Promise.resolve((function(){console.error("Mocking ... Mocking");console.error("Mocking ... Mocking");return ${mockData};})())`); | ||
} | ||
if (type === "reject") { | ||
replaceExpression = api.template.ast(`Promise.reject((function(){console.error("Mocking ... Mocking");console.error("Mocking ... Mocking");return ${mockData};})())`); | ||
} | ||
if (!replaceExpression) { | ||
return; | ||
} | ||
path.replaceWith(replaceExpression); | ||
path.skip(); | ||
} | ||
@@ -49,0 +54,0 @@ } |
{ | ||
"name": "babel-plugin-aff-mock", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "AFF Mock", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2349
44