Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty comments #75

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
/.idea
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- [JavaScript] Trim comment trailing whitespace and introduce space after comment hashtag ([#75](https://github.com/cucumber/gherkin-utils/pull/75))

## [9.0.0] - 2024-03-26
### Changed
Expand Down
7 changes: 6 additions & 1 deletion javascript/src/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function pretty(
let scenarioLevel = 1
return walkGherkinDocument<string>(gherkinDocument, '', {
comment(comment, content) {
return content.concat(comment.text).concat('\n')
return content.concat(prettyComment(comment.text)).concat('\n')
},
feature(feature, content) {
return content
Expand Down Expand Up @@ -247,3 +247,8 @@ export function escapeCell(s: string) {
function isNumeric(s: string) {
return !isNaN(parseFloat(s))
}

export function prettyComment(comment: string): string {
// Ensure space after hashtag in a comment (`# `)
return comment.replace(/^(\s*#)(\S)/, '$1 $2').trimEnd()
}
20 changes: 19 additions & 1 deletion javascript/test/prettyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fg from 'fast-glob'
import fs from 'fs'
import path from 'path'

import pretty, { escapeCell } from '../src/pretty'
import pretty, { escapeCell, prettyComment } from '../src/pretty'
import parse from './parse'

const featuresPath = path.resolve(__dirname, '../node_modules/@cucumber/compatibility-kit/features')
Expand Down Expand Up @@ -288,6 +288,24 @@ Feature: hello
assert.strictEqual(escapeCell('\\'), '\\\\')
})
})

describe('prettyComment', () => {
it('should add missing space after hashtag', () => {
assert.deepStrictEqual(prettyComment('#text'), '# text')
})
it('should retain leading whitespace', () => {
assert.deepStrictEqual(prettyComment(' # text'), ' # text')
})
it('should trim trailing whitespace', () => {
assert.deepStrictEqual(prettyComment(' # text '), ' # text')
})
it('should retain spaces between hashtag and text', () => {
assert.deepStrictEqual(prettyComment('# text'), '# text')
})
it('should not introduce space after inline hashtag', () => {
assert.deepStrictEqual(prettyComment('number #1'), 'number #1')
})
})
})

function checkGherkinToAstToMarkdownToAstToGherkin(gherkinSource: string) {
Expand Down