Prefer const wherever possible

Summary:
Non-final identifiers make code harder to understand.
This is particularly true for JavaScript where even the *type*
can change as a value gets reassigned later.

This enforces to use `const` whereever possible, but doesn't
"outlaw" `let`. Mixed destructuring is also still allowed.

Used `eslint --fix` to change all existing cases.

Reviewed By: jknoxville

Differential Revision: D16131329

fbshipit-source-id: 2eceaca7c603b71b36e005be5d135e1849f2518d
This commit is contained in:
Pascal Hartig
2019-07-09 04:15:32 -07:00
committed by Facebook Github Bot
parent 662db20948
commit c588b650ae
34 changed files with 141 additions and 140 deletions

View File

@@ -18,56 +18,56 @@ function getAndroidLog(
test('test shouldParseAndroidLog function for type error and tag is AndroidRuntime', () => {
const referenceDate = new Date();
let log: DeviceLogEntry = getAndroidLog(
const log: DeviceLogEntry = getAndroidLog(
new Date(referenceDate.getTime() + 10000), //This log arrives 10 secs after the refernce time
'error',
'AndroidRuntime',
'Possible runtime crash',
);
let shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
const shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
expect(shouldParseTheLog).toEqual(true);
});
test('test shouldParseAndroidLog function for type non-error', () => {
const referenceDate = new Date();
let log: DeviceLogEntry = getAndroidLog(
const log: DeviceLogEntry = getAndroidLog(
new Date(referenceDate.getTime() + 10000), //This log arrives 10 secs after the refernce time
'debug',
'fb4a.activitymanager',
'Possible debug info in activitymanager',
);
let shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
const shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
expect(shouldParseTheLog).toEqual(false);
});
test('test shouldParseAndroidLog function for the older android log', () => {
const referenceDate = new Date();
let log: DeviceLogEntry = getAndroidLog(
const log: DeviceLogEntry = getAndroidLog(
new Date(referenceDate.getTime() - 10000), //This log arrives 10 secs before the refernce time
'error',
'fb4a.activitymanager',
'Possible error info in activitymanager',
);
let shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
const shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
expect(shouldParseTheLog).toEqual(false);
});
test('test shouldParseAndroidLog function for the fatal log', () => {
const referenceDate = new Date();
let log: DeviceLogEntry = getAndroidLog(
const log: DeviceLogEntry = getAndroidLog(
new Date(referenceDate.getTime() + 10000), //This log arrives 10 secs after the refernce time
'fatal',
'arbitrary tag',
'Possible error info in activitymanager',
);
let shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
const shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
expect(shouldParseTheLog).toEqual(true);
});
test('test shouldParseAndroidLog function for the error log which does not staisfy our tags check', () => {
const referenceDate = new Date();
let log: DeviceLogEntry = getAndroidLog(
const log: DeviceLogEntry = getAndroidLog(
new Date(referenceDate.getTime() + 10000), //This log arrives 10 secs after the refernce time
'error',
'arbitrary tag',
'Possible error info in fb4a',
);
let shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
const shouldParseTheLog = shouldParseAndroidLog(log, referenceDate);
expect(shouldParseTheLog).toEqual(false);
});

View File

@@ -35,7 +35,7 @@ function generateClientIdentifierWithSalt(
identifier: string,
salt: string,
): string {
let array = identifier.split('#');
const array = identifier.split('#');
const serial = array.pop();
return array.join('#') + '#' + salt + '-' + serial;
}
@@ -198,7 +198,7 @@ test('test processStore function for an iOS device connected with client plugin
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
let expectedPluginState = {
const expectedPluginState = {
[generateClientIdentifierWithSalt(clientIdentifier, 'salt')]: {
msg: 'Test plugin',
},
@@ -258,7 +258,7 @@ test('test processStore function to have only the client for the selected device
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already added
const {clients} = json;
const {pluginStates} = json.store;
let expectedPluginState = {
const expectedPluginState = {
[generateClientIdentifierWithSalt(selectedDeviceClientIdentifier, 'salt') +
'#testapp']: {
msg: 'Test plugin selected device',
@@ -313,7 +313,7 @@ test('test processStore function to have multiple clients for the selected devic
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already added
const {clients} = json;
const {pluginStates} = json.store;
let expectedPluginState = {
const expectedPluginState = {
[generateClientIdentifierWithSalt(clientIdentifierApp1, 'salt') +
'#testapp1']: {
msg: 'Test plugin App1',
@@ -355,7 +355,7 @@ test('test processStore function for device plugin state and no clients', async
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
const {clients} = json;
let expectedPluginState = {
const expectedPluginState = {
'salt-serial#TestDevicePlugin': {msg: 'Test Device plugin'},
};
expect(pluginStates).toEqual(expectedPluginState);

View File

@@ -41,18 +41,18 @@ function mockPluginDefinition(name: string): PluginDefinition {
}
test('getActivePluginNames with the plugins getting excluded', () => {
let state = mockPluginState(
const state = mockPluginState(
[mockPluginDefinition('DevicePlugin1')],
[mockPluginDefinition('ClientPlugin1')],
[[mockPluginDefinition('DevicePlugin2'), 'DevicePlugin2']],
);
let list = getActivePluginNames(state);
const list = getActivePluginNames(state);
expect(list).toEqual(['ClientPlugin2']);
});
test('getActivePluginNames with the no plugins getting excluded', () => {
let state = mockPluginState([], [], []);
let list = getActivePluginNames(state);
const state = mockPluginState([], [], []);
const list = getActivePluginNames(state);
expect(list).toEqual([
'ClientPlugin1',
'ClientPlugin2',

View File

@@ -7,10 +7,10 @@
import promiseTimeout from '../promiseTimeout';
test('test promiseTimeout for timeout to happen', () => {
let promise = promiseTimeout(
const promise = promiseTimeout(
200,
new Promise((resolve, reject) => {
let id = setTimeout(() => {
const id = setTimeout(() => {
clearTimeout(id);
resolve();
}, 500);
@@ -22,10 +22,10 @@ test('test promiseTimeout for timeout to happen', () => {
});
test('test promiseTimeout for timeout not to happen', () => {
let promise = promiseTimeout(
const promise = promiseTimeout(
200,
new Promise((resolve, reject) => {
let id = setTimeout(() => {
const id = setTimeout(() => {
clearTimeout(id);
resolve();
}, 100);

View File

@@ -19,12 +19,12 @@ class TestObject extends Object {
set: ?Set<any>;
}
test('test makeObjectSerializable function for unnested object with no Set and Map', () => {
let obj = {key1: 'value1', key2: 'value2'};
const obj = {key1: 'value1', key2: 'value2'};
const output = makeObjectSerializable(obj);
expect(output).toEqual(obj);
// Testing numbers
let obj2 = {key1: 1, key2: 2};
const obj2 = {key1: 1, key2: 2};
const output2 = makeObjectSerializable(obj2);
expect(output2).toEqual(obj2);
});
@@ -36,27 +36,27 @@ test('makeObjectSerializable function for unnested object with values which retu
});
test('test deserializeObject function for unnested object with no Set and Map', () => {
let obj = {key1: 'value1', key2: 'value2'};
const obj = {key1: 'value1', key2: 'value2'};
const output = deserializeObject(obj);
expect(output).toEqual(obj);
// Testing numbers
let obj2 = {key1: 1, key2: 2};
const obj2 = {key1: 1, key2: 2};
const output2 = deserializeObject(obj2);
expect(output2).toEqual(obj2);
});
test('test makeObjectSerializable and deserializeObject function for nested object with no Set and Map', () => {
let subObj = {key1: 'value1', key2: 'value2'};
let subObj2 = {key21: 'value21', key22: 'value22'};
let obj = {key1: subObj, key2: subObj2};
const subObj = {key1: 'value1', key2: 'value2'};
const subObj2 = {key21: 'value21', key22: 'value22'};
const obj = {key1: subObj, key2: subObj2};
const output = makeObjectSerializable(obj);
expect(output).toEqual(obj);
expect(deserializeObject(output)).toEqual(obj);
let subObjNum = {key1: 1, key2: 2};
let subObjNum2 = {key21: 21, key22: 22};
let obj2 = {key1: subObjNum, key2: subObjNum2};
const subObjNum = {key1: 1, key2: 2};
const subObjNum2 = {key21: 21, key22: 22};
const obj2 = {key1: subObjNum, key2: subObjNum2};
const output2 = makeObjectSerializable(obj2);
expect(output2).toEqual(obj2);
expect(deserializeObject(output2)).toEqual(obj2);
@@ -184,19 +184,19 @@ test('test makeObjectSerializable and deserializeObject function for custom Obje
});
test('test makeObjectSerializable and deserializeObject function for Array as input', () => {
let arr = [1, 2, 4, 5];
const arr = [1, 2, 4, 5];
const output = makeObjectSerializable(arr);
expect(output).toEqual(arr);
expect(deserializeObject(output)).toEqual(arr);
let arrMap = [
const arrMap = [
new Map([['a1', 'v1'], ['a2', 'v2']]),
new Map([['b1', 'v1'], ['b2', 'v2']]),
new Map([['c1', 'v1'], ['c2', 'v2']]),
new Map([['d1', 'v1'], ['d2', 'v2']]),
];
let expectedArr = [
const expectedArr = [
{
__flipper_object_type__: 'Map',
data: [['a1', 'v1'], ['a2', 'v2']],
@@ -218,7 +218,7 @@ test('test makeObjectSerializable and deserializeObject function for Array as in
expect(outputMap).toEqual(expectedArr);
expect(deserializeObject(outputMap)).toEqual(arrMap);
let arrStr = ['first', 'second', 'third', 'fourth'];
const arrStr = ['first', 'second', 'third', 'fourth'];
const outputStr = makeObjectSerializable(arrStr);
expect(outputStr).toEqual(arrStr);
expect(deserializeObject(outputStr)).toEqual(arrStr);

View File

@@ -70,8 +70,8 @@ export function processPluginStates(
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
): PluginStatesState {
let pluginStates = {};
for (let key in allPluginStates) {
let keyArray = key.split('#');
for (const key in allPluginStates) {
const keyArray = key.split('#');
const pluginName = keyArray.pop();
const filteredClients = clients.filter(client => {
// Remove the last entry related to plugin
@@ -94,7 +94,7 @@ export function processNotificationStates(
allActiveNotifications: Array<PluginNotification>,
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
): Array<PluginNotification> {
let activeNotifications = allActiveNotifications.filter(notif => {
const activeNotifications = allActiveNotifications.filter(notif => {
const filteredClients = clients.filter(client =>
notif.client ? client.id.includes(notif.client) : false,
);
@@ -176,7 +176,7 @@ export const processStore = async (
if (device) {
const {serial} = device;
const processedClients = processClients(clients, serial);
let processedPluginStates = processPluginStates(
const processedPluginStates = processPluginStates(
processedClients,
serial,
pluginStates,
@@ -210,7 +210,7 @@ export async function fetchMetadata(
const errorArray: Array<Error> = [];
const clients = store.getState().connections.clients;
const selectedDevice = store.getState().connections.selectedDevice;
for (let client of clients) {
for (const client of clients) {
if (
!selectedDevice ||
selectedDevice.isArchived ||
@@ -218,7 +218,7 @@ export async function fetchMetadata(
) {
continue;
}
for (let plugin of client.plugins) {
for (const plugin of client.plugins) {
const pluginClass: ?Class<
FlipperDevicePlugin<> | FlipperPlugin<>,
> = plugin ? pluginsMap.get(plugin) : null;

View File

@@ -22,7 +22,7 @@ async function exportMetrics(
pluginsMap: Map<string, Class<FlipperDevicePlugin<> | FlipperPlugin<>>>,
): Promise<string> {
const metrics: ExportMetricType = {};
for (let key in pluginStates) {
for (const key in pluginStates) {
const pluginStateData = pluginStates[key];
const arr = key.split('#');
const pluginName = arr.pop();

View File

@@ -46,12 +46,12 @@ export function getPersistedState<PersistedState>(
}
export function getActivePluginNames(plugins: PluginsState): Array<string> {
let pluginsMap: Map<
const pluginsMap: Map<
string,
Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
> = pluginsClassMap(plugins);
let arr: Array<PluginDefinition> = plugins.disabledPlugins.concat(
const arr: Array<PluginDefinition> = plugins.disabledPlugins.concat(
plugins.gatekeepedPlugins,
);
arr.forEach((plugin: PluginDefinition) => {

View File

@@ -11,8 +11,8 @@ export default function promiseTimeout<T>(
timeoutMessage: ?string,
): Promise<T> | Promise<void> {
// Create a promise that rejects in <ms> milliseconds
let timeout = new Promise((resolve, reject) => {
let id = setTimeout(() => {
const timeout = new Promise((resolve, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(new Error(timeoutMessage || `Timed out in ${ms} ms.`));
}, ms);

View File

@@ -20,7 +20,7 @@ function processArray(
dict: Map<any, any>,
): {childNeedsIteration: boolean, outputArr: Array<any>} {
// Adds the array item to the stack if it needs to undergo iteration to serialise it. Otherwise it adds the serialized version of the item to the memoization dict
let outputArr = [];
const outputArr = [];
let childNeedsIteration = false;
for (const item of array) {
const isItemInstanceOfObject = item instanceof Object;
@@ -116,7 +116,7 @@ export function makeObjectSerializable(obj: any): any {
if (!(obj instanceof Object)) {
return obj;
}
let stack = [obj];
const stack = [obj];
const dict: Map<any, any> = new Map();
while (stack.length > 0) {
const element = stack[stack.length - 1];