Skip to content

Commit

Permalink
Fix off-by-one error in lineStart reset (Really fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jun 15, 2018
1 parent f6c7bfd commit 56b7811
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 3 deletions.
96 changes: 96 additions & 0 deletions __tests__/artifacts/prettier-circleci-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
aliases:
# Cache management
- &restore_yarn_cache
restore_cache:
keys:
- v1-yarn-cache

- &save_yarn_cache
save_cache:
paths:
- ~/.cache/yarn
key: v1-yarn-cache

- &restore_deps_cache
restore_cache:
keys:
- v1-deps-cache-{{ checksum "yarn.lock" }}

- &save_deps_cache
save_cache:
paths:
- node_modules
key: v1-yarn-deps-{{ checksum "yarn.lock" }}

# Default
- &defaults
working_directory: ~/prettier
docker:
- image: circleci/node:9

version: 2
jobs:
# Install dependencies and cache everything
checkout_code:
<<: *defaults
steps:
- checkout
- *restore_yarn_cache
- *restore_deps_cache
- run: yarn install
- run: yarn check-deps
- *save_deps_cache
- *save_yarn_cache
- persist_to_workspace:
root: .
paths:
- .

# Create the production bundle and cache
build_prod:
<<: *defaults
environment:
NODE_ENV: production
steps:
- attach_workspace:
at: ~/prettier
- run: yarn build
- persist_to_workspace:
root: .
paths:
- dist
- store_artifacts:
path: ~/prettier/dist

# Run tests on the production bundle
test_prod_node4:
<<: *defaults
docker:
- image: circleci/node:4
steps:
- attach_workspace:
at: ~/prettier
- run: yarn test:dist

# Run tests on the production bundle
test_prod_node9:
<<: *defaults
steps:
- attach_workspace:
at: ~/prettier
- run: yarn test:dist

workflows:
version: 2
prod:
jobs:
- checkout_code
- build_prod:
requires:
- checkout_code
- test_prod_node4:
requires:
- build_prod
- test_prod_node9:
requires:
- build_prod
14 changes: 12 additions & 2 deletions __tests__/ast/corner-cases.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Node, { Type } from '../../src/ast/Node'
import parse from '../../src/ast/parse'
import { pretty } from './common'

describe('folded block with chomp: keep', () => {
test('nl + nl', () => {
Expand Down Expand Up @@ -90,3 +88,15 @@ describe('seq unindent to non-empty indent', () => {
expect(doc.contents[0].items[1].error).toBeNull()
})
})

test('eemeli/yaml#10', () => {
const src = `
a:
- b
c: d
`
const doc = parse(src)[0]
expect(doc.contents).toHaveLength(1)
expect(doc.contents[0].items).toHaveLength(4)
expect(doc.contents[0].items[1].error).toBeNull()
})
103 changes: 103 additions & 0 deletions __tests__/corner-cases.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs'
import path from 'path'
import Node from '../src/ast/Node'
import { YAMLSemanticError } from '../src/errors'
import YAML from '../src/index'
Expand Down Expand Up @@ -70,6 +72,107 @@ aliases:
expect(docs).toHaveLength(1)
expect(docs[0].errors).toHaveLength(0)
})

test('complete file', () => {
const src = fs.readFileSync(
path.resolve(__dirname, './artifacts/prettier-circleci-config.yml'),
'utf8'
)
const cfg = YAML.parse(src)
expect(cfg).toMatchObject({
aliases: [
{ restore_cache: { keys: ['v1-yarn-cache'] } },
{ save_cache: { key: 'v1-yarn-cache', paths: ['~/.cache/yarn'] } },
{
restore_cache: { keys: ['v1-deps-cache-{{ checksum "yarn.lock" }}'] }
},
{
save_cache: {
key: 'v1-yarn-deps-{{ checksum "yarn.lock" }}',
paths: ['node_modules']
}
},
{
docker: [{ image: 'circleci/node:9' }],
working_directory: '~/prettier'
}
],
jobs: {
build_prod: {
'<<': {
docker: [{ image: 'circleci/node:9' }],
working_directory: '~/prettier'
},
environment: { NODE_ENV: 'production' },
steps: [
{ attach_workspace: { at: '~/prettier' } },
{ run: 'yarn build' },
{ persist_to_workspace: { paths: ['dist'], root: '.' } },
{ store_artifacts: { path: '~/prettier/dist' } }
]
},
checkout_code: {
'<<': {
docker: [{ image: 'circleci/node:9' }],
working_directory: '~/prettier'
},
steps: [
'checkout',
{ restore_cache: { keys: ['v1-yarn-cache'] } },
{
restore_cache: {
keys: ['v1-deps-cache-{{ checksum "yarn.lock" }}']
}
},
{ run: 'yarn install' },
{ run: 'yarn check-deps' },
{
save_cache: {
key: 'v1-yarn-deps-{{ checksum "yarn.lock" }}',
paths: ['node_modules']
}
},
{ save_cache: { key: 'v1-yarn-cache', paths: ['~/.cache/yarn'] } },
{ persist_to_workspace: { paths: ['.'], root: '.' } }
]
},
test_prod_node4: {
'<<': {
docker: [{ image: 'circleci/node:9' }],
working_directory: '~/prettier'
},
docker: [{ image: 'circleci/node:4' }],
steps: [
{ attach_workspace: { at: '~/prettier' } },
{ run: 'yarn test:dist' }
]
},
test_prod_node9: {
'<<': {
docker: [{ image: 'circleci/node:9' }],
working_directory: '~/prettier'
},
steps: [
{ attach_workspace: { at: '~/prettier' } },
{ run: 'yarn test:dist' }
]
}
},
version: 2,
workflows: {
prod: {
jobs: [
'checkout_code',
{ build_prod: { requires: ['checkout_code'] } },
{ test_prod_node4: { requires: ['build_prod'] } },
{ test_prod_node9: { requires: ['build_prod'] } }
]
},
version: 2
}
})
})

test('minimal', () => {
const src = `
- a
Expand Down
2 changes: 1 addition & 1 deletion src/ast/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class Collection extends Node {
let ls = offset - 1
let prev = src[ls]
while (prev === ' ' || prev === '\t') prev = src[--ls]
if (prev === '\n') lineStart = ls
if (prev === '\n') lineStart = ls + 1
}
trace: 'item-end', node.type, { offset, ch: JSON.stringify(ch) }
}
Expand Down

0 comments on commit 56b7811

Please sign in to comment.