Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 20, 2020
1 parent 0ffcc38 commit 6a8b4a0
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.peg.js
node_modules/**
.eslintrc.js
12 changes: 10 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@
*/

module.exports = {
root: true,
"env": {
"node": true,
"es6": true
"es6": true,
"jest/globals": true
},
"plugins": ["jest"],
parserOptions: {
ecmaVersion: 2020,
},
globals: {
BigInt: false
BigInt: false,
WeakRef: false
},
"rules": {
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "warn",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error",
"arrow-spacing": "error",
"brace-style": ["error", "1tbs"],
"comma-dangle": ["error", "never"],
Expand Down
2 changes: 1 addition & 1 deletion day10.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function part1(inp) {
}

function part2(inp) {
const len = inp.length
const len = inp.length
const adjacent = mathjs.zeros(len, len)

// create adacency matrix, A, which has a 1 in A[i,j] for each
Expand Down
4 changes: 2 additions & 2 deletions day12.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function part1(inp) {
let x = 0
let y = 0

for (let [dir, dist] of inp) {
for (const [d, dist] of inp) {
let dir = d
if (dir === 'F') {
dir = DIRKEYS[cur]
}
Expand Down Expand Up @@ -94,7 +95,6 @@ function part2(inp) {
return Math.abs(x) + Math.abs(y)
}


function main(...args) {
const inp = Utils.readLines().map(s => {
const m = s.match(/([NSEWLRF])(\d+)/)
Expand Down
7 changes: 4 additions & 3 deletions day14.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const Utils = require('./utils')
function part1(inp, args) {
const reg = {}
let mask = null
for (let [inst, x, y] of inp) {
for (const [inst, x, y0] of inp) {
let y = y0
switch (inst) {
case 'mask':
mask = x
Expand All @@ -26,13 +27,13 @@ function part1(inp, args) {
reg[x] = y
}
}
return Object.values(reg).reduce((t, x) => t + x, 0n).toString()
return Object.values(reg).reduce((t, x) => t + x, 0n).toString()
}

function part2(inp, args) {
const reg = {}
let mask = null
for (let [inst, x, y] of inp) {
for (const [inst, x, y] of inp) {
switch (inst) {
case 'mask':
mask = x
Expand Down
2 changes: 1 addition & 1 deletion day15.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Utils = require('./utils')
function speakNumbers(inp, target) {
const spoken = Array(target) // pre-allocate, do NOT use an object
let last = null
for (const [i,x] of inp.entries()) {
for (const [i, x] of inp.entries()) {
if (last != null) {
spoken[last] = i + 1
}
Expand Down
2 changes: 1 addition & 1 deletion day16.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function part2(inp, args) {

// remove any rule from the ruleset that doesn't allow n
function check(n, rules) {
for (const ruleName of rules) { // make copy so we can modify it in the loop
for (const ruleName of rules) { // make copy so we can modify it in the loop
const [[a1, a2], [b1, b2]] = inp.rules[ruleName]
if ( (n < a1 || n > a2) && (n < b1 || n > b2)) {
rules.delete(ruleName)
Expand Down
19 changes: 12 additions & 7 deletions day17.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Pos {
if (coords[0] instanceof Pos) {
coords = coords[0].coords
}
return coords.reduce((t, c, i) => t && ((c == null) || (this.coords[i] === c)), true)
return coords.reduce(
(t, c, i) => t && ((c == null) || (this.coords[i] === c)),
true)
}
toString() {
return this.coords.join(',')
Expand Down Expand Up @@ -85,7 +87,7 @@ class Cell {
if (pos.isOrigin()) {
return chalk.bgBlue(ch)
}
if (pos.is(0,0)) {
if (pos.is(0, 0)) {
return chalk.bgRed(ch)
}
return ch
Expand Down Expand Up @@ -117,7 +119,7 @@ class Sheet {
return c
}
getCellVal(pos) {
let c = this.cells[pos]
const c = this.cells[pos]
return c ? c.active : false
}
setCell(pos, active) {
Expand Down Expand Up @@ -145,8 +147,11 @@ class Sheet {
return Utils.reduce((t, c) => c.active ? t + 1 : t, this.allCells(), 0)
}
scanPos() {
const ranges = this.min.map((min, i) => range(min-1, this.max[i] + 2)).reverse()
return Utils.map((coords) => new Pos(...coords.reverse()), Utils.product(ranges))
const ranges =
this.min.map((min, i) => range(min-1, this.max[i] + 2)).reverse()
return Utils.map(
(coords) => new Pos(...coords.reverse()),
Utils.product(ranges))
}
toString() {
let str = ''
Expand Down Expand Up @@ -191,7 +196,7 @@ function life(sheet, times=6) {
}

function part1(inp, args) {
let sheet = new Sheet(3)
const sheet = new Sheet(3)

for (const i of range(inp.length)) {
for (const j of range(inp[i].length)) {
Expand All @@ -202,7 +207,7 @@ function part1(inp, args) {
}

function part2(inp, args) {
let sheet = new Sheet(4)
const sheet = new Sheet(4)

for (const i of range(inp.length)) {
for (const j of range(inp[i].length)) {
Expand Down
2 changes: 1 addition & 1 deletion day19.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function part2(inp, args) {
const rules = inp[0]
.sort((a, b) => a[0] - b[0])
.filter(r => (r[0] != 8) && (r[0] != 11)) // remove rule 8 and 11
rules.push([11, ["or", ["and", 42, 31], ["and", 42, 11, 31]]])
rules.push([11, ['or', ['and', 42, 31], ['and', 42, 11, 31]]])
const grammar = generateGrammar(rules, true) + 'r8 = r42+\n'
const {parse} = pegjs.generate(grammar, {trace: false})
let count = 0
Expand Down
4 changes: 3 additions & 1 deletion day3.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function part1(trees, args) {
}

function part2(trees, args) {
return slopes.reduce((t, [right, down]) => t * countSlope(trees, right, down), 1)
return slopes.reduce(
(t, [right, down]) => t * countSlope(trees, right, down),
1)
}

function main(...args) {
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports = {
// An array of directory names to be searched recursively up from the
// requiring module's location
moduleDirectories: [
"node_modules"
'node_modules'
],

// An array of file extensions your modules use
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "jest",
"coverage": "jest --coverage",
"lint": "eslint *.js",
"lint": "eslint .",
"day": "sao ./template",
"build": "for f in *.pegjs; do pegjs -o ${f%.*}.peg.js $f; done"
},
Expand Down
10 changes: 7 additions & 3 deletions template/saofile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict'

const superb = require('superb')
const fs = require('fs')
const {spawn} = require('child_process')

const days = fs.readdirSync('.')
.filter(f => f.match(/day\d+\.js/))
.sort((a, b) => a.match(/\d+/)[0] - b.match(/\d+/)[0])
const d = (days.length == 0) ? 1 : parseInt(days[days.length-1].match(/\d+/)[0]) + 1
const d = (days.length == 0) ?
1 :
parseInt(days[days.length-1].match(/\d+/)[0]) + 1

function spawnAsync(cmd, ...args) {
return new Promise((resolve, reject) => {
Expand All @@ -23,7 +27,7 @@ function spawnAsync(cmd, ...args) {
}

module.exports = {
actions: [
actions: [
{
type: 'add',
files: '**'
Expand All @@ -36,7 +40,7 @@ actions: [
'lines.pegjs': `day${d}.pegjs`,
'input.txt': `day${d}.txt`
}
},
}
],
async completed() {
await spawnAsync('make')
Expand Down
21 changes: 14 additions & 7 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ test('parseFile', () => {
const t = Utils.parseFile()
expect(t).toEqual(['1', '2'])
const {parse} = require('./utils.test.peg')
const u = Utils.parseFile(path.join(__dirname, 'inputs', 'utils.test.txt'), parse)
const u = Utils.parseFile(
path.join(__dirname, 'inputs', 'utils.test.txt'),
parse)
expect(u).toEqual(['1', '2'])

const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const v = Utils.parseFile(null, INVALID_FILE)
expect(v).toEqual(['1', '2'])
expect(spy).toHaveBeenCalled()
expect(spy.mock.calls).toEqual([[`No parser: "${INVALID_FILE}", falling back on readLines`]])
expect(spy.mock.calls)
.toEqual([[`No parser: "${INVALID_FILE}", falling back on readLines`]])
spy.mockRestore()
})

Expand Down Expand Up @@ -52,13 +55,13 @@ test('itSome', () => {
expect(Utils.itSome(Utils.range(3), i => i % 2)).toBe(true)
expect(Utils.itSome([1, 3, 5], i => i % 2 === 0)).toBe(false)
const t = {}
expect(Utils.itSome([1, 3, 5], function (_, i) {
expect(Utils.itSome([1, 3, 5], function(_, i) {
return i < 3 && this == t
}, t)).toBe(true)
})

test('range', () => {
let seen = []
const seen = []
for (const x of Utils.range(4)) {
seen.push(x)
}
Expand All @@ -73,7 +76,8 @@ test('pick', () => {

test('combinations', () => {
expect([...Utils.combinations(Utils.range(3), 5)]).toEqual([])
expect([...Utils.combinations([0, 1, 2], 2)]).toEqual([[0, 1], [0, 2], [1, 2]])
expect([...Utils.combinations([0, 1, 2], 2)])
.toEqual([[0, 1], [0, 2], [1, 2]])
})

test('trunc', () => {
Expand All @@ -92,7 +96,9 @@ test('take', () => {

test('permutations', () => {
expect([...Utils.permutations('ABCD', 2)].map(a => a.join('')))
.toEqual(['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC'])
.toEqual([
'AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC'
])
expect([...Utils.permutations([], 1)]).toEqual([])
expect([...Utils.permutations([1, 2, 3], 0)]).toEqual([])
expect([...Utils.permutations([1, 2, 3], 5)]).toEqual([])
Expand Down Expand Up @@ -127,5 +133,6 @@ test('ncycle', () => {
test('reduce', () => {
expect(Utils.reduce((t, x) => t + x, Utils.range(10))).toBe(45)
expect(Utils.reduce((t, x) => t + x, Utils.range(10), 1)).toBe(46)
expect(() => Utils.reduce(() => {}, [])).toThrow('Empty iterable and no initializer')
expect(() => Utils.reduce(() => {}, []))
.toThrow('Empty iterable and no initializer')
})

0 comments on commit 6a8b4a0

Please sign in to comment.