Socket
Socket
Sign inDemoInstall

babel-plugin-aff-mock

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-aff-mock - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

79

lib/index.js

@@ -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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc