Commit 15b11fce authored by Robert Knight's avatar Robert Knight

Replace "var" with "const" or "let" everywhere

This is the result of using jscodeshift [1] to apply the "no-vars"
transform from js-codemod [2] on all code in scripts/ and to gulpfile.js

[1] https://github.com/facebook/jscodeshift
[2] https://github.com/cpojer/js-codemod
parent 920be1a3
This diff is collapsed.
...@@ -37,19 +37,19 @@ ...@@ -37,19 +37,19 @@
// the last expression in a function, so the generated // the last expression in a function, so the generated
// JS source has to do the same. // JS source has to do the same.
var Checker = require('jscs'); const Checker = require('jscs');
var babylon = require('babylon'); const babylon = require('babylon');
var decaffeinate = require('decaffeinate'); const decaffeinate = require('decaffeinate');
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var typescriptFormatter = require('typescript-formatter'); const typescriptFormatter = require('typescript-formatter');
var inFile = process.argv[2]; const inFile = process.argv[2];
var jscsConfigPath = require.resolve('../../.jscsrc'); const jscsConfigPath = require.resolve('../../.jscsrc');
var jscsConfig = JSON.parse(fs.readFileSync(jscsConfigPath, 'utf-8')); const jscsConfig = JSON.parse(fs.readFileSync(jscsConfigPath, 'utf-8'));
var stripReturnPatterns = [ const stripReturnPatterns = [
// Unit test cases // Unit test cases
/it\(/, /it\(/,
// Assignments in setters etc. // Assignments in setters etc.
...@@ -75,10 +75,10 @@ function stripUnnecessaryReturns(js) { ...@@ -75,10 +75,10 @@ function stripUnnecessaryReturns(js) {
// If we need something more sophisticated, we shouldn't modify the // If we need something more sophisticated, we shouldn't modify the
// source as a string but should instead write a Babel code transformer. // source as a string but should instead write a Babel code transformer.
return js.split('\n').map(line => { return js.split('\n').map(line => {
var returnPrefix = 'return '; const returnPrefix = 'return ';
if (line.trim().startsWith(returnPrefix)) { if (line.trim().startsWith(returnPrefix)) {
var remainder = line.trim().slice(returnPrefix.length); const remainder = line.trim().slice(returnPrefix.length);
for (var i=0; i < stripReturnPatterns.length; i++) { for (let i=0; i < stripReturnPatterns.length; i++) {
if (remainder.match(stripReturnPatterns[i])) { if (remainder.match(stripReturnPatterns[i])) {
return remainder; return remainder;
} }
...@@ -101,9 +101,9 @@ function checkSyntax(js) { ...@@ -101,9 +101,9 @@ function checkSyntax(js) {
try { try {
babylon.parse(js, {sourceType: 'module'}); babylon.parse(js, {sourceType: 'module'});
} catch (err) { } catch (err) {
var context = js.split('\n').reduce((context, line, index) => { const context = js.split('\n').reduce((context, line, index) => {
var lineNumber = index+1; const lineNumber = index+1;
var linePrefix; let linePrefix;
if (lineNumber === err.loc.line) { if (lineNumber === err.loc.line) {
linePrefix = `**${lineNumber}`; linePrefix = `**${lineNumber}`;
} else { } else {
...@@ -147,7 +147,7 @@ function reformat(js) { ...@@ -147,7 +147,7 @@ function reformat(js) {
return result.dest; return result.dest;
}) })
.then(result => { .then(result => {
var checker = new Checker(); const checker = new Checker();
checker.configure(jscsConfig); checker.configure(jscsConfig);
return checker.fixString(result).output; return checker.fixString(result).output;
}); });
...@@ -164,7 +164,7 @@ function toResultOrError(promise) { ...@@ -164,7 +164,7 @@ function toResultOrError(promise) {
function convertFile(inFile, outFile) { function convertFile(inFile, outFile) {
console.log('Converting', inFile); console.log('Converting', inFile);
var js; let js;
try { try {
js = decaffeinate.convert( js = decaffeinate.convert(
...@@ -179,10 +179,10 @@ function convertFile(inFile, outFile) { ...@@ -179,10 +179,10 @@ function convertFile(inFile, outFile) {
}); });
} }
var conversions = []; const conversions = [];
process.argv.slice(2).forEach(filePath => { process.argv.slice(2).forEach(filePath => {
var inFile = path.resolve(filePath); const inFile = path.resolve(filePath);
var outFile = inFile.replace(/\.coffee$/, '.js'); const outFile = inFile.replace(/\.coffee$/, '.js');
conversions.push(toResultOrError(convertFile(inFile, outFile)).then(function (result) { conversions.push(toResultOrError(convertFile(inFile, outFile)).then(function (result) {
result.fileName = inFile; result.fileName = inFile;
return result; return result;
...@@ -190,8 +190,8 @@ process.argv.slice(2).forEach(filePath => { ...@@ -190,8 +190,8 @@ process.argv.slice(2).forEach(filePath => {
}); });
Promise.all(conversions).then(results => { Promise.all(conversions).then(results => {
var ok = 0; let ok = 0;
var failed = 0; let failed = 0;
results.forEach(result => { results.forEach(result => {
if (result.error) { if (result.error) {
console.log('Error converting %s: \n\n%s', result.fileName, result.error.message); console.log('Error converting %s: \n\n%s', result.fileName, result.error.message);
......
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
*/ */
'use strict'; 'use strict';
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var babelify = require('babelify'); const babelify = require('babelify');
var browserify = require('browserify'); const browserify = require('browserify');
var coffeeify = require('coffeeify'); const coffeeify = require('coffeeify');
var exorcist = require('exorcist'); const exorcist = require('exorcist');
var gulpUtil = require('gulp-util'); const gulpUtil = require('gulp-util');
var mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
var uglifyify = require('uglifyify'); const uglifyify = require('uglifyify');
var watchify = require('watchify'); const watchify = require('watchify');
var log = gulpUtil.log; const log = gulpUtil.log;
function streamFinished(stream) { function streamFinished(stream) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
...@@ -65,7 +65,7 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -65,7 +65,7 @@ module.exports = function createBundle(config, buildOpts) {
buildOpts = buildOpts || {watch: false}; buildOpts = buildOpts || {watch: false};
var bundleOpts = { const bundleOpts = {
debug: true, debug: true,
extensions: ['.coffee'], extensions: ['.coffee'],
...@@ -115,7 +115,7 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -115,7 +115,7 @@ module.exports = function createBundle(config, buildOpts) {
bundleOpts.noParse = (config.noParse || []).map(function (id) { bundleOpts.noParse = (config.noParse || []).map(function (id) {
// If package.json specifies a custom entry point for the module for // If package.json specifies a custom entry point for the module for
// use in the browser, resolve that. // use in the browser, resolve that.
var packageConfig = require('../../package.json'); const packageConfig = require('../../package.json');
if (packageConfig.browser && packageConfig.browser[id]) { if (packageConfig.browser && packageConfig.browser[id]) {
return require.resolve('../../' + packageConfig.browser[id]); return require.resolve('../../' + packageConfig.browser[id]);
} else { } else {
...@@ -123,13 +123,13 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -123,13 +123,13 @@ module.exports = function createBundle(config, buildOpts) {
} }
}); });
var name = config.name; const name = config.name;
var bundleFileName = name + '.bundle.js'; const bundleFileName = name + '.bundle.js';
var bundlePath = config.path + '/' + bundleFileName; const bundlePath = config.path + '/' + bundleFileName;
var sourcemapPath = bundlePath + '.map'; const sourcemapPath = bundlePath + '.map';
var bundle = browserify([], bundleOpts); const bundle = browserify([], bundleOpts);
(config.require || []).forEach(function (req) { (config.require || []).forEach(function (req) {
// When another bundle uses 'bundle.external(<module path>)', // When another bundle uses 'bundle.external(<module path>)',
...@@ -146,8 +146,8 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -146,8 +146,8 @@ module.exports = function createBundle(config, buildOpts) {
// If the require path is absolute, the same rules as // If the require path is absolute, the same rules as
// above apply but the path needs to be relative to // above apply but the path needs to be relative to
// the root of the repository // the root of the repository
var repoRootPath = path.join(__dirname, '../../'); const repoRootPath = path.join(__dirname, '../../');
var relativePath = path.relative(path.resolve(repoRootPath), const relativePath = path.relative(path.resolve(repoRootPath),
path.resolve(req)); path.resolve(req));
bundle.require(req, {expose: '/' + relativePath}); bundle.require(req, {expose: '/' + relativePath});
} else { } else {
...@@ -174,12 +174,12 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -174,12 +174,12 @@ module.exports = function createBundle(config, buildOpts) {
} }
function build() { function build() {
var output = fs.createWriteStream(bundlePath); const output = fs.createWriteStream(bundlePath);
var b = bundle.bundle(); const b = bundle.bundle();
b.on('error', function (err) { b.on('error', function (err) {
log('Build error', err.toString()); log('Build error', err.toString());
}); });
var stream = b.pipe(exorcist(sourcemapPath)) const stream = b.pipe(exorcist(sourcemapPath))
.pipe(output); .pipe(output);
return streamFinished(stream); return streamFinished(stream);
} }
...@@ -187,7 +187,7 @@ module.exports = function createBundle(config, buildOpts) { ...@@ -187,7 +187,7 @@ module.exports = function createBundle(config, buildOpts) {
if (buildOpts.watch) { if (buildOpts.watch) {
bundle.plugin(watchify); bundle.plugin(watchify);
bundle.on('update', function (ids) { bundle.on('update', function (ids) {
var start = Date.now(); const start = Date.now();
log('Source files changed', ids); log('Source files changed', ids);
build().then(function () { build().then(function () {
......
'use strict'; 'use strict';
var fs = require('fs'); const fs = require('fs');
var gulpUtil = require('gulp-util'); const gulpUtil = require('gulp-util');
var http = require('http'); const http = require('http');
var WebSocketServer = require('websocket').server; const WebSocketServer = require('websocket').server;
var urlParser = require('url'); const urlParser = require('url');
function readmeText() { function readmeText() {
return fs.readFileSync('./README.md', 'utf-8'); return fs.readFileSync('./README.md', 'utf-8');
...@@ -37,13 +37,13 @@ function changelogText() { ...@@ -37,13 +37,13 @@ function changelogText() {
* @constructor * @constructor
*/ */
function LiveReloadServer(port, config) { function LiveReloadServer(port, config) {
var connections = []; let connections = [];
function listen() { function listen() {
var log = gulpUtil.log; const log = gulpUtil.log;
var server = http.createServer(function (req, response) { const server = http.createServer(function (req, response) {
var url = urlParser.parse(req.url); const url = urlParser.parse(req.url);
var content; let content;
if (url.pathname === '/document/license') { if (url.pathname === '/document/license') {
content = ` content = `
...@@ -145,17 +145,17 @@ function LiveReloadServer(port, config) { ...@@ -145,17 +145,17 @@ function LiveReloadServer(port, config) {
log(`Live reload server listening at http://localhost:${port}/`); log(`Live reload server listening at http://localhost:${port}/`);
}); });
var ws = new WebSocketServer({ const ws = new WebSocketServer({
httpServer: server, httpServer: server,
}); });
ws.on('request', function (req) { ws.on('request', function (req) {
log('Live reload client connected'); log('Live reload client connected');
var conn = req.accept(null, req.origin); const conn = req.accept(null, req.origin);
connections.push(conn); connections.push(conn);
conn.on('close', function () { conn.on('close', function () {
var closedConn = conn; const closedConn = conn;
connections = connections.filter(function (conn) { connections = connections.filter(function (conn) {
return conn !== closedConn; return conn !== closedConn;
}); });
......
'use strict'; 'use strict';
var path = require('path'); const path = require('path');
var crypto = require('crypto'); const crypto = require('crypto');
var through = require('through2'); const through = require('through2');
var VinylFile = require('vinyl'); const VinylFile = require('vinyl');
/** /**
* Gulp plugin that generates a cache-busting manifest file. * Gulp plugin that generates a cache-busting manifest file.
...@@ -15,19 +15,19 @@ var VinylFile = require('vinyl'); ...@@ -15,19 +15,19 @@ var VinylFile = require('vinyl');
* to URLs with cache-busting query parameters (eg. "scripts/foo.js?af95bd"). * to URLs with cache-busting query parameters (eg. "scripts/foo.js?af95bd").
*/ */
module.exports = function (opts) { module.exports = function (opts) {
var manifest = {}; const manifest = {};
return through.obj(function (file, enc, callback) { return through.obj(function (file, enc, callback) {
var hash = crypto.createHash('sha1'); const hash = crypto.createHash('sha1');
hash.update(file.contents); hash.update(file.contents);
var hashSuffix = hash.digest('hex').slice(0, 6); const hashSuffix = hash.digest('hex').slice(0, 6);
var relPath = path.relative('build/', file.path); const relPath = path.relative('build/', file.path);
manifest[relPath] = relPath + '?' + hashSuffix; manifest[relPath] = relPath + '?' + hashSuffix;
callback(); callback();
}, function (callback) { }, function (callback) {
var manifestFile = new VinylFile({ const manifestFile = new VinylFile({
path: opts.name, path: opts.name,
contents: Buffer.from(JSON.stringify(manifest, null, 2), 'utf-8'), contents: Buffer.from(JSON.stringify(manifest, null, 2), 'utf-8'),
}); });
......
'use strict'; 'use strict';
var { readFileSync } = require('fs'); const { readFileSync } = require('fs');
var express = require('express'); const express = require('express');
var { log } = require('gulp-util'); const { log } = require('gulp-util');
var { version } = require('../../package.json'); const { version } = require('../../package.json');
/** /**
* An express server which serves the contents of the package. * An express server which serves the contents of the package.
...@@ -19,7 +19,7 @@ var { version } = require('../../package.json'); ...@@ -19,7 +19,7 @@ var { version } = require('../../package.json');
* app. * app.
*/ */
function servePackage(port, hostname) { function servePackage(port, hostname) {
var app = express(); const app = express();
// Enable CORS for assets so that cross-origin font loading works. // Enable CORS for assets so that cross-origin font loading works.
app.use(function (req, res, next) { app.use(function (req, res, next) {
...@@ -28,9 +28,9 @@ function servePackage(port, hostname) { ...@@ -28,9 +28,9 @@ function servePackage(port, hostname) {
next(); next();
}); });
var serveBootScript = function (req, res) { const serveBootScript = function (req, res) {
var entryPath = require.resolve('../..'); const entryPath = require.resolve('../..');
var entryScript = readFileSync(entryPath).toString('utf-8'); const entryScript = readFileSync(entryPath).toString('utf-8');
res.send(entryScript); res.send(entryScript);
}; };
......
'use strict'; 'use strict';
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var gulpUtil = require('gulp-util'); const gulpUtil = require('gulp-util');
var request = require('request'); const request = require('request');
var through = require('through2'); const through = require('through2');
var SENTRY_API_ROOT = 'https://app.getsentry.com/api/0'; const SENTRY_API_ROOT = 'https://app.getsentry.com/api/0';
/** /**
* interface SentryOptions { * interface SentryOptions {
...@@ -49,8 +49,8 @@ function createRelease(opts, project, release) { ...@@ -49,8 +49,8 @@ function createRelease(opts, project, release) {
}, },
json: true, json: true,
}).then(function (result) { }).then(function (result) {
var success = (result.response.statusCode === 201); const success = (result.response.statusCode === 201);
var alreadyCreated = (result.response.statusCode === 400 && const alreadyCreated = (result.response.statusCode === 400 &&
result.body.detail.match(/already exists/)); result.body.detail.match(/already exists/));
if (success || alreadyCreated) { if (success || alreadyCreated) {
...@@ -99,7 +99,7 @@ function uploadReleaseFile(opts, project, release, file) { ...@@ -99,7 +99,7 @@ function uploadReleaseFile(opts, project, release, file) {
*/ */
module.exports = function uploadToSentry(opts, projects, release) { module.exports = function uploadToSentry(opts, projects, release) {
// Create releases in every project // Create releases in every project
var releases = projects.map(function (project) { const releases = projects.map(function (project) {
gulpUtil.log(`Creating release '${release}' in project '${project}'`); gulpUtil.log(`Creating release '${release}' in project '${project}'`);
return createRelease(opts, project, release); return createRelease(opts, project, release);
}); });
...@@ -108,7 +108,7 @@ module.exports = function uploadToSentry(opts, projects, release) { ...@@ -108,7 +108,7 @@ module.exports = function uploadToSentry(opts, projects, release) {
Promise.all(releases) Promise.all(releases)
.then(function () { .then(function () {
gulpUtil.log(`Uploading ${path.basename(file.path)}`); gulpUtil.log(`Uploading ${path.basename(file.path)}`);
var uploads = projects.map(function (project) { const uploads = projects.map(function (project) {
return uploadReleaseFile(opts, project, release, file); return uploadReleaseFile(opts, project, release, file);
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment