Skip to content

Commit

Permalink
Update lint
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jul 22, 2024
1 parent 139f2db commit 18b4dc0
Show file tree
Hide file tree
Showing 10 changed files with 2,559 additions and 2,342 deletions.
16 changes: 0 additions & 16 deletions .eslintrc.json

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ on: push

jobs:
test:
name: Lint, build, and test on node 14.x and ubuntu-latest
name: Lint, build, and test on node 20.x and ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
- uses: actions/checkout@v4
- name: Use Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 14.x
node-version: 20.x
- name: Install deps (with cache)
uses: bahmutov/npm-install@v1
- name: Lint codebase
Expand Down
63 changes: 63 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import prettier from 'eslint-plugin-prettier'
import typescriptEslint from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import js from '@eslint/js'
import { FlatCompat } from '@eslint/eslintrc'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
})

export default [
...compat.extends(
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:prettier/recommended',
'plugin:unicorn/recommended',
),
{
plugins: {
prettier,
'@typescript-eslint': typescriptEslint,
},

languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: 'script',

parserOptions: {
project: './tsconfig.lint.json',
},
},

rules: {
'no-underscore-dangle': 0,
curly: 'error',
'unicorn/no-useless-undefined': 0,
'unicorn/filename-case': 0,
'unicorn/numeric-separators-style': 0,
'unicorn/number-literal-case': 0,
'unicorn/no-new-array': 0,
'unicorn/no-array-for-each': 0,
'unicorn/prefer-spread': 0,
'unicorn/prefer-string-replace-all': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-unsafe-call': 0,
'@typescript-eslint/no-unsafe-argument': 0,
'@typescript-eslint/no-unsafe-return': 0,
'@typescript-eslint/no-unsafe-member-access': 0,
'@typescript-eslint/no-unsafe-assignment': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0,
semi: ['error', 'never'],
},
},
]
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"scripts": {
"test": "jest",
"coverage": "npm test -- --coverage",
"lint": "eslint src test",
"lint": "eslint --report-unused-disable-directives --max-warnings 0 src test",
"docs": "documentation readme src/twoBitFile.ts --section=TwoBitFile",
"clean": "rimraf dist esm",
"prebuild": "npm run docs && npm run clean",
"build:esm": "tsc --target es2018 --outDir esm",
"build:es5": "tsc --target es5 --module commonjs --outDir dist",
"build:es5": "tsc --target es2015 --module commonjs --outDir dist",
"build": "npm run build:esm && npm run build:es5",
"version": "standard-changelog && git add CHANGELOG.md",
"prepublishOnly": "npm run lint && npm test && npm run build",
Expand All @@ -44,19 +44,20 @@
},
"devDependencies": {
"@types/jest": "^29.2.4",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"documentation": "^14.0.1",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-unicorn": "^54.0.0",
"jest": "^29.3.1",
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
"standard-changelog": "^2.0.27",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"standard-changelog": "^6.0.0",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
"typescript": "^5.5.3"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import TwoBitFile from './twoBitFile'
export { TwoBitFile }
export { default as TwoBitFile } from './twoBitFile'
86 changes: 47 additions & 39 deletions src/twoBitFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ const TWOBIT_MAGIC = 0x1a412743

function tinyMemoize(_class: any, methodName: string) {
const method = _class.prototype[methodName]
const memoAttrName = `_memo_${methodName}`
const memoAttributeName = `_memo_${methodName}`
_class.prototype[methodName] = function _tinyMemoized() {
if (!(memoAttrName in this)) {
this[memoAttrName] = method.call(this)
if (!(memoAttributeName in this)) {
this[memoAttributeName] = method.call(this)
}
return this[memoAttrName]
return this[memoAttributeName]
}
}

const twoBit = ['T', 'C', 'A', 'G']
// byteTo4Bases is an array of byteValue -> 'ACTG'
// the weird `...keys()` incantation generates an array of numbers 0 to 255
const byteTo4Bases = [] as string[]
for (let i = 0; i < 256; i++) {
for (let index = 0; index < 256; index++) {
byteTo4Bases.push(
twoBit[(i >> 6) & 3] +
twoBit[(i >> 4) & 3] +
twoBit[(i >> 2) & 3] +
twoBit[i & 3],
twoBit[(index >> 6) & 3] +
twoBit[(index >> 4) & 3] +
twoBit[(index >> 2) & 3] +
twoBit[index & 3],
)
}

Expand Down Expand Up @@ -60,16 +60,22 @@ export default class TwoBitFile {
}

async _getParser(name: ParserName) {
const parser = (await this._getParsers())[name]
const parsers = await this._getParsers()
const parser = parsers[name]
if (!parser) {
throw new Error(`parser ${name} not found`)
}
return parser
}

async _detectEndianness() {
const ret = await this.filehandle.read(Buffer.allocUnsafe(8), 0, 8, 0)
const { buffer } = ret
const returnValue = await this.filehandle.read(
Buffer.allocUnsafe(8),
0,
8,
0,
)
const { buffer } = returnValue
if (buffer.readInt32LE(0) === TWOBIT_MAGIC) {
this.isBigEndian = false
this.version = buffer.readInt32LE(4)
Expand All @@ -96,13 +102,12 @@ export default class TwoBitFile {
.endianess(endianess)
.uint8('nameLength')
.string('name', { length: 'nameLength' })
if (this.version === 1) {
indexEntryParser = indexEntryParser.buffer('offsetBytes', {
length: 8,
})
} else {
indexEntryParser = indexEntryParser.uint32('offset')
}
indexEntryParser =
this.version === 1
? indexEntryParser.buffer('offsetBytes', {
length: 8,
})
: indexEntryParser.uint32('offset')
/* istanbul ignore next */
const header = new Parser()
.endianess(endianess)
Expand Down Expand Up @@ -195,7 +200,7 @@ export default class TwoBitFile {
)
const indexParser = await this._getParser('index')
const indexData = indexParser.parse(buffer).result.index
const index = {} as { [key: string]: number }
const index = {} as Record<string, number>
if (this.version === 1) {
indexData.forEach(
({ name, offsetBytes }: { name: string; offsetBytes: number }) => {
Expand Down Expand Up @@ -237,12 +242,12 @@ export default class TwoBitFile {
const index = await this.getIndex()
const seqNames = Object.keys(index)
const sizePromises = Object.values(index).map(offset =>
this._getSequenceSize(offset as number),
this._getSequenceSize(offset),
)
const sizes = await Promise.all(sizePromises)
const returnObject = {} as { [key: string]: number }
for (let i = 0; i < seqNames.length; i += 1) {
returnObject[seqNames[i]] = sizes[i]
const returnObject = {} as Record<string, number>
for (const [index_, seqName] of seqNames.entries()) {
returnObject[seqName] = sizes[index_]
}
return returnObject
}
Expand Down Expand Up @@ -310,7 +315,7 @@ export default class TwoBitFile {
* @param {number} [regionEnd] optional 0-based half-open end of the sequence region to fetch. defaults to end of the sequence
* @returns {Promise} for a string of sequence bases
*/
async getSequence(seqName: string, regionStart = 0, regionEnd: number) {
async getSequence(seqName: string, regionStart = 0, regionEnd = Infinity) {
const index = await this.getIndex()
const offset = index[seqName]
if (!offset) {
Expand All @@ -324,7 +329,7 @@ export default class TwoBitFile {
}
// end defaults to the end of the sequence
if (regionEnd === undefined || regionEnd > record.dnaSize) {
regionEnd = record.dnaSize
regionEnd = record.dnaSize as number
}

const nBlocks = this._getOverlappingBlocks(
Expand Down Expand Up @@ -358,7 +363,7 @@ export default class TwoBitFile {
genomicPosition += 1
) {
// check whether we are currently masked
while (maskBlocks.length && maskBlocks[0].end <= genomicPosition) {
while (maskBlocks.length > 0 && maskBlocks[0].end <= genomicPosition) {
maskBlocks.shift()
}
const baseIsMasked =
Expand Down Expand Up @@ -401,19 +406,18 @@ export default class TwoBitFile {
blockSizes: number[],
) {
// find the start and end indexes of the blocks that match
let startIndex
let endIndex
for (let i = 0; i < blockStarts.length; i += 1) {
const blockStart = blockStarts[i]
const blockSize = blockSizes[i]
let startIndex: number | undefined
let endIndex: number | undefined
for (const [index, blockStart] of blockStarts.entries()) {
const blockSize = blockSizes[index]
if (regionStart >= blockStart + blockSize || regionEnd <= blockStart) {
// block does not overlap the region
if (startIndex !== undefined) {
endIndex = i
endIndex = index
break
}
} else if (startIndex === undefined) {
startIndex = i
startIndex = index
} // block does overlap the region, record this if it is the first
}

Expand All @@ -427,11 +431,15 @@ export default class TwoBitFile {
}

const blocks = new Array(endIndex - startIndex)
for (let blockNum = startIndex; blockNum < endIndex; blockNum += 1) {
blocks[blockNum - startIndex] = {
start: blockStarts[blockNum],
end: blockStarts[blockNum] + blockSizes[blockNum],
size: blockSizes[blockNum],
for (
let blockNumber = startIndex;
blockNumber < endIndex;
blockNumber += 1
) {
blocks[blockNumber - startIndex] = {
start: blockStarts[blockNumber],
end: blockStarts[blockNumber] + blockSizes[blockNumber],
size: blockSizes[blockNumber],
}
}
return blocks
Expand Down
Loading

0 comments on commit 18b4dc0

Please sign in to comment.