Skip to content

Commit d0e9865

Browse files
fix: rewrite string enum extractor as a token parser
1 parent f5d5caa commit d0e9865

File tree

2 files changed

+269
-53
lines changed

2 files changed

+269
-53
lines changed

src/__tests__/markdown-helpers.spec.ts

+70
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,76 @@ def fn():
104104
expect(extractStringEnum('wassup')).toBe(null);
105105
});
106106

107+
it('should error helpfully on invalid value separators', () => {
108+
expect(() => extractStringEnum('Can be `x` sometimes `y'))
109+
.toThrowErrorMatchingInlineSnapshot(`
110+
"Unexpected separator token while extracting string enum, expected a comma or "and" or "or" but found "s"
111+
Context: \`x\` sometimes \`y
112+
^"
113+
`);
114+
});
115+
116+
it('should error helpfully on unterminated enum strings', () => {
117+
expect(() => extractStringEnum('Can be `x` or `y')).toThrowErrorMatchingInlineSnapshot(`
118+
"Unexpected early termination of token sequence while extracting string enum, did you forget to close a quote?
119+
Context: \`x\` or \`y"
120+
`);
121+
});
122+
123+
describe('mixed ticks', () => {
124+
it('should extract an enum when mixed quotes are used', () => {
125+
const values = extractStringEnum('Can be `x"` or "`y"')!;
126+
expect(values).not.toBe(null);
127+
expect(values).toHaveLength(2);
128+
expect(values[0].value).toBe('x"');
129+
expect(values[1].value).toBe('`y');
130+
});
131+
});
132+
133+
describe('deprecated wrappers', () => {
134+
it('should handle strikethrough deprecation wrappers', () => {
135+
const values = extractStringEnum('Can be `x` or ~~`y`~~')!;
136+
expect(values).not.toBe(null);
137+
expect(values).toHaveLength(2);
138+
expect(values[0].value).toBe('x');
139+
expect(values[1].value).toBe('y');
140+
});
141+
});
142+
143+
describe('lead-in descriptions', () => {
144+
it('should handle value lists that smoothly lead in to prose with a comma', () => {
145+
const values = extractStringEnum('Can be `x` or `y`, where `x` implies that...')!;
146+
expect(values).not.toBe(null);
147+
expect(values).toHaveLength(2);
148+
expect(values[0].value).toBe('x');
149+
expect(values[1].value).toBe('y');
150+
});
151+
152+
it('should handle value lists that smoothly lead in to prose with a fullstop', () => {
153+
const values = extractStringEnum('Can be `x` or `y`. The `x` value implies that...')!;
154+
expect(values).not.toBe(null);
155+
expect(values).toHaveLength(2);
156+
expect(values[0].value).toBe('x');
157+
expect(values[1].value).toBe('y');
158+
});
159+
160+
it('should handle value lists that smoothly lead in to prose with a semicolon', () => {
161+
const values = extractStringEnum('Can be `x` or `y`; the `x` value implies that...')!;
162+
expect(values).not.toBe(null);
163+
expect(values).toHaveLength(2);
164+
expect(values[0].value).toBe('x');
165+
expect(values[1].value).toBe('y');
166+
});
167+
168+
it('should handle value lists that smoothly lead in to prose with a hyphen', () => {
169+
const values = extractStringEnum('Can be `x` or `y` - the `x` value implies that...')!;
170+
expect(values).not.toBe(null);
171+
expect(values).toHaveLength(2);
172+
expect(values[0].value).toBe('x');
173+
expect(values[1].value).toBe('y');
174+
});
175+
});
176+
107177
describe('with backticks', () => {
108178
it('should extract an enum of the format "can be x"', () => {
109179
const values = extractStringEnum('Can be `x`')!;

0 commit comments

Comments
 (0)