From 6f796f1e7dfd631314f9a1df534eabd96dd1528c Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 2 Feb 2023 07:09:09 +0100 Subject: [PATCH] fix: sort imports (#84) https://github.com/ipfs/eslint-config-ipfs/pull/126 will introduce linting rules on import sorting so order imports during building in a way that will comply with the new linting rules. --- packages/protons-benchmark/src/decode.ts | 3 +- packages/protons-benchmark/src/encode.ts | 3 +- packages/protons-benchmark/src/index.ts | 8 +++-- .../protons-benchmark/src/protons/bench.ts | 2 +- packages/protons-benchmark/src/protons/rpc.ts | 2 +- packages/protons-benchmark/src/rpc.ts | 6 ++-- packages/protons-runtime/src/decode.ts | 1 + packages/protons-runtime/src/utils.ts | 5 ++-- packages/protons/bin/protons.ts | 1 + packages/protons/src/index.ts | 29 ++++++++++++------- packages/protons/test/fixtures/basic.ts | 2 +- packages/protons/test/fixtures/circuit.ts | 2 +- packages/protons/test/fixtures/daemon.ts | 2 +- packages/protons/test/fixtures/dht.ts | 2 +- packages/protons/test/fixtures/maps.ts | 2 +- packages/protons/test/fixtures/noise.ts | 2 +- packages/protons/test/fixtures/optional.ts | 2 +- packages/protons/test/fixtures/peer.ts | 2 +- packages/protons/test/fixtures/singular.ts | 2 +- packages/protons/test/fixtures/test.ts | 2 +- packages/protons/test/index.spec.ts | 13 +++++---- packages/protons/test/maps.spec.ts | 6 ++-- packages/protons/test/unsupported.spec.ts | 1 + 23 files changed, 62 insertions(+), 38 deletions(-) diff --git a/packages/protons-benchmark/src/decode.ts b/packages/protons-benchmark/src/decode.ts index 271068f..cdd798d 100644 --- a/packages/protons-benchmark/src/decode.ts +++ b/packages/protons-benchmark/src/decode.ts @@ -6,9 +6,10 @@ $ npx playwright-test dist/src/index.js --runner benchmark */ import Benchmark from 'benchmark' -import { Test as ProtonsTest } from './protons/bench.js' + import { decodeTest as pbjsDecodeTest } from './pbjs/bench.js' import { Test as ProtobufjsTest } from './protobufjs/bench.js' +import { Test as ProtonsTest } from './protons/bench.js' const message = { meh: { diff --git a/packages/protons-benchmark/src/encode.ts b/packages/protons-benchmark/src/encode.ts index 68fb964..119a01a 100644 --- a/packages/protons-benchmark/src/encode.ts +++ b/packages/protons-benchmark/src/encode.ts @@ -6,9 +6,10 @@ $ npx playwright-test dist/src/index.js --runner benchmark */ import Benchmark from 'benchmark' -import { Test as ProtonsTest } from './protons/bench.js' + import { encodeTest as pbjsEncodeTest } from './pbjs/bench.js' import { Test as ProtobufjsTest } from './protobufjs/bench.js' +import { Test as ProtonsTest } from './protons/bench.js' const message = { meh: { diff --git a/packages/protons-benchmark/src/index.ts b/packages/protons-benchmark/src/index.ts index ef2fb16..760d892 100644 --- a/packages/protons-benchmark/src/index.ts +++ b/packages/protons-benchmark/src/index.ts @@ -5,12 +5,14 @@ $ node dist/src/index.js $ npx playwright-test dist/src/index.js --runner benchmark */ -import Benchmark from 'benchmark' import { expect } from 'aegir/chai' -import { Test as ProtonsTest } from './protons/bench.js' + +import Benchmark from 'benchmark' + import { encodeTest as pbjsEncodeTest, decodeTest as pbjsDecodeTest } from './pbjs/bench.js' -import { Test as ProtobufjsTest } from './protobufjs/bench.js' import { Test as ProtobufTsTest } from './protobuf-ts/bench.js' +import { Test as ProtobufjsTest } from './protobufjs/bench.js' +import { Test as ProtonsTest } from './protons/bench.js' const message = { meh: { diff --git a/packages/protons-benchmark/src/protons/bench.ts b/packages/protons-benchmark/src/protons/bench.ts index 8347fd5..5353cdb 100644 --- a/packages/protons-benchmark/src/protons/bench.ts +++ b/packages/protons-benchmark/src/protons/bench.ts @@ -4,8 +4,8 @@ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Foo { baz?: number diff --git a/packages/protons-benchmark/src/protons/rpc.ts b/packages/protons-benchmark/src/protons/rpc.ts index a757885..eb0450f 100644 --- a/packages/protons-benchmark/src/protons/rpc.ts +++ b/packages/protons-benchmark/src/protons/rpc.ts @@ -4,8 +4,8 @@ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ import { encodeMessage, decodeMessage, message, writer } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface RPC { subscriptions: RPC.SubOpts[] diff --git a/packages/protons-benchmark/src/rpc.ts b/packages/protons-benchmark/src/rpc.ts index 1cd33cf..fa9dc5e 100644 --- a/packages/protons-benchmark/src/rpc.ts +++ b/packages/protons-benchmark/src/rpc.ts @@ -5,10 +5,12 @@ $ node dist/src/index.js $ npx playwright-test dist/src/index.js --runner benchmark */ +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' + import Benchmark from 'benchmark' -import { RPC as ProtonsRPC } from './protons/rpc.js' + import { RPC as ProtobufjsRPC } from './protobufjs/rpc.js' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { RPC as ProtonsRPC } from './protons/rpc.js' const rpc = { subscriptions: [], diff --git a/packages/protons-runtime/src/decode.ts b/packages/protons-runtime/src/decode.ts index 3d9356b..bd395ac 100644 --- a/packages/protons-runtime/src/decode.ts +++ b/packages/protons-runtime/src/decode.ts @@ -1,4 +1,5 @@ import type { Uint8ArrayList } from 'uint8arraylist' + import type { Codec } from './codec.js' import { reader } from './utils.js' diff --git a/packages/protons-runtime/src/utils.ts b/packages/protons-runtime/src/utils.ts index 9c75ba2..d125d64 100644 --- a/packages/protons-runtime/src/utils.ts +++ b/packages/protons-runtime/src/utils.ts @@ -3,11 +3,12 @@ import ReaderClass from 'protobufjs/src/reader.js' // @ts-expect-error no types import ReaderBufferClass from 'protobufjs/src/reader_buffer.js' // @ts-expect-error no types +import util from 'protobufjs/src/util/minimal.js' +// @ts-expect-error no types import WriterClass from 'protobufjs/src/writer.js' // @ts-expect-error no types import WriterBufferClass from 'protobufjs/src/writer_buffer.js' -// @ts-expect-error no types -import util from 'protobufjs/src/util/minimal.js' + import type { Reader, Writer } from './index.js' function configure (): void { diff --git a/packages/protons/bin/protons.ts b/packages/protons/bin/protons.ts index 23ceee2..55e6615 100644 --- a/packages/protons/bin/protons.ts +++ b/packages/protons/bin/protons.ts @@ -1,6 +1,7 @@ #! /usr/bin/env node import meow from 'meow' + import { generate } from '../src/index.js' async function main (): Promise { diff --git a/packages/protons/src/index.ts b/packages/protons/src/index.ts index 6f5744d..04ec68f 100644 --- a/packages/protons/src/index.ts +++ b/packages/protons/src/index.ts @@ -1,9 +1,10 @@ /* eslint-disable max-depth */ -import { main as pbjs } from 'protobufjs-cli/pbjs.js' +import fs from 'fs/promises' import path from 'path' import { promisify } from 'util' -import fs from 'fs/promises' + +import { main as pbjs } from 'protobufjs-cli/pbjs.js' export enum CODEC_TYPES { VARINT = 0, @@ -697,29 +698,37 @@ export async function generate (source: string, flags: Flags): Promise { const moduleDef = defineModule(def) - let lines = [ + const ignores = [ '/* eslint-disable import/export */', '/* eslint-disable complexity */', '/* eslint-disable @typescript-eslint/no-namespace */', '/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */', - '/* eslint-disable @typescript-eslint/no-empty-interface */', - '' + '/* eslint-disable @typescript-eslint/no-empty-interface */' ] + const imports = [] + if (moduleDef.imports.size > 0) { - lines.push(`import { ${Array.from(moduleDef.imports).join(', ')} } from 'protons-runtime'`) + imports.push(`import { ${Array.from(moduleDef.imports).join(', ')} } from 'protons-runtime'`) } if (moduleDef.imports.has('encodeMessage')) { - lines.push("import type { Uint8ArrayList } from 'uint8arraylist'") + imports.push("import type { Uint8ArrayList } from 'uint8arraylist'") } if (moduleDef.importedTypes.size > 0) { - lines.push(`import type { ${Array.from(moduleDef.importedTypes).join(', ')} } from 'protons-runtime'`) + imports.push(`import type { ${Array.from(moduleDef.importedTypes).join(', ')} } from 'protons-runtime'`) } - lines = [ - ...lines, + const lines = [ + ...ignores, + '', + ...imports.sort((a, b) => { + const aModule = a.split("from '")[1].toString() + const bModule = b.split("from '")[1].toString() + + return aModule.localeCompare(bModule) + }), '', ...moduleDef.compiled ] diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index 759c1e5..d25f972 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Basic { foo?: string diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index 658f7ef..34b445a 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface CircuitRelay { type?: CircuitRelay.Type diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 8db6fee..5a2c8c7 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Request { type: Request.Type diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index 9fcab58..36c3b07 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Record { key?: Uint8Array diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index 5295105..6a12986 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface SubMessage { foo: string diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 204fef0..1863a3d 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface pb {} diff --git a/packages/protons/test/fixtures/optional.ts b/packages/protons/test/fixtures/optional.ts index a882080..58cb48f 100644 --- a/packages/protons/test/fixtures/optional.ts +++ b/packages/protons/test/fixtures/optional.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export enum OptionalEnum { NO_VALUE = 'NO_VALUE', diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index 0911b18..ecd6ad6 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export interface Peer { addresses: Address[] diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index 2342c3a..1c2b2d3 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export enum SingularEnum { NO_VALUE = 'NO_VALUE', diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index 05849d8..c97f1d4 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -5,8 +5,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime' -import type { Uint8ArrayList } from 'uint8arraylist' import type { Codec } from 'protons-runtime' +import type { Uint8ArrayList } from 'uint8arraylist' export enum AnEnum { HERP = 'HERP', diff --git a/packages/protons/test/index.spec.ts b/packages/protons/test/index.spec.ts index ebda6da..2d31617 100644 --- a/packages/protons/test/index.spec.ts +++ b/packages/protons/test/index.spec.ts @@ -1,16 +1,19 @@ /* eslint-env mocha */ +import fs from 'fs' + import { expect } from 'aegir/chai' + +import Long from 'long' import pbjs from 'pbjs' -import { Basic, Empty } from './fixtures/basic.js' -import { AllTheTypes, AnEnum } from './fixtures/test.js' -import fs from 'fs' import protobufjs, { Type as PBType } from 'protobufjs' -import { Peer } from './fixtures/peer.js' + +import { Basic, Empty } from './fixtures/basic.js' import { CircuitRelay } from './fixtures/circuit.js' -import Long from 'long' import { Optional, OptionalEnum } from './fixtures/optional.js' +import { Peer } from './fixtures/peer.js' import { Singular, SingularEnum } from './fixtures/singular.js' +import { AllTheTypes, AnEnum } from './fixtures/test.js' function longifyBigInts (obj: any): any { const output = { diff --git a/packages/protons/test/maps.spec.ts b/packages/protons/test/maps.spec.ts index fdf004b..c73eb77 100644 --- a/packages/protons/test/maps.spec.ts +++ b/packages/protons/test/maps.spec.ts @@ -1,9 +1,11 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { MapTypes, SubMessage } from './fixtures/maps.js' -import protobufjs from 'protobufjs' + import Long from 'long' +import protobufjs from 'protobufjs' + +import { MapTypes, SubMessage } from './fixtures/maps.js' function longifyBigInts (obj: any): any { const output = { diff --git a/packages/protons/test/unsupported.spec.ts b/packages/protons/test/unsupported.spec.ts index 7f29154..216198e 100644 --- a/packages/protons/test/unsupported.spec.ts +++ b/packages/protons/test/unsupported.spec.ts @@ -1,6 +1,7 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' + import { generate } from '../src/index.js' describe('unsupported', () => {