Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
feat: no more provider require function by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Apr 30, 2018
1 parent 89f6efc commit 3243a17
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
30 changes: 18 additions & 12 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,21 +1422,27 @@ const visitors: EvaluateMap = {

const requireVar = scope.hasBinding("require");

if (requireVar) {
const requireFunc = requireVar.value;
if (requireVar === undefined) {
throw ErrNotDefined("require");
}

const targetModule: any = requireFunc(moduleName) || {};
const requireFunc = requireVar.value;

if (defaultImport) {
scope.const(
defaultImport,
targetModule.default ? targetModule.default : targetModule
);
}
if (!isFunction(requireFunc)) {
throw ErrIsNotFunction("require");
}

otherImport.forEach((varName: string) => {
scope.const(varName, targetModule[varName]);
});
const targetModule: any = requireFunc(moduleName) || {};

if (defaultImport) {
scope.const(
defaultImport,
targetModule.default ? targetModule.default : targetModule
);
}

for (const varName of otherImport) {
scope.const(varName, targetModule[varName]);
}
},
ImportDefaultSpecifier(path) {
Expand Down
20 changes: 2 additions & 18 deletions src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ export function runInContext(code: string, context: Context) {
scope.const("module", $module);
scope.var("exports", $exports);

// require can be cover
if (!scope.hasBinding("require")) {
const requireFunc =
// tslint:disable-next-line
typeof context["require"] === "function"
? // tslint:disable-next-line
context["require"]
: typeof require === "function"
? require
: function _require(id: string) {
return {};
};
scope.var("require", requireFunc);
}

const ast = parse(code, {
sourceType: "module",
plugins: [
Expand All @@ -52,12 +37,11 @@ export function runInContext(code: string, context: Context) {
]
});

const path = new Path(ast, null, scope, {});
evaluate(path);
evaluate(new Path(ast, null, scope, {}));

// exports
const moduleVar = scope.hasBinding("module");
return moduleVar ? moduleVar.value.exports : null;
return moduleVar ? moduleVar.value.exports : undefined;
}

/**
Expand Down
37 changes: 35 additions & 2 deletions test/es2015/es-module/ImportDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import test from "ava";
import { isImportDefaultSpecifier } from "babel-types";
import { parse } from "babylon";
import vm from "../../../src/vm";
import { ErrNotDefined, ErrIsNotFunction } from "../../../src/error";

test("ImportDeclaration-1", t => {
const sandbox: any = vm.createContext({});
const sandbox: any = vm.createContext({ require });

const obj: any = vm.runInContext(
`
Expand All @@ -22,7 +23,7 @@ module.exports = {b, c, d, isImportDefaultSpecifier};
});

test("ImportDeclaration-2", t => {
const sandbox: any = vm.createContext({});
const sandbox: any = vm.createContext({ require });

const outputParser: any = vm.runInContext(
`
Expand All @@ -34,3 +35,35 @@ module.exports = parse;
);
t.true(outputParser === parse);
});

test("ImportDeclaration without require", t => {
// we didnot defined require function in the context
const sandbox: any = vm.createContext({});

t.throws(function() {
vm.runInContext(
`
import {parse} from "babylon";
module.exports = parse;
`,
sandbox
);
}, ErrNotDefined("require").message);
});

test("ImportDeclaration with null require", t => {
// we didnot defined require function in the context
const sandbox: any = vm.createContext({ require: null });

t.throws(function() {
vm.runInContext(
`
import {parse} from "babylon";
module.exports = parse;
`,
sandbox
);
}, ErrIsNotFunction("require").message);
});

0 comments on commit 3243a17

Please sign in to comment.