Skip to content

Commit

Permalink
Use more ES6 in node code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton authored and benjamn committed Mar 27, 2017
1 parent 95e5702 commit a7a4c17
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 60 deletions.
5 changes: 2 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var objToStr = Object.prototype.toString;
var objStr = objToStr.call({});

function isPlainObject(value) {
exports.isPlainObject = function (value) {
return objToStr.call(value) === objStr;
}
exports.isPlainObject = isPlainObject;
};

exports.getNamesFromPattern = function (pattern) {
var queue = [pattern];
Expand Down
78 changes: 42 additions & 36 deletions node/caching-compiler.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
var fs = require("fs");
var path = require("path");
var createHash = require("crypto").createHash;
var compile = require("../lib/compiler.js").compile;
var dynRequire = module.require ? module.require.bind(module) : __non_webpack_require__;
var hasOwn = Object.prototype.hasOwnProperty;
"use strict";

const fs = require("fs");
const path = require("path");
const createHash = require("crypto").createHash;
const compile = require("../lib/compiler.js").compile;
const dynRequire = module.require ? module.require.bind(module) : __non_webpack_require__;
const hasOwn = Object.prototype.hasOwnProperty;

// Map from absolute file paths to the package.json that governs them.
var pkgInfoCache = Object.create(null);
const pkgInfoCache = Object.create(null);

// Take only the major and minor components of the reify version, so that
// we don't invalidate the cache every time a patch version is released.
var reifyVersion = require("../package.json")
const reifyVersion = require("../package.json")
.version.split(".", 2).join(".");

var DEFAULT_CACHE_DIR = ".reify-cache";
const DEFAULT_CACHE_DIR = ".reify-cache";

exports.compile = function (content, filename) {
exports.compile = (content, filename) => {
if (filename === "repl") {
// Treat the REPL as if there was no filename.
filename = null;
}

var info = filename ? getPkgInfo(filename) : fallbackPkgInfo;
const info = filename ? getPkgInfo(filename) : fallbackPkgInfo;
if (! info) {
return content;
}
Expand All @@ -42,19 +44,19 @@ function readWithCacheAndFilename(info, content, filename) {

// Used when compile filename argument is falsy. Enables in-memory
// caching, at least.
var fallbackPkgInfo = {
const fallbackPkgInfo = {
cache: Object.create(null)
};

function readWithCache(info, content) {
var json = info && info.json;
var reify = json && json.reify;
var cacheFilename = getCacheFilename(content, reify);
var absCachePath = typeof info.cacheDir === "string" &&
const json = info && info.json;
const reify = json && json.reify;
const cacheFilename = getCacheFilename(content, reify);
const absCachePath = typeof info.cacheDir === "string" &&
path.join(info.cacheDir, cacheFilename);

if (hasOwn.call(info.cache, cacheFilename)) {
var cacheValue = info.cache[cacheFilename];
let cacheValue = info.cache[cacheFilename];

if (cacheValue === true && absCachePath) {
cacheValue = info.cache[cacheFilename] =
Expand All @@ -66,7 +68,7 @@ function readWithCache(info, content) {
}
}

var compileOptions = {
const compileOptions = {
ast: false
};

Expand Down Expand Up @@ -102,7 +104,7 @@ function readFileOrNull(filename) {
}

function getCacheFilename(content, reify) {
var hash = createHash("sha1")
const hash = createHash("sha1")
.update(reifyVersion).update("\0")
.update(content).update("\0");

Expand All @@ -118,7 +120,7 @@ function getPkgInfo(filename) {
return pkgInfoCache[filename];
}

var stat = statOrNull(filename);
const stat = statOrNull(filename);
if (! stat) {
return pkgInfoCache[filename] = null;
}
Expand All @@ -128,13 +130,13 @@ function getPkgInfo(filename) {
return pkgInfoCache[filename] = null;
}

var info = readPkgInfo(filename);
const info = readPkgInfo(filename);
if (info) {
return pkgInfoCache[filename] = info;
}
}

var parentDir = path.dirname(filename);
const parentDir = path.dirname(filename);
return pkgInfoCache[filename] =
parentDir !== filename &&
getPkgInfo(parentDir);
Expand All @@ -149,8 +151,10 @@ function statOrNull(filename) {
}

function readPkgInfo(dir) {
let pkg;

try {
var pkg = JSON.parse(fs.readFileSync(
pkg = JSON.parse(fs.readFileSync(
path.join(dir, "package.json")
));

Expand All @@ -162,17 +166,16 @@ function readPkgInfo(dir) {
}

if (pkg && typeof pkg === "object") {
var reify = pkg.reify;
const reify = pkg.reify;
if (reify === false) {
// An explicit "reify": false property in package.json disables
// reification even if "reify" is listed as a dependency.
return null;
}

function check(name) {
return typeof pkg[name] === "object" &&
hasOwn.call(pkg[name], "reify");
}
const check = (name) => (
typeof pkg[name] === "object" && hasOwn.call(pkg[name], "reify")
);

if (! check("dependencies") &&
! check("peerDependencies") &&
Expand All @@ -186,34 +189,37 @@ function readPkgInfo(dir) {
return null;
}

var info = {
const info = {
json: pkg,
cacheDir: null,
cache: Object.create(null)
};

if (reify) {
var cacheDir = hasOwn.call(reify, "cache-directory")
let cacheDir = hasOwn.call(reify, "cache-directory")
? reify["cache-directory"]
: DEFAULT_CACHE_DIR;

if (typeof cacheDir === "string") {
cacheDir = mkdirp(dir, cacheDir);

var cacheFiles = cacheDir && fs.readdirSync(cacheDir);
const cacheFiles = cacheDir && fs.readdirSync(cacheDir);
if (cacheFiles) {
// If we leave info.cacheDir === null, we won't be able to save
// cache files to disk, but we can still cache compilation
// results in memory.
info.cacheDir = cacheDir;

cacheFiles.forEach(function (file) {
const filesCount = cacheFiles.length;

for (let i = 0; i < filesCount; ++i) {
// Later we'll change the value to the actual contents of the
// file, but for now we merely register that it exists.
const file = cacheFiles[i];
if (/\.js$/.test(file)) {
info.cache[file] = true;
}
});
}
}
}
}
Expand All @@ -232,7 +238,7 @@ function getOwn(obj, name) {
}

function mkdirp(rootDir, relativeDir) {
var parentDir = path.dirname(relativeDir);
const parentDir = path.dirname(relativeDir);
if (parentDir === relativeDir) {
return rootDir;
}
Expand All @@ -241,8 +247,8 @@ function mkdirp(rootDir, relativeDir) {
return null;
}

var absoluteDir = path.join(rootDir, relativeDir);
var stat = statOrNull(absoluteDir);
const absoluteDir = path.join(rootDir, relativeDir);
const stat = statOrNull(absoluteDir);
if (stat && stat.isDirectory()) {
return absoluteDir;
}
Expand Down
10 changes: 6 additions & 4 deletions node/compile-hook.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var compile = require("./caching-compiler.js").compile;
var Module = require("./runtime.js").Module;
var Mp = Module.prototype;
"use strict";

const compile = require("./caching-compiler.js").compile;
const Module = require("./runtime.js").Module;
const Mp = Module.prototype;

// Override Module.prototype._compile to compile any code that will be
// evaluated as a module.
var _compile = Mp._compile;
const _compile = Mp._compile;
if (! _compile.reified) {
(Mp._compile = function (content, filename) {
return _compile.call(
Expand Down
12 changes: 7 additions & 5 deletions node/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var dynRequire = module.require ? module.require.bind(module) : __non_webpack_require__;
var Module = require("../lib/runtime.js").enable(dynRequire('module'));
var Mp = Module.prototype;
"use strict";

const dynRequire = module.require ? module.require.bind(module) : __non_webpack_require__;
const Module = require("../lib/runtime.js").enable(dynRequire('module'));
const Mp = Module.prototype;

exports.Module = Module;

Expand All @@ -11,10 +13,10 @@ Mp.resolve = function (id) {

// Override Module.prototype.load to call this.runModuleSetters() whenever
// a module has loaded.
var load = Mp.load;
const load = Mp.load;
if (! load.reified && Mp.runModuleSetters) {
(Mp.load = function () {
var result = load.apply(this, arguments);
const result = load.apply(this, arguments);
this.runModuleSetters();
return result;
}).reified = load;
Expand Down
25 changes: 13 additions & 12 deletions repl/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
var vm = require("vm");
var compile = require("../node/caching-compiler.js").compile;
"use strict";

const vm = require("vm");
const compile = require("../node/caching-compiler.js").compile;

require("../node");

function wrap(name, optionsArgIndex) {
var method = vm[name];
const method = vm[name];

if (typeof method !== "function") {
return;
}

if (method.reified) {
if (typeof method !== "function" ||
method.reified) {
return;
}

vm[name] = function (code) {
var options = arguments[optionsArgIndex];
var filename = options && options.filename;
var args = [compile(code, filename)];
for (var i = 1; i < arguments.length; ++i) {
const options = arguments[optionsArgIndex];
const filename = options && options.filename;
const args = [compile(code, filename)];
const argsCount = arguments.length;

for (let i = 1; i < argsCount; ++i) {
args.push(arguments[i]);
}
return method.apply(vm, args);
Expand Down

0 comments on commit a7a4c17

Please sign in to comment.