Fixes #662: exchange var for const of require statements in src/filesystem/interface.js (#668)

* exchange var for const of require statements

Changed decleration of import files from var to const in interface.js

* Fixed all areas where var was used as well as use destructing assignment for  CONST imports.

* Fixed import const statement

* Added changes from let to const in certain lines
This commit is contained in:
Brandon Wissmann 2019-01-31 20:22:46 -05:00 committed by David Humphrey
parent 701849eb5a
commit 3b4bf42604
1 changed files with 125 additions and 120 deletions

View File

@ -1,30 +1,35 @@
var { promisify } = require('es6-promisify');
'use strict';
var Path = require('../path.js');
var nop = require('../shared.js').nop;
const { promisify } = require('es6-promisify');
var Constants = require('../constants.js');
var FILE_SYSTEM_NAME = Constants.FILE_SYSTEM_NAME;
var FS_FORMAT = Constants.FS_FORMAT;
var FS_READY = Constants.FS_READY;
var FS_PENDING = Constants.FS_PENDING;
var FS_ERROR = Constants.FS_ERROR;
var FS_NODUPEIDCHECK = Constants.FS_NODUPEIDCHECK;
const Path = require('../path.js');
var providers = require('../providers/index.js');
const providers = require('../providers/index.js');
var Shell = require('../shell/shell.js');
var Intercom = require('../../lib/intercom.js');
var FSWatcher = require('../fs-watcher.js');
var Errors = require('../errors.js');
var defaultGuidFn = require('../shared.js').guid;
const Shell = require('../shell/shell.js');
const Intercom = require('../../lib/intercom.js');
const FSWatcher = require('../fs-watcher.js');
const Errors = require('../errors.js');
const {
nop,
guid: defaultGuidFn
} = require('../shared.js');
var STDIN = Constants.STDIN;
var STDOUT = Constants.STDOUT;
var STDERR = Constants.STDERR;
const {
fsConstants,
FILE_SYSTEM_NAME,
FS_FORMAT,
FS_READY,
FS_PENDING,
FS_ERROR,
FS_NODUPEIDCHECK,
STDIN,
STDOUT,
STDERR
} = require('../constants.js');
// The core fs operations live on impl
var impl = require('./implementation.js');
const impl = require('./implementation.js');
// node.js supports a calling pattern that leaves off a callback.
function maybeCallback(callback) {
@ -58,10 +63,10 @@ function toPathIfFileURL(fileURLOrPath) {
throw new Errors.EINVAL('only file: URLs are supported for paths', fileURLOrPath);
}
var pathname = fileURLOrPath.pathname;
for (var n = 0; n < pathname.length; n++) {
const pathname = fileURLOrPath.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === '%') {
var third = pathname.codePointAt(n + 2) | 0x20;
const third = pathname.codePointAt(n + 2) | 0x20;
if (pathname[n + 1] === '2' && third === 102) {
throw new Errors.EINVAL('file: URLs must not include encoded / characters', fileURLOrPath);
}
@ -87,12 +92,12 @@ function validatePath(path, allowRelative) {
}
function processPathArg(args, idx, allowRelative) {
var path = args[idx];
let path = args[idx];
path = toPathIfFileURL(path);
path = toPathIfBuffer(path);
// Some methods specifically allow for rel paths (eg symlink with srcPath)
var err = validatePath(path, allowRelative);
let err = validatePath(path, allowRelative);
if (err) {
throw err;
}
@ -133,14 +138,14 @@ function FileSystem(options, callback) {
options = options || {};
callback = callback || defaultCallback;
var flags = options.flags || [];
var guid = options.guid ? options.guid : defaultGuidFn;
var provider = options.provider || new providers.Default(options.name || FILE_SYSTEM_NAME);
const flags = options.flags || [];
const guid = options.guid ? options.guid : defaultGuidFn;
const provider = options.provider || new providers.Default(options.name || FILE_SYSTEM_NAME);
// If we're given a provider, match its name unless we get an explicit name
var name = options.name || provider.name;
var forceFormatting = flags.includes(FS_FORMAT);
const name = options.name || provider.name;
const forceFormatting = flags.includes(FS_FORMAT);
var fs = this;
const fs = this;
fs.readyState = FS_PENDING;
fs.name = name;
fs.error = null;
@ -150,20 +155,20 @@ function FileSystem(options, callback) {
fs.stderr = STDERR;
// Expose Node's fs.constants to users
fs.constants = Constants.fsConstants;
fs.constants = fsConstants;
// Node also forwards the access mode flags onto fs
fs.F_OK = Constants.fsConstants.F_OK;
fs.R_OK = Constants.fsConstants.R_OK;
fs.W_OK = Constants.fsConstants.W_OK;
fs.X_OK = Constants.fsConstants.X_OK;
fs.F_OK = fsConstants.F_OK;
fs.R_OK = fsConstants.R_OK;
fs.W_OK = fsConstants.W_OK;
fs.X_OK = fsConstants.X_OK;
// Expose Shell constructor
this.Shell = Shell.bind(undefined, this);
// Safely expose the operation queue
var queue = [];
let queue = [];
this.queueOrRun = function (operation) {
var error;
let error;
if (FS_READY === fs.readyState) {
operation.call(fs);
@ -194,7 +199,7 @@ function FileSystem(options, callback) {
options = options || {};
listener = listener || nop;
var watcher = new FSWatcher();
const watcher = new FSWatcher();
watcher.start(filename, false, options.recursive);
watcher.on('change', listener);
@ -212,7 +217,7 @@ function FileSystem(options, callback) {
// Otherwise (default) make sure this id is unused first
function guidWithCheck(callback) {
var id = guid();
const id = guid();
context.getObject(id, function (err, value) {
if (err) {
callback(err);
@ -237,7 +242,7 @@ function FileSystem(options, callback) {
if (!changes.length) {
return;
}
var intercom = Intercom.getInstance();
const intercom = Intercom.getInstance();
changes.forEach(function (change) {
intercom.emit(change.event, change.path);
});
@ -247,7 +252,7 @@ function FileSystem(options, callback) {
provider.open(function (err) {
function complete(error) {
function wrappedContext(methodName) {
var context = provider[methodName]();
let context = provider[methodName]();
context.name = name;
context.flags = flags;
context.changes = [];
@ -255,7 +260,7 @@ function FileSystem(options, callback) {
// When the context is finished, let the fs deal with any change events
context.close = function () {
var changes = context.changes;
let changes = context.changes;
broadcastChanges(changes);
changes.length = 0;
};
@ -289,7 +294,7 @@ function FileSystem(options, callback) {
return complete(err);
}
var context = provider.getReadWriteContext();
const context = provider.getReadWriteContext();
context.guid = wrappedGuidFn(context);
// Mount the filesystem, formatting if necessary
@ -364,18 +369,18 @@ function FileSystem(options, callback) {
{ name: 'writeFile', promises: true, absPathArgs: [0] },
{ name: 'write' }
].forEach(function (method) {
var methodName = method.name;
var shouldPromisify = method.promises === true;
const methodName = method.name;
const shouldPromisify = method.promises === true;
FileSystem.prototype[methodName] = function () {
var fs = this;
var args = Array.prototype.slice.call(arguments, 0);
var lastArgIndex = args.length - 1;
const fs = this;
const args = Array.prototype.slice.call(arguments, 0);
const lastArgIndex = args.length - 1;
// We may or may not get a callback, and since node.js supports
// fire-and-forget style fs operations, we have to dance a bit here.
var missingCallback = typeof args[lastArgIndex] !== 'function';
var callback = maybeCallback(args[lastArgIndex]);
const missingCallback = typeof args[lastArgIndex] !== 'function';
const callback = maybeCallback(args[lastArgIndex]);
// Deal with path arguments, validating and normalizing Buffer and file:// URLs
if (method.absPathArgs) {
@ -385,13 +390,13 @@ function FileSystem(options, callback) {
method.relPathArgs.forEach(pathArg => processPathArg(args, pathArg, true));
}
var error = fs.queueOrRun(function() {
var context = fs.provider.openReadWriteContext();
const error = fs.queueOrRun(function () {
const context = fs.provider.openReadWriteContext();
// Fail early if the filesystem is in an error state (e.g.,
// provider failed to open.
if (FS_ERROR === fs.readyState) {
var err = new Errors.EFILESYSTEMERROR('filesystem unavailable, operation canceled');
const err = new Errors.EFILESYSTEMERROR('filesystem unavailable, operation canceled');
return callback.call(fs, err);
}
@ -411,7 +416,7 @@ function FileSystem(options, callback) {
// Forward this call to the impl's version, using the following
// call signature, with complete() as the callback/last-arg now:
// fn(fs, context, arg0, arg1, ... , complete);
var fnArgs = [context].concat(args);
const fnArgs = [context].concat(args);
impl[methodName].apply(null, fnArgs);
});
if (error) {