Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: eemeli/yaml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.4.0
Choose a base ref
...
head repository: eemeli/yaml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4aa56d337dc5e286eb0c9111a3b370f21e321117
Choose a head ref
  • 8 commits
  • 10 files changed
  • 1 contributor

Commits on Feb 25, 2024

  1. ci: Update yaml-playground build & BrowserStack tests (#524)

    eemeli authored Feb 25, 2024
    Copy the full SHA
    5ba73d1 View commit details
  2. ci: Move update action to yaml-playground, where it has write access

    eemeli committed Feb 25, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e07998c View commit details

Commits on Mar 5, 2024

  1. fix: Stringify flow collection comments in parent (fixes #528)

    eemeli committed Mar 5, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    750adbe View commit details

Commits on Mar 6, 2024

  1. fix: Do not skip folding lines after the first in indented block scal…

    …ars (fixes #529)
    eemeli committed Mar 6, 2024
    Copy the full SHA
    46a816e View commit details
  2. fix(cst): Do not drop trailing newline after line comment in block-ma…

    …p if followed by unindented block-seq value (fixes #525)
    eemeli committed Mar 6, 2024
    Copy the full SHA
    40903db View commit details
  3. test: Drop unused import to satisfy TS

    eemeli committed Mar 6, 2024
    Copy the full SHA
    1775f13 View commit details
  4. chore: Refresh lockfile

    eemeli committed Mar 6, 2024
    Copy the full SHA
    e651ce1 View commit details
  5. 2.4.1

    eemeli committed Mar 6, 2024
    Copy the full SHA
    4aa56d3 View commit details
Showing with 320 additions and 240 deletions.
  1. +19 −11 .github/workflows/browsers.yml
  2. +180 −180 package-lock.json
  3. +1 −1 package.json
  4. +1 −1 playground
  5. +5 −1 src/parse/parser.ts
  6. +19 −10 src/stringify/foldFlowLines.ts
  7. +6 −13 src/stringify/stringifyCollection.ts
  8. +6 −0 tests/cst.ts
  9. +28 −0 tests/doc/comments.ts
  10. +55 −23 tests/doc/foldFlowLines.ts
30 changes: 19 additions & 11 deletions .github/workflows/browsers.yml
Original file line number Diff line number Diff line change
@@ -13,23 +13,31 @@ jobs:
with: { submodules: true }
- uses: actions/setup-node@v4
with: { node-version: 20 }
- name: Cache BrowserStackLocal
uses: actions/cache@v4
- uses: browserstack/github-actions/setup-env@master
with:
path: ~/.browserstack
key: bsl-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
bsl-${{ runner.os }}-
username: ${{ secrets.BROWSERSTACK_USERNAME }}
access-key: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
project-name: yaml
- uses: browserstack/github-actions/setup-local@master
with:
local-testing: start
local-identifier: random

- run: npm ci
- run: npm run build:browser
- name: Playground setup
working-directory: ./playground
run: npm ci
- name: Playground build
working-directory: ./playground
run: npm run build
- run: npx http-server site &
working-directory: ./playground

- name: Run tests on BrowserStack
- run: npx browserstack-node-sdk jest
working-directory: ./playground
run: npm test
env:
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}

- uses: browserstack/github-actions/setup-local@master
if: always()
with:
local-testing: stop
360 changes: 180 additions & 180 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yaml",
"version": "2.4.0",
"version": "2.4.1",
"license": "ISC",
"author": "Eemeli Aro <eemeli@gmail.com>",
"repository": "github:eemeli/yaml",
6 changes: 5 additions & 1 deletion src/parse/parser.ts
Original file line number Diff line number Diff line change
@@ -529,7 +529,11 @@ export class Parser {
}

if (this.indent >= map.indent) {
const atNextItem = !this.onKeyLine && this.indent === map.indent && it.sep
const atNextItem =
!this.onKeyLine &&
this.indent === map.indent &&
it.sep &&
this.type !== 'seq-item-ind'

// For empty nodes, assign newline-separated not indented empty tokens to following node
let start: SourceToken[] = []
29 changes: 19 additions & 10 deletions src/stringify/foldFlowLines.ts
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ export function foldFlowLines(
let escStart = -1
let escEnd = -1
if (mode === FOLD_BLOCK) {
i = consumeMoreIndentedLines(text, i)
i = consumeMoreIndentedLines(text, i, indent.length)
if (i !== -1) end = i + endStep
}
for (let ch; (ch = text[(i += 1)]); ) {
@@ -89,8 +89,9 @@ export function foldFlowLines(
escEnd = i
}
if (ch === '\n') {
if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i)
end = i + endStep
if (mode === FOLD_BLOCK)
i = consumeMoreIndentedLines(text, i, indent.length)
end = i + indent.length + endStep
split = undefined
} else {
if (
@@ -151,13 +152,21 @@ export function foldFlowLines(
* Presumes `i + 1` is at the start of a line
* @returns index of last newline in more-indented block
*/
function consumeMoreIndentedLines(text: string, i: number) {
let ch = text[i + 1]
function consumeMoreIndentedLines(text: string, i: number, indent: number) {
let end = i
let start = i + 1
let ch = text[start]
while (ch === ' ' || ch === '\t') {
do {
ch = text[(i += 1)]
} while (ch && ch !== '\n')
ch = text[i + 1]
if (i < start + indent) {
ch = text[++i]
} else {
do {
ch = text[++i]
} while (ch && ch !== '\n')
end = i
start = i + 1
ch = text[start]
}
}
return i
return end
}
19 changes: 6 additions & 13 deletions src/stringify/stringifyCollection.ts
Original file line number Diff line number Diff line change
@@ -87,9 +87,9 @@ function stringifyBlockCollection(
}

function stringifyFlowCollection(
{ comment, items }: Readonly<Collection>,
{ items }: Readonly<Collection>,
ctx: StringifyContext,
{ flowChars, itemIndent, onComment }: StringifyCollectionOptions
{ flowChars, itemIndent }: StringifyCollectionOptions
) {
const {
indent,
@@ -141,30 +141,23 @@ function stringifyFlowCollection(
linesAtValue = lines.length
}

let str: string
const { start, end } = flowChars
if (lines.length === 0) {
str = start + end
return start + end
} else {
if (!reqNewline) {
const len = lines.reduce((sum, line) => sum + line.length + 2, 2)
reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth
}
if (reqNewline) {
str = start
let str = start
for (const line of lines)
str += line ? `\n${indentStep}${indent}${line}` : '\n'
str += `\n${indent}${end}`
return `${str}\n${indent}${end}`
} else {
str = `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`
return `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`
}
}

if (comment) {
str += lineComment(str, indent, commentString(comment))
if (onComment) onComment()
}
return str
}

function addCommentBefore(
6 changes: 6 additions & 0 deletions tests/cst.ts
Original file line number Diff line number Diff line change
@@ -195,3 +195,9 @@ describe('CST.visit', () => {
expect(visits).toBe(3)
})
})

test('Line comment before unindented block-seq in block-map (eemeli/yaml#525)', () => {
const src = 'a:\n#\n- b'
const [doc] = Array.from(new Parser().parse(src))
expect(CST.stringify(doc)).toBe(src)
})
28 changes: 28 additions & 0 deletions tests/doc/comments.ts
Original file line number Diff line number Diff line change
@@ -540,6 +540,19 @@ describe('stringify comments', () => {
`)
})

test('line comment after [], in seq', () => {
const doc = YAML.parseDocument(source`
[ [a], #c0
[b] #c1
]`)
expect(String(doc)).toBe(source`
[
[ a ], #c0
[ b ] #c1
]
`)
})

test('line comment after , in map', () => {
const doc = YAML.parseDocument(source`
{ a, #c0
@@ -555,6 +568,21 @@ describe('stringify comments', () => {
`)
})

test('line comment after {}, in map', () => {
const doc = YAML.parseDocument(source`
{ {a: 1}, #c0
b: {c: 2}, #c1
d #c2
}`)
expect(String(doc)).toBe(source`
{
? { a: 1 }, #c0
b: { c: 2 }, #c1
d #c2
}
`)
})

test('line comment after flow collection (eemeli/yaml#443)', () => {
const doc = YAML.parseDocument(source`
[ value1, value2 ] # comment
78 changes: 55 additions & 23 deletions tests/doc/foldFlowLines.ts
Original file line number Diff line number Diff line change
@@ -278,31 +278,33 @@ describe('end-to-end', () => {
const foldOptions = { lineWidth: 20, minContentWidth: 0 }

test('more-indented folded block', () => {
const src = `> # comment with an excessive length that won't get folded
Text on a line that
should get folded
with a line width of
20 characters.
const src = source`
> # comment with an excessive length that won't get folded
Text on a line that
should get folded
with a line width of
20 characters.
Indented text
that appears to be
folded but is not.
Indented text
that appears to be
folded but is not.
Text that is prevented from folding due to being more-indented.
Text that is prevented from folding due to being more-indented.
Unfolded paragraph.\n`
Unfolded paragraph.
`
const doc = YAML.parseDocument<YAML.Scalar, false>(src)
expect(doc.contents.value).toBe(
`Text on a line that should get folded with a line width of 20 characters.
expect(doc.contents.value).toBe(source`
Text on a line that should get folded with a line width of 20 characters.
Indented text
that appears to be
folded but is not.
Indented text
that appears to be
folded but is not.
Text that is prevented from folding due to being more-indented.
Text that is prevented from folding due to being more-indented.
Unfolded paragraph.\n`
)
Unfolded paragraph.
`)
expect(doc.toString(foldOptions)).toBe(src)
})

@@ -313,10 +315,12 @@ Unfolded paragraph.\n`
})

test('plain string', () => {
const src = `- plain value with
enough length to
fold twice
- plain with comment # that won't get folded\n`
const src = source`
- plain value with
enough length to
fold twice
- plain with comment # that won't get folded
`
const doc = YAML.parseDocument<YAML.YAMLSeq<YAML.Scalar>, false>(src)
expect(doc.contents.items[0].value).toBe(
'plain value with enough length to fold twice'
@@ -326,7 +330,8 @@ Unfolded paragraph.\n`
})

test('long line width', () => {
const src = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.
const src = `\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.
Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti. \n`

@@ -338,4 +343,31 @@ Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condiment
for (const lineWidth of [1000, 0, -1])
expect(YAML.stringify(src, { lineWidth })).toBe(exp)
})

test('multiple lines', () => {
const src = source`
first line which is long enough to be wrapped to another line
second line which is long enough to be wrapped to another line
third line which is not wrapped because it's indented
fourth line which is long enough to be wrapped to another line
`
expect(YAML.stringify({ src }, { lineWidth: 20, minContentWidth: 0 }))
.toBe(source`
src: >
first line which
is long enough to
be wrapped to
another line
second line which
is long enough to
be wrapped to
another line
third line which is not wrapped because it's indented
fourth line which
is long enough to
be wrapped to
another line
`)
})
})