Skip to content

Commit

Permalink
Strip Java case insensitive flags
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Oct 10, 2022
1 parent 3c07aa2 commit 2fa8585
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/language/javaLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ export const javaLanguage: Language = {
`,
}

function stringLiteral(node: TreeSitterSyntaxNode | null): string {
export function stringLiteral(node: TreeSitterSyntaxNode | null): string {
if (node === null) throw new Error('node cannot be null')
return unescapeString(node.text.slice(1, -1))
const string = node.text.slice(1, -1)
return unescapeString(string.replace('(?i)', ''))
}

// Java escapes \ as \\. Turn \\ back to \.
Expand Down
18 changes: 18 additions & 0 deletions test/language/javaLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert'

import { stringLiteral } from '../../src/language/javaLanguage.js'
import { TreeSitterSyntaxNode } from '../../src/language/types.js'

describe('javaLanguage', () => {
it('should remove (?i) from regexp strings', () => {
const node: TreeSitterSyntaxNode = {
type: 'string_literal',
text: '"(?i)(day|hour)s?"',
startPosition: { row: 1, column: 19 },
endPosition: { row: 1, column: 37 },
children: [],
}
const result = stringLiteral(node)
assert.deepStrictEqual(result, '(day|hour)s?')
})
})
4 changes: 2 additions & 2 deletions test/language/rubyLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TreeSitterSyntaxNode } from '../../src/language/types.js'

describe('rubyLanguage', () => {
it('should preserve regexp flags in step definitions', () => {
const regex: TreeSitterSyntaxNode = {
const node: TreeSitterSyntaxNode = {
type: 'regex',
text: '/^a regexp$/i',
startPosition: { row: 0, column: 6 },
Expand All @@ -20,7 +20,7 @@ describe('rubyLanguage', () => {
},
],
}
const result = toStringOrRegExp(regex)
const result = toStringOrRegExp(node)
assert.deepStrictEqual(result, /^a regexp$/i)
})
})

0 comments on commit 2fa8585

Please sign in to comment.