From 42bb67a74df9ab7533f326b614860c39b1325c93 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Fri, 12 Jan 2024 21:42:07 +0000 Subject: [PATCH] r# strings in rust (#176) * Support r# strings in rust * CHANGELOG entry for r# rust string support --- CHANGELOG.md | 2 ++ src/language/rustLanguage.ts | 6 ++-- test/language/rustLanguage.test.ts | 44 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 test/language/rustLanguage.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d6cdb184..8cf03445 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] +### Fixed +- (Rust) Support for r# raw strings with step definition patterns ([#176](https://github.com/cucumber/language-service/pull/176)) ### Fixed - (Python) Unexpected spaces and commas in generated step definitions [#160](https://github.com/cucumber/language-service/issues/160) diff --git a/src/language/rustLanguage.ts b/src/language/rustLanguage.ts index 8dbedd2b..5d15f40c 100644 --- a/src/language/rustLanguage.ts +++ b/src/language/rustLanguage.ts @@ -109,13 +109,13 @@ fn {{ #lowercase }}{{ #underscore }}{{ expression }}{{ /underscore }}{{ /lowerca `, } -function stringLiteral(node: TreeSitterSyntaxNode | null): string { +export function stringLiteral(node: TreeSitterSyntaxNode | null): string { if (node === null) throw new Error('node cannot be null') - if (node.text[0] === 'r') return unescapeString(node.text.slice(2, -1)) + if (node.text.startsWith('r#')) return unescapeString(node.text.slice(3, -2)) + if (node.text.startsWith('r')) return unescapeString(node.text.slice(2, -1)) return unescapeString(node.text.slice(1, -1)) } -// rust function unescapeString(s: string): string { return s.replace(/\\\\/g, '\\') } diff --git a/test/language/rustLanguage.test.ts b/test/language/rustLanguage.test.ts new file mode 100644 index 00000000..31155f39 --- /dev/null +++ b/test/language/rustLanguage.test.ts @@ -0,0 +1,44 @@ +import assert from 'assert' + +import { stringLiteral } from '../../src/language/rustLanguage.js' +import { TreeSitterSyntaxNode } from '../../src/language/types.js' + +describe('rustLanguage', () => { + it('should extract string literal pattern', () => { + const cases = [ + { + input: '"the bee\'s knees"', + expected: "the bee's knees", + }, + { + input: '"a {uuid}"', + expected: 'a {uuid}', + }, + { + input: 'r"^a regexp$"', + expected: '^a regexp$', + }, + { + // eslint-disable-next-line + input: 'r"^stream (\w+) field (\w+) is a key$"', + // eslint-disable-next-line + expected: '^stream (\w+) field (\w+) is a key$', + }, + { + input: 'r#"I have cucumber in my belly"#', + expected: 'I have cucumber in my belly', + }, + ] + + cases.forEach(({ input, expected }) => { + const node: TreeSitterSyntaxNode = { + type: '', + text: input, + children: [], + startPosition: { row: 0, column: 0 }, + endPosition: { row: 0, column: 0 }, + } + assert.strictEqual(stringLiteral(node), expected) + }) + }) +})