diff --git a/Build/lib/create-file.test.ts b/Build/lib/create-file.test.ts index 7d1538aa..9da5c52a 100644 --- a/Build/lib/create-file.test.ts +++ b/Build/lib/create-file.test.ts @@ -19,20 +19,26 @@ describe('fileEqual', () => { true )); - it('ignore comment', async () => { + it('ignore comment 1', async () => { await test( ['# A', 'B'], ['# B', 'B'], true ); - - await test( - ['# A', '# C', 'B'], - ['# A', '# D', 'B'], - true - ); }); + it('ignore comment 2', () => test( + ['# A', '# C', 'B'], + ['# A', '# D', 'B'], + true + )); + + it('ignore comment 3', () => test( + ['# A', '# C', 'B'], + ['# A', '# D', 'A'], + false + )); + it('comment more', () => test( ['# A', 'B'], ['# A', '# B', 'B'], diff --git a/Build/lib/create-file.ts b/Build/lib/create-file.ts index ef89a8b8..1db0a408 100644 --- a/Build/lib/create-file.ts +++ b/Build/lib/create-file.ts @@ -5,20 +5,36 @@ import picocolors from 'picocolors'; import type { Span } from '../trace'; import { readFileByLine } from './fetch-text-by-line'; import { writeFile } from './misc'; +import { invariant } from 'foxts/guard'; export async function fileEqual(linesA: string[], source: AsyncIterable | Iterable): Promise { if (linesA.length === 0) { return false; } - const maxIndexA = linesA.length - 1; + const aLen = linesA.length; + + const maxIndexA = aLen - 1; let index = -1; - for await (const lineB of source) { + const iterator = Symbol.asyncIterator in source + ? source[Symbol.asyncIterator]() + : ( + Symbol.iterator in source + ? source[Symbol.iterator]() + : null + ); + + invariant(iterator, 'source must be iterable or async iterable'); + + let result = await iterator.next(); + let lineB: string = result.value; + + while (!result.done) { index++; // b become bigger - if (index > maxIndexA) { + if (index === aLen) { return false; } @@ -35,12 +51,19 @@ export async function fileEqual(linesA: string[], source: AsyncIterable aFirstChar === 35 // # || aFirstChar === 33 // ! ) { + // eslint-disable-next-line no-await-in-loop -- sequential + result = await iterator.next(); + lineB = result.value; continue; } if (lineA !== lineB) { return false; } + + // eslint-disable-next-line no-await-in-loop -- sequential + result = await iterator.next(); + lineB = result.value; } // b is not smaller than a