From 260203b81a5e26ffa38c45cd80458ca5328d8723 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Tue, 9 Apr 2024 20:13:08 +0100 Subject: [PATCH 1/2] Pretty comments --- .gitignore | 1 + javascript/src/pretty.ts | 7 ++++++- javascript/test/prettyTest.ts | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a09c56df..88284629 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.DS_Store /.idea diff --git a/javascript/src/pretty.ts b/javascript/src/pretty.ts index f5cfc540..cb3d0832 100644 --- a/javascript/src/pretty.ts +++ b/javascript/src/pretty.ts @@ -11,7 +11,7 @@ export default function pretty( let scenarioLevel = 1 return walkGherkinDocument(gherkinDocument, '', { comment(comment, content) { - return content.concat(comment.text).concat('\n') + return content.concat(prettyComment(comment.text)).concat('\n') }, feature(feature, content) { return content @@ -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() +} diff --git a/javascript/test/prettyTest.ts b/javascript/test/prettyTest.ts index 1473131e..73f8347c 100644 --- a/javascript/test/prettyTest.ts +++ b/javascript/test/prettyTest.ts @@ -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') @@ -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('inline hashtag should be ignored', () => { + assert.deepStrictEqual(prettyComment('number #1'), 'number #1') + }) + }) }) function checkGherkinToAstToMarkdownToAstToGherkin(gherkinSource: string) { From 14b3f8341a970d3784a700d6a2e6a790bf16e33a Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Tue, 9 Apr 2024 20:26:00 +0100 Subject: [PATCH 2/2] Changelog comment formatting --- CHANGELOG.md | 2 ++ javascript/test/prettyTest.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6381fac4..cf41563f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/javascript/test/prettyTest.ts b/javascript/test/prettyTest.ts index 73f8347c..41ad2ace 100644 --- a/javascript/test/prettyTest.ts +++ b/javascript/test/prettyTest.ts @@ -302,7 +302,7 @@ Feature: hello it('should retain spaces between hashtag and text', () => { assert.deepStrictEqual(prettyComment('# text'), '# text') }) - it('inline hashtag should be ignored', () => { + it('should not introduce space after inline hashtag', () => { assert.deepStrictEqual(prettyComment('number #1'), 'number #1') }) })