Skip to content

Commit 3c8fa48

Browse files
committed
fix(internal): space resolution after eol without trimming
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 81d4087 commit 3c8fa48

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

__fixtures__/cli-argument-help.mts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file Fixtures - cliArgumentHelp
3+
* @module fixtures/cliArgumentHelp
4+
*/
5+
6+
import digits from '#fixtures/digits'
7+
import { chars } from '@flex-development/fsm-tokenizer'
8+
9+
/**
10+
* A string containing several sequences and a line break.
11+
*
12+
* @type {string}
13+
*/
14+
export default 'first value in starting sequence' +
15+
chars.lf +
16+
chars.space.repeat(2) +
17+
'choices' +
18+
chars.colon +
19+
chars.space +
20+
JSON.stringify(digits)

src/constructs/space.mts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ function resolveSpace(this: void, events: Event[]): Event[] {
5757
self.flush()
5858
if (!self.trim) self.line += token.value
5959
} else if (width(self.line) || !self.trim) {
60+
self.line += token.value
61+
6062
// this space belongs on the newline specified by `self.string`.
6163
// drop the last line (which is empty) to prevent duplicate newlines.
6264
// when the current line is flushed, it'll replace the popped line.
63-
if (self.events.at(-3)?.[1].type === tt.eol) self.lines.pop()
64-
self.line += token.value
65+
if (self.events.at(-3)?.[1].type === tt.eol && !self.lines.at(-1)) {
66+
self.lines.pop()
67+
}
6568
}
6669

6770
return events

src/internal/__snapshots__/happy-dom/tokenize.functional.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,18 @@ exports[`functional:internal/tokenize > options.trim > should not trim lines if
35863586
}
35873587
`;
35883588
3589+
exports[`functional:internal/tokenize > options.trim > should not trim lines if disabled (10) 1`] = `
3590+
{
3591+
"candidate": ""first value in starting sequence\\n choices: [\\"0\\",\\"1\\",\\"2\\",\\"3\\",\\"4\\",\\"5\\",\\"6\\",\\"7\\",\\"8\\",\\"9\\"]"",
3592+
"cols": 66,
3593+
"columns": 66,
3594+
"lines": [
3595+
"first value in starting sequence",
3596+
" choices: ["0","1","2","3","4","5","6","7","8","9"]",
3597+
],
3598+
}
3599+
`;
3600+
35893601
exports[`functional:internal/tokenize > options.trim > should trim whitespace only on actual wrapped lines if enabled (0) 1`] = `
35903602
{
35913603
"candidate": "" foo bar "",

src/internal/__snapshots__/node/tokenize.functional.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,18 @@ exports[`functional:internal/tokenize > options.trim > should not trim lines if
35863586
}
35873587
`;
35883588
3589+
exports[`functional:internal/tokenize > options.trim > should not trim lines if disabled (10) 1`] = `
3590+
{
3591+
"candidate": ""first value in starting sequence\\n choices: [\\"0\\",\\"1\\",\\"2\\",\\"3\\",\\"4\\",\\"5\\",\\"6\\",\\"7\\",\\"8\\",\\"9\\"]"",
3592+
"cols": 66,
3593+
"columns": 66,
3594+
"lines": [
3595+
"first value in starting sequence",
3596+
" choices: ["0","1","2","3","4","5","6","7","8","9"]",
3597+
],
3598+
}
3599+
`;
3600+
35893601
exports[`functional:internal/tokenize > options.trim > should trim whitespace only on actual wrapped lines if enabled (0) 1`] = `
35903602
{
35913603
"candidate": "" foo bar "",

src/internal/__tests__/tokenize.functional.spec.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ansiFixture07 from '#fixtures/ansi-fixture-07'
1212
import ansiFixture08 from '#fixtures/ansi-fixture-08'
1313
import ansiFixture09 from '#fixtures/ansi-fixture-09'
1414
import ansiFixture10 from '#fixtures/ansi-fixture-10'
15+
import cliArgumentHelp from '#fixtures/cli-argument-help'
1516
import digitSequence from '#fixtures/digit-sequence'
1617
import digitSequence2 from '#fixtures/digit-sequence-2'
1718
import digitSequences from '#fixtures/digit-sequences'
@@ -507,7 +508,8 @@ describe('functional:internal/tokenize', () => {
507508
[fooBar2, chars.digit3],
508509
[ansiFixture07, 100],
509510
[colors.bgGreen(` ${colors.black('ok')} `), 100],
510-
[ansiFixture10, chars.digit8]
511+
[ansiFixture10, chars.digit8],
512+
[cliArgumentHelp, 66]
511513
])('should not trim lines if disabled (%#)', (thing, columns) => {
512514
// Act
513515
const result = testSubject(thing, columns, { trim: false })

0 commit comments

Comments
 (0)