Skip to content

Commit

Permalink
Fix posix class parsing (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Feb 1, 2025
1 parent bc4fa8c commit e27ed71
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion spec/match-char-class.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
import {toRegExpDetails} from '../dist/esm/index.js';
import {toRegExp, toRegExpDetails} from '../dist/esm/index.js';
import {matchers} from './helpers/matchers.js';

beforeEach(() => {
jasmine.addMatchers(matchers);
});

describe('CharacterClass', () => {
// See also `match-char-class-range.spec.js` and `match-char-class-intersection.spec.js`
// Tests for specific tokens within char classes are mixed into specs elsewhere

// <github.com/slevithan/oniguruma-to-es/issues/23#issuecomment-2597598227>
describe('posix class vs nested class', () => {
it('should throw for invalid posix classes', () => {
[ '[[:^:]]',
'[[:^u:]]',
'[[:^uper:]]',
'[[:u:]]',
'[[:upp:]]',
'[[:uppers:]]',
'[[:\u212A:]]',
].forEach(p => {
expect(() => toRegExpDetails(p)).toThrow();
});
});

it('should interpret non-posix classes starting/ending with ":" as standard nested classes', () => {
[ '[[:^1:]]',
'[[:1:]]',
'[[:upper :]]',
'[[:upper1:]]',
'[[:]]',
'[[::]]',
'[[:::]]',
'[[:abc[:upper:]def:]]',
].forEach(p => {
expect(':').toExactlyMatch(p);
});
});

it('should throw for unclosed classes', () => {
[ '[[:::]',
'[[:[:[:[:upper:]]',
].forEach(p => {
expect(() => toRegExpDetails(p)).toThrow();
});
});
});

describe('nested class unwrapping', () => {
it('should unwrap unneeded nested classes', () => {
expect(toRegExpDetails('[[ab]]').pattern).toBe('[ab]');
Expand Down
2 changes: 1 addition & 1 deletion src/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const charClassTokenRe = new RegExp(r`
${sharedEscapesPattern}
| .
)
| \[:[^:]*:\]
| \[:(?:\^?\p{Alpha}+|\^):\]
| ${charClassOpenPattern}
| &&
| .
Expand Down

0 comments on commit e27ed71

Please sign in to comment.