Skip to content

Commit

Permalink
refactor: add prefer-const to .eslintrc + fix errors (electron#14880)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored and MarshallOfSound committed Oct 2, 2018
1 parent 07161a8 commit 3ad3ade
Show file tree
Hide file tree
Showing 47 changed files with 239 additions and 238 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"browser": true
},
"rules": {
"no-var": "error"
"no-var": "error",
"prefer-const": ["error", {
"destructuring": "all"
}]
}
}
6 changes: 3 additions & 3 deletions lib/browser/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Object.assign(app, {

const nativeFn = app.getAppMetrics
app.getAppMetrics = () => {
let metrics = nativeFn.call(app)
const metrics = nativeFn.call(app)
for (const metric of metrics) {
if ('memory' in metric) {
deprecate.removeProperty(metric, 'memory')
Expand Down Expand Up @@ -96,7 +96,7 @@ app.allowNTLMCredentialsForAllDomains = function (allow) {
if (!process.noDeprecations) {
deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains')
}
let domains = allow ? '*' : ''
const domains = allow ? '*' : ''
if (!this.isReady()) {
this.commandLine.appendSwitch('auth-server-whitelist', domains)
} else {
Expand All @@ -106,7 +106,7 @@ app.allowNTLMCredentialsForAllDomains = function (allow) {

// Routes the events to webContents.
const events = ['login', 'certificate-error', 'select-client-certificate']
for (let name of events) {
for (const name of events) {
app.on(name, (event, webContents, ...args) => {
webContents.emit(name, event, ...args)
})
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/auto-updater/squirrel-update-win.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isSameArgs = (args) => args.length === spawnedArgs.length && args.every((e

// Spawn a command and invoke the callback when it completes with an error
// and the output from standard out.
let spawnUpdate = function (args, detached, callback) {
const spawnUpdate = function (args, detached, callback) {
let error, errorEmitted, stderr, stdout

try {
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/api/browser-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ BrowserWindow.prototype._init = function () {
this.webContents.on('-add-new-contents', (event, webContents, disposition,
userGesture, left, top, width,
height) => {
let urlFrameName = v8Util.getHiddenValue(webContents, 'url-framename')
const urlFrameName = v8Util.getHiddenValue(webContents, 'url-framename')
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
disposition !== 'background-tab') || !urlFrameName) {
event.preventDefault()
return
}

let { url, frameName } = urlFrameName
const { url, frameName } = urlFrameName
v8Util.deleteHiddenValue(webContents, 'url-framename')
const options = {
show: true,
Expand Down Expand Up @@ -115,7 +115,7 @@ BrowserWindow.prototype._init = function () {
}

const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
for (let event of visibilityEvents) {
for (const event of visibilityEvents) {
this.on(event, visibilityChanged)
}

Expand Down Expand Up @@ -145,7 +145,7 @@ BrowserWindow.getAllWindows = () => {
}

BrowserWindow.getFocusedWindow = () => {
for (let window of BrowserWindow.getAllWindows()) {
for (const window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window
}
return null
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ module.exports = {
},

showCertificateTrustDialog: function (...args) {
let [window, options, callback] = parseArgs(...args)
const [window, options, callback] = parseArgs(...args)

if (options == null || typeof options !== 'object') {
throw new TypeError('options must be an object')
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MenuItem = function (options) {
const { Menu } = require('electron')

// Preserve extra fields specified by user
for (let key in options) {
for (const key in options) {
if (!(key in this)) this[key] = options[key]
}
if (typeof this.role === 'string' || this.role instanceof String) {
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function areValidTemplateItems (template) {

function sortTemplate (template) {
const sorted = sortMenuItems(template)
for (let id in sorted) {
for (const id in sorted) {
const item = sorted[id]
if (Array.isArray(item.submenu)) {
item.submenu = sortTemplate(item.submenu)
Expand Down
3 changes: 1 addition & 2 deletions lib/browser/api/navigation-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ const NavigationController = (function () {
}

NavigationController.prototype.goToOffset = function (offset) {
let pendingIndex
if (!this.canGoToOffset(offset)) {
return
}
pendingIndex = this.currentIndex + offset
const pendingIndex = this.currentIndex + offset
if (this.inPageIndex > -1 && pendingIndex >= this.inPageIndex) {
this.pendingIndex = pendingIndex
return this.webContents._goToOffset(offset)
Expand Down
18 changes: 9 additions & 9 deletions lib/browser/api/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ClientRequest extends EventEmitter {
let urlStr = options.url

if (!urlStr) {
let urlObj = {}
const urlObj = {}
const protocol = options.protocol || 'http:'
if (!kSupportedProtocols.has(protocol)) {
throw new Error('Protocol "' + protocol + '" not supported. ')
Expand Down Expand Up @@ -149,7 +149,7 @@ class ClientRequest extends EventEmitter {
// an invalid request.
throw new TypeError('Request path contains unescaped characters.')
}
let pathObj = url.parse(options.path || '/')
const pathObj = url.parse(options.path || '/')
urlObj.pathname = pathObj.pathname
urlObj.search = pathObj.search
urlObj.hash = pathObj.hash
Expand All @@ -161,7 +161,7 @@ class ClientRequest extends EventEmitter {
throw new Error('redirect mode should be one of follow, error or manual')
}

let urlRequestOptions = {
const urlRequestOptions = {
method: method,
url: urlStr,
redirect: redirectPolicy
Expand All @@ -180,7 +180,7 @@ class ClientRequest extends EventEmitter {
}
}

let urlRequest = new URLRequest(urlRequestOptions)
const urlRequest = new URLRequest(urlRequestOptions)

// Set back and forward links.
this.urlRequest = urlRequest
Expand All @@ -192,7 +192,7 @@ class ClientRequest extends EventEmitter {
this.extraHeaders = {}

if (options.headers) {
for (let key in options.headers) {
for (const key in options.headers) {
this.setHeader(key, options.headers[key])
}
}
Expand Down Expand Up @@ -286,8 +286,8 @@ class ClientRequest extends EventEmitter {
}

_write (chunk, encoding, callback, isLast) {
let chunkIsString = typeof chunk === 'string'
let chunkIsBuffer = chunk instanceof Buffer
const chunkIsString = typeof chunk === 'string'
const chunkIsBuffer = chunk instanceof Buffer
if (!chunkIsString && !chunkIsBuffer) {
throw new TypeError('First argument must be a string or Buffer.')
}
Expand All @@ -306,7 +306,7 @@ class ClientRequest extends EventEmitter {

// Headers are assumed to be sent on first call to _writeBuffer,
// i.e. after the first call to write or end.
let result = this.urlRequest.write(chunk, isLast)
const result = this.urlRequest.write(chunk, isLast)

// The write callback is fired asynchronously to mimic Node.js.
if (callback) {
Expand All @@ -318,7 +318,7 @@ class ClientRequest extends EventEmitter {

write (data, encoding, callback) {
if (this.urlRequest.finished) {
let error = new Error('Write after end.')
const error = new Error('Write after end.')
process.nextTick(writeAfterEndNT, this, error, callback)
return true
}
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ module.exports = {

getFocusedWebContents () {
let focused = null
for (let contents of binding.getAllWebContents()) {
for (const contents of binding.getAllWebContents()) {
if (!contents.isFocused()) continue
if (focused == null) focused = contents
// Return webview web contents which may be embedded inside another
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/objects-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ObjectsRegistry {
// then garbage collected in old page).
remove (webContents, contextId, id) {
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
const owner = this.owners[ownerKey]
if (owner) {
// Remove the reference in owner.
owner.delete(id)
Expand All @@ -63,10 +63,10 @@ class ObjectsRegistry {
// Clear all references to objects refrenced by the WebContents.
clear (webContents, contextId) {
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
const owner = this.owners[ownerKey]
if (!owner) return

for (let id of owner) this.dereference(id)
for (const id of owner) this.dereference(id)

delete this.owners[ownerKey]
}
Expand All @@ -87,7 +87,7 @@ class ObjectsRegistry {

// Private: Dereference the object from store.
dereference (id) {
let pointer = this.storage[id]
const pointer = this.storage[id]
if (pointer == null) {
return
}
Expand Down
32 changes: 16 additions & 16 deletions lib/browser/rpc-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const getObjectMembers = function (object) {
}
// Map properties to descriptors.
return names.map((name) => {
let descriptor = Object.getOwnPropertyDescriptor(object, name)
let member = { name, enumerable: descriptor.enumerable, writable: false }
const descriptor = Object.getOwnPropertyDescriptor(object, name)
const member = { name, enumerable: descriptor.enumerable, writable: false }
if (descriptor.get === undefined && typeof object[name] === 'function') {
member.type = 'method'
} else {
Expand All @@ -51,7 +51,7 @@ const getObjectMembers = function (object) {

// Return the description of object's prototype.
const getObjectPrototype = function (object) {
let proto = Object.getPrototypeOf(object)
const proto = Object.getPrototypeOf(object)
if (proto === null || proto === Object.prototype) return null
return {
members: getObjectMembers(proto),
Expand Down Expand Up @@ -189,7 +189,7 @@ const unwrapArgs = function (sender, contextId, args) {
then: metaToValue(meta.then)
})
case 'object': {
let ret = {}
const ret = {}
Object.defineProperty(ret.constructor, 'name', { value: meta.name })

for (const { name, value } of meta.members) {
Expand All @@ -213,7 +213,7 @@ const unwrapArgs = function (sender, contextId, args) {
}

const processId = sender.getProcessId()
let callIntoRenderer = function (...args) {
const callIntoRenderer = function (...args) {
if (!sender.isDestroyed() && processId === sender.getProcessId()) {
sender.send('ELECTRON_RENDERER_CALLBACK', contextId, meta.id, valueToMeta(sender, contextId, args))
} else {
Expand Down Expand Up @@ -295,7 +295,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, co

handleRemoteCommand('ELECTRON_BROWSER_CONSTRUCTOR', function (event, contextId, id, args) {
args = unwrapArgs(event.sender, contextId, args)
let constructor = objectsRegistry.get(id)
const constructor = objectsRegistry.get(id)

if (constructor == null) {
throwRPCError(`Cannot call constructor on missing remote object ${id}`)
Expand All @@ -306,7 +306,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CONSTRUCTOR', function (event, contextId,

handleRemoteCommand('ELECTRON_BROWSER_FUNCTION_CALL', function (event, contextId, id, args) {
args = unwrapArgs(event.sender, contextId, args)
let func = objectsRegistry.get(id)
const func = objectsRegistry.get(id)

if (func == null) {
throwRPCError(`Cannot call function on missing remote object ${id}`)
Expand All @@ -317,7 +317,7 @@ handleRemoteCommand('ELECTRON_BROWSER_FUNCTION_CALL', function (event, contextId

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, contextId, id, method, args) {
args = unwrapArgs(event.sender, contextId, args)
let object = objectsRegistry.get(id)
const object = objectsRegistry.get(id)

if (object == null) {
throwRPCError(`Cannot call constructor '${method}' on missing remote object ${id}`)
Expand All @@ -328,7 +328,7 @@ handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, cont

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId, id, method, args) {
args = unwrapArgs(event.sender, contextId, args)
let obj = objectsRegistry.get(id)
const obj = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot call function '${method}' on missing remote object ${id}`)
Expand All @@ -339,7 +339,7 @@ handleRemoteCommand('ELECTRON_BROWSER_MEMBER_CALL', function (event, contextId,

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_SET', function (event, contextId, id, name, args) {
args = unwrapArgs(event.sender, contextId, args)
let obj = objectsRegistry.get(id)
const obj = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot set property '${name}' on missing remote object ${id}`)
Expand All @@ -350,7 +350,7 @@ handleRemoteCommand('ELECTRON_BROWSER_MEMBER_SET', function (event, contextId, i
})

handleRemoteCommand('ELECTRON_BROWSER_MEMBER_GET', function (event, contextId, id, name) {
let obj = objectsRegistry.get(id)
const obj = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot get property '${name}' on missing remote object ${id}`)
Expand All @@ -369,14 +369,14 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
})

handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
let guestViewManager = require('@electron/internal/browser/guest-view-manager')
const guestViewManager = require('@electron/internal/browser/guest-view-manager')
return valueToMeta(event.sender, contextId, guestViewManager.getGuest(guestInstanceId))
})

ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, requestId, guestInstanceId, method, args, hasCallback) {
new Promise(resolve => {
let guestViewManager = require('./guest-view-manager')
let guest = guestViewManager.getGuest(guestInstanceId)
const guestViewManager = require('./guest-view-manager')
const guest = guestViewManager.getGuest(guestInstanceId)
if (guest.hostWebContents !== event.sender) {
throw new Error('Access denied')
}
Expand All @@ -396,8 +396,8 @@ ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, request

ipcMain.on('ELECTRON_BROWSER_SYNC_CALL_TO_GUEST_VIEW', function (event, guestInstanceId, method, args) {
try {
let guestViewManager = require('@electron/internal/browser/guest-view-manager')
let guest = guestViewManager.getGuest(guestInstanceId)
const guestViewManager = require('@electron/internal/browser/guest-view-manager')
const guest = guestViewManager.getGuest(guestInstanceId)
if (guest.hostWebContents !== event.sender) {
throw new Error('Access denied')
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/api/crash-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const binding = process.atomBinding('crash_reporter')

const sendSync = function (channel, ...args) {
if (process.type === 'browser') {
let event = {}
const event = {}
electron.ipcMain.emit(channel, event, ...args)
return event.returnValue
} else {
Expand Down
Loading

0 comments on commit 3ad3ade

Please sign in to comment.