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

r# strings in rust #176

Merged
merged 2 commits into from
Jan 12, 2024
Merged
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
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]
### Fixed
- (Rust) Support for r# raw strings with step definition patterns ([#176](https://github.com/cucumber/language-service/pull/176))

## [1.4.1] - 2023-07-16
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions src/language/rustLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\')
}
44 changes: 44 additions & 0 deletions test/language/rustLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})