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