From c12db2a53a11f701fbfbd04f5f977580c37af54b Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 6 Sep 2022 13:18:42 +1000 Subject: [PATCH] fix: address JS & TS linting complaints --- src/block.js | 111 +++++++++++++++++++++++---------------- test/test-cid.spec.js | 17 ------ test/ts-use/package.json | 11 +++- test/ts-use/src/main.ts | 1 + 4 files changed, 76 insertions(+), 64 deletions(-) diff --git a/src/block.js b/src/block.js index 2235b620..7b438ec3 100644 --- a/src/block.js +++ b/src/block.js @@ -3,11 +3,37 @@ import { bytes as binary, CID } from './index.js' // eslint-disable-next-line import * as API from './interface.js' -const readonly = ({ enumerable = true, configurable = false } = {}) => ({ - enumerable, - configurable, - writable: false -}) +function readonly ({ enumerable = true, configurable = false } = {}) { + return { enumerable, configurable, writable: false } +} + +/** + * @param {[string|number, string]} path + * @param {any} value + * @returns {Iterable<[string, CID]>} + */ +function * linksWithin (path, value) { + if (value != null && typeof value === 'object') { + if (Array.isArray(value)) { + for (const [index, element] of value.entries()) { + const elementPath = [...path, index] + const cid = CID.asCID(element) + if (cid) { + yield [elementPath.join('/'), cid] + } else if (typeof element === 'object') { + yield * links(element, elementPath) + } + } + } else { + const cid = CID.asCID(value) + if (cid) { + yield [path.join('/'), cid] + } else { + yield * links(value, path) + } + } + } +} /** * @template T @@ -15,31 +41,32 @@ const readonly = ({ enumerable = true, configurable = false } = {}) => ({ * @param {Array} base * @returns {Iterable<[string, CID]>} */ -const links = function * (source, base) { - if (source == null) return - if (source instanceof Uint8Array) return +function * links (source, base) { + if (source == null || source instanceof Uint8Array) { + return + } for (const [key, value] of Object.entries(source)) { - const path = [...base, key] - if (value != null && typeof value === 'object') { - if (Array.isArray(value)) { - for (const [index, element] of value.entries()) { - const elementPath = [...path, index] - const cid = CID.asCID(element) - if (cid) { - yield [elementPath.join('/'), cid] - } else if (typeof element === 'object') { - yield * links(element, elementPath) - } - } - } else { - const cid = CID.asCID(value) - if (cid) { - yield [path.join('/'), cid] - } else { - yield * links(value, path) - } + const path = /** @type {[string|number, string]} */ ([...base, key]) + yield * linksWithin(path, value) + } +} + +/** + * @param {[string|number, string]} path + * @param {any} value + * @returns {Iterable} + */ +function * treeWithin (path, value) { + if (Array.isArray(value)) { + for (const [index, element] of value.entries()) { + const elementPath = [...path, index] + yield elementPath.join('/') + if (typeof element === 'object' && !CID.asCID(element)) { + yield * tree(element, elementPath) } } + } else { + yield * tree(value, path) } } @@ -49,23 +76,15 @@ const links = function * (source, base) { * @param {Array} base * @returns {Iterable} */ -const tree = function * (source, base) { - if (source == null) return +function * tree (source, base) { + if (source == null || typeof source !== 'object') { + return + } for (const [key, value] of Object.entries(source)) { - const path = [...base, key] + const path = /** @type {[string|number, string]} */ ([...base, key]) yield path.join('/') if (value != null && !(value instanceof Uint8Array) && typeof value === 'object' && !CID.asCID(value)) { - if (Array.isArray(value)) { - for (const [index, element] of value.entries()) { - const elementPath = [...path, index] - yield elementPath.join('/') - if (typeof element === 'object' && !CID.asCID(element)) { - yield * tree(element, elementPath) - } - } - } else { - yield * tree(value, path) - } + yield * treeWithin(path, value) } } } @@ -77,7 +96,7 @@ const tree = function * (source, base) { * @param {string[]} path * @return {API.BlockCursorView} */ -const get = (source, path) => { +function get (source, path) { let node = /** @type {Record} */(source) for (const [index, key] of path.entries()) { node = node[key] @@ -151,7 +170,7 @@ class Block { * @param {API.MultihashHasher} options.hasher * @returns {Promise>} */ -const encode = async ({ value, codec, hasher }) => { +async function encode ({ value, codec, hasher }) { if (typeof value === 'undefined') throw new Error('Missing required argument "value"') if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher') @@ -177,7 +196,7 @@ const encode = async ({ value, codec, hasher }) => { * @param {API.MultihashHasher} options.hasher * @returns {Promise>} */ -const decode = async ({ bytes, codec, hasher }) => { +async function decode ({ bytes, codec, hasher }) { if (!bytes) throw new Error('Missing required argument "bytes"') if (!codec || !hasher) throw new Error('Missing required argument: codec or hasher') @@ -202,7 +221,7 @@ const decode = async ({ bytes, codec, hasher }) => { * @param {{ cid: API.Link, value:T, codec?: API.BlockDecoder, bytes: API.ByteView }|{cid:API.Link, bytes:API.ByteView, value?:void, codec:API.BlockDecoder}} options * @returns {API.BlockView} */ -const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => { +function createUnsafe ({ bytes, cid, value: maybeValue, codec }) { const value = maybeValue !== undefined ? maybeValue : (codec && codec.decode(bytes)) @@ -229,7 +248,7 @@ const createUnsafe = ({ bytes, cid, value: maybeValue, codec }) => { * @param {API.MultihashHasher} options.hasher * @returns {Promise>} */ -const create = async ({ bytes, cid, hasher, codec }) => { +async function create ({ bytes, cid, hasher, codec }) { if (!bytes) throw new Error('Missing required argument "bytes"') if (!hasher) throw new Error('Missing required argument "hasher"') const value = codec.decode(bytes) diff --git a/test/test-cid.spec.js b/test/test-cid.spec.js index 9bc19594..d35d8209 100644 --- a/test/test-cid.spec.js +++ b/test/test-cid.spec.js @@ -217,23 +217,6 @@ describe('CID', () => { ) }) - /* TODO: after i have a keccak hash for the new interface - it('handles multibyte varint encoded codec codes', () => { - const ethBlockHash = textEncoder.encode('8a8e84c797605fbe75d5b5af107d4220a2db0ad35fd66d9be3d38d87c472b26d', 'hex') - const hash = keccak256.digest(ethBlockHash) - const cid1 = CID.create(1, 0x90, hash) - const cid2 = CID.parse(cid1.toString()) - - assert.deepStrictEqual(cid1.code, 0x90) - assert.deepStrictEqual(cid1.version, 1) - assert.deepStrictEqual(cid1.multihash, hash) - - assert.deepStrictEqual(cid2.code, 0x90) - assert.deepStrictEqual(cid2.version, 1) - assert.deepStrictEqual(cid2.multihash, hash) - }) - */ - it('.bytes', async () => { const hash = await sha256.digest(textEncoder.encode('abc')) const code = 0x71 diff --git a/test/ts-use/package.json b/test/ts-use/package.json index c4ef82b8..805023fb 100644 --- a/test/ts-use/package.json +++ b/test/ts-use/package.json @@ -6,5 +6,14 @@ }, "scripts": { "test": "npm install && npm_config_yes=true npx -p typescript tsc --noEmit" + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module", + "project": [ + "./test/ts-use/tsconfig.json" + ] + } } -} +} \ No newline at end of file diff --git a/test/ts-use/src/main.ts b/test/ts-use/src/main.ts index d57c5688..ff7de729 100644 --- a/test/ts-use/src/main.ts +++ b/test/ts-use/src/main.ts @@ -9,6 +9,7 @@ const main = async () => { codec: json }) + /* eslint-disable no-console */ console.log(block) }