Skip to content

A local-first personal finance app

[ESLint] Enable prefer-const project-wide (#2113)

Joel Jeremy Marquez6fbf0fdc1069
diff --git a/.eslintrc.js b/.eslintrc.js
index 8796c10859..0c2caca16c 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -162,7 +162,7 @@ module.exports = {
// Rules disable during TS migration
'@typescript-eslint/no-var-requires': 'off',
- 'prefer-const': 'off',
+ 'prefer-const': 'warn',
'prefer-spread': 'off',
'@typescript-eslint/no-empty-function': 'off',
},
@@ -238,54 +238,6 @@ module.exports = {
'no-restricted-imports': ['off', { patterns: restrictedImportColors }],
},
},
- // TODO: Remove this override once we addressed all warnings and enable the rule globally.
- {
- files: [
- './packages/api/*',
- './packages/api/app/**/*',
- './packages/crdt/**/*',
- './packages/desktop-client/src/*',
- './packages/desktop-client/src/components/*',
- './packages/desktop-client/src/components/accounts/**/*',
- './packages/desktop-client/src/components/autocomplete/**/*',
- './packages/desktop-client/src/components/budget/**/*',
- './packages/desktop-client/src/components/common/**/*',
- './packages/desktop-client/src/components/filters/**/*',
- './packages/desktop-client/src/components/gocardless/**/*',
- './packages/desktop-client/src/components/manager/**/*',
- './packages/desktop-client/src/components/mobile/**/*',
- './packages/desktop-client/src/components/modals/**/*',
- './packages/desktop-client/src/components/payees/**/*',
- './packages/desktop-client/src/components/reports/**/*',
- './packages/desktop-client/src/components/responsive/**/*',
- './packages/desktop-client/src/components/rules/**/*',
- './packages/desktop-client/src/components/schedules/**/*',
- './packages/desktop-client/src/components/select/**/*',
- './packages/desktop-client/src/components/settings/**/*',
- './packages/desktop-client/src/components/sidebar/**/*',
- './packages/desktop-client/src/components/spreadsheet/**/*',
- './packages/desktop-client/src/components/transactions/**/*',
- './packages/desktop-client/src/components/util/**/*',
- './packages/desktop-client/src/hooks/**/*',
- './packages/desktop-client/src/icons/**/*',
- './packages/desktop-client/src/style/**/*',
- './packages/desktop-client/src/types/**/*',
- './packages/desktop-client/src/util/**/*',
- './packages/desktop-electron/**/*',
- './packages/eslint-plugin-actual/**/*',
- './packages/loot-core/*',
- './packages/loot-core/src/client/**/*',
- './packages/loot-core/src/mocks/**/*',
- './packages/loot-core/src/platform/**/*',
- './packages/loot-core/src/server/**/*',
- './packages/loot-core/src/shared/**/*',
- './packages/loot-core/src/types/**/*',
- './packages/loot-core/webpack/**/*',
- ],
- rules: {
- 'prefer-const': 'warn',
- },
- },
],
settings: {
'import/resolver': {
diff --git a/packages/desktop-client/e2e/budget.test.js b/packages/desktop-client/e2e/budget.test.js
index 95404837f5..630d6d87a7 100644
--- a/packages/desktop-client/e2e/budget.test.js
+++ b/packages/desktop-client/e2e/budget.test.js
@@ -59,8 +59,8 @@ test.describe('Budget', () => {
});
test('clicking on spent amounts opens a transaction page', async () => {
- let categoryName = await budgetPage.getCategoryNameForRow(1);
- let accountPage = await budgetPage.clickOnSpentAmountForRow(1);
+ const categoryName = await budgetPage.getCategoryNameForRow(1);
+ const accountPage = await budgetPage.clickOnSpentAmountForRow(1);
expect(page.url()).toContain('/accounts');
expect(await accountPage.accountName.textContent()).toMatch(
new RegExp(String.raw`${categoryName} \(\w+ \d+\)`),
diff --git a/packages/eslint-plugin-actual/lib/rules/prefer-if-statement.js b/packages/eslint-plugin-actual/lib/rules/prefer-if-statement.js
index 386db0590e..f3584602d2 100644
--- a/packages/eslint-plugin-actual/lib/rules/prefer-if-statement.js
+++ b/packages/eslint-plugin-actual/lib/rules/prefer-if-statement.js
@@ -4,7 +4,8 @@
// Rule Definition
//------------------------------------------------------------------------------
-let suggestion = 'Consider using an if statement or optional chaining instead.';
+const suggestion =
+ 'Consider using an if statement or optional chaining instead.';
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
@@ -23,7 +24,7 @@ module.exports = {
},
create(context) {
- let sourceCode = context.getSourceCode();
+ const sourceCode = context.getSourceCode();
//----------------------------------------------------------------------
// Helpers
diff --git a/packages/eslint-plugin-actual/lib/rules/typography.js b/packages/eslint-plugin-actual/lib/rules/typography.js
index cd32cfc183..6ee049327d 100644
--- a/packages/eslint-plugin-actual/lib/rules/typography.js
+++ b/packages/eslint-plugin-actual/lib/rules/typography.js
@@ -34,7 +34,7 @@ module.exports = {
let rawText = context.getSourceCode().getText(node);
if (strip) rawText = rawText.slice(1, -1);
for (const match of rawText.matchAll(/['"]/g)) {
- let index = node.range[0] + match.index + (strip ? 1 : 0);
+ const index = node.range[0] + match.index + (strip ? 1 : 0);
context.report({
node,
loc: {
diff --git a/packages/loot-core/migrations/1632571489012_remove_cache.js b/packages/loot-core/migrations/1632571489012_remove_cache.js
index 2f3764c47d..bbd3e21384 100644
--- a/packages/loot-core/migrations/1632571489012_remove_cache.js
+++ b/packages/loot-core/migrations/1632571489012_remove_cache.js
@@ -31,14 +31,14 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
`);
// Migrate budget amounts and carryover
- let budget = db.runQuery(
+ const budget = db.runQuery(
`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!budget-%'`,
[],
true,
);
db.transaction(() => {
budget.forEach(monthBudget => {
- let match = monthBudget.name.match(
+ const match = monthBudget.name.match(
/^(budget-report|budget)(\d+)!budget-(.+)$/,
);
if (match == null) {
@@ -46,24 +46,25 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
return;
}
- let type = match[1];
- let month = match[2].slice(0, 4) + '-' + match[2].slice(4);
- let dbmonth = parseInt(match[2]);
- let cat = match[3];
+ const type = match[1];
+ const month = match[2].slice(0, 4) + '-' + match[2].slice(4);
+ const dbmonth = parseInt(match[2]);
+ const cat = match[3];
let amount = parseInt(getValue(monthBudget));
if (isNaN(amount)) {
amount = 0;
}
- let sheetName = monthBudget.name.split('!')[0];
- let carryover = db.runQuery(
+ const sheetName = monthBudget.name.split('!')[0];
+ const carryover = db.runQuery(
'SELECT * FROM spreadsheet_cells WHERE name = ?',
[`${sheetName}!carryover-${cat}`],
true,
);
- let table = type === 'budget-report' ? 'reflect_budgets' : 'zero_budgets';
+ const table =
+ type === 'budget-report' ? 'reflect_budgets' : 'zero_budgets';
db.runQuery(
`INSERT INTO ${table} (id, month, category, amount, carryover) VALUES (?, ?, ?, ?, ?)`,
[
@@ -78,16 +79,16 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
});
// Migrate buffers
- let buffers = db.runQuery(
+ const buffers = db.runQuery(
`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!buffered'`,
[],
true,
);
db.transaction(() => {
buffers.forEach(buffer => {
- let match = buffer.name.match(/^budget(\d+)!buffered$/);
+ const match = buffer.name.match(/^budget(\d+)!buffered$/);
if (match) {
- let month = match[1].slice(0, 4) + '-' + match[1].slice(4);
+ const month = match[1].slice(0, 4) + '-' + match[1].slice(4);
let amount = parseInt(getValue(buffer));
if (isNaN(amount)) {
amount = 0;
@@ -102,15 +103,15 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
});
// Migrate notes
- let notes = db.runQuery(
+ const notes = db.runQuery(
`SELECT * FROM spreadsheet_cells WHERE name LIKE 'notes!%'`,
[],
true,
);
- let parseNote = str => {
+ const parseNote = str => {
try {
- let value = JSON.parse(str);
+ const value = JSON.parse(str);
return value && value !== '' ? value : null;
} catch (e) {
return null;
@@ -119,9 +120,9 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
db.transaction(() => {
notes.forEach(note => {
- let parsed = parseNote(getValue(note));
+ const parsed = parseNote(getValue(note));
if (parsed) {
- let [, id] = note.name.split('!');
+ const [, id] = note.name.split('!');
db.runQuery(`INSERT INTO notes (id, note) VALUES (?, ?)`, [id, parsed]);
}
});
diff --git a/packages/node-libofx/index.js b/packages/node-libofx/index.js
index 50b2a203e7..8c8939325b 100644
--- a/packages/node-libofx/index.js
+++ b/packages/node-libofx/index.js
@@ -5,7 +5,7 @@ let _libofxPromise;
let _libofx;
let ffi;
-let parser = {
+const parser = {
ctx: null,
transactions: [],
@@ -61,7 +61,7 @@ export async function initModule() {
export function getOFXTransactions(data) {
ffi.parse_data(parser.ctx, data);
- let transactions = parser.transactions;
+ const transactions = parser.transactions;
parser.reset();
return transactions;
}
diff --git a/upcoming-release-notes/2113.md b/upcoming-release-notes/2113.md
new file mode 100644
index 0000000000..b78ebabc77
--- /dev/null
+++ b/upcoming-release-notes/2113.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Enable prefer-const ESLint rule project-wide