Skip to content

Commit e1b8225

Browse files
committed
typings: add FailedMatchResult / SucceededMatchResult
1 parent 891b6d8 commit e1b8225

3 files changed

Lines changed: 48 additions & 34 deletions

File tree

packages/ohm-js/index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,29 @@ export interface MatchResult {
168168
/**
169169
* True iff match succeeded
170170
*/
171-
succeeded(): boolean;
171+
succeeded(): this is SucceededMatchResult;
172172

173173
/**
174174
* True iff match did not succeed
175175
*/
176-
failed(): boolean;
176+
failed(): this is FailedMatchResult;
177+
}
178+
179+
export interface SucceededMatchResult extends MatchResult {}
177180

181+
export interface FailedMatchResult extends MatchResult {
178182
/**
179183
* If match failed contains an error message indicating where and
180184
* why the match failed. This message is suitable for end users of a
181185
* language (i.e., people who do not have access to the grammar source).
182186
*/
183-
message?: string;
187+
message: string;
184188

185189
/**
186190
* If match failed contains an abbreviated version of this.message that
187191
* does not include an excerpt from the invalid input.
188192
*/
189-
shortMessage?: string;
193+
shortMessage: string;
190194

191195
/**
192196
* If this MatchResult is a failure, returns an Interval indicating

packages/ohm-js/scripts/data/index.d.ts.template

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,30 @@ export interface MatchResult {
167167
/**
168168
* True iff match succeeded
169169
*/
170-
succeeded(): boolean;
170+
succeeded(): this is SucceededMatchResult;
171171

172172
/**
173173
* True iff match did not succeed
174174
*/
175-
failed(): boolean;
175+
failed(): this is FailedMatchResult;
176+
}
177+
178+
export interface SucceededMatchResult extends MatchResult {
179+
}
176180

181+
export interface FailedMatchResult extends MatchResult {
177182
/**
178183
* If match failed contains an error message indicating where and
179184
* why the match failed. This message is suitable for end users of a
180185
* language (i.e., people who do not have access to the grammar source).
181186
*/
182-
message?: string;
187+
message: string;
183188

184189
/**
185190
* If match failed contains an abbreviated version of this.message that
186191
* does not include an excerpt from the invalid input.
187192
*/
188-
shortMessage?: string;
193+
shortMessage: string;
189194

190195
/**
191196
* If this MatchResult is a failure, returns an Interval indicating

packages/packaging-tests/test/test-typings.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ test('extras - getLineAndColumn & getLineAndColumnMessage', () => {
7272

7373
test('getLineAndColumn - #410', () => {
7474
const matchResult = g.match('Sup friend!');
75-
assert.is(matchResult.failed(), true);
76-
const lineAndCol = matchResult.getInterval().getLineAndColumn();
77-
assert.is(lineAndCol.offset, 0);
78-
assert.is(lineAndCol.lineNum, 1);
79-
assert.is(lineAndCol.colNum, 1);
80-
assert.is(lineAndCol.line, 'Sup friend!');
81-
assert.is(lineAndCol.prevLine, null);
82-
assert.is(lineAndCol.nextLine, null);
75+
if (matchResult.failed()) {
76+
const lineAndCol = matchResult.getInterval().getLineAndColumn();
77+
assert.is(lineAndCol.offset, 0);
78+
assert.is(lineAndCol.lineNum, 1);
79+
assert.is(lineAndCol.colNum, 1);
80+
assert.is(lineAndCol.line, 'Sup friend!');
81+
assert.is(lineAndCol.prevLine, null);
82+
assert.is(lineAndCol.nextLine, null);
83+
} else {
84+
assert.unreachable('Expected match failure');
85+
}
8386
});
8487

8588
test('asIteration - #407', () => {
@@ -100,24 +103,26 @@ test('asIteration - #407', () => {
100103
test('Interval typings', () => {
101104
const inputStr = ' Sup friend! ';
102105
const matchFailure = g.match(inputStr);
103-
assert.is(matchFailure.failed(), true);
104-
105-
const interval = matchFailure.getInterval();
106-
interval.startIdx = 1;
107-
interval.endIdx = inputStr.length;
108-
assert.is(interval.sourceString, ' Sup friend! ');
109-
assert.is(interval.contents, 'Sup friend! ');
110-
assert.is(interval.trimmed().contents, 'Sup friend!');
111-
112-
const left = interval.collapsedLeft();
113-
const right = interval.collapsedRight();
114-
assert.is(left.startIdx, interval.startIdx);
115-
assert.is(right.startIdx, interval.endIdx);
116-
117-
const fat = interval.minus(interval.trimmed());
118-
assert.is(fat.length, 1);
119-
assert.is(fat[0].contents, ' ');
120-
assert.is(fat[0].relativeTo(interval).startIdx, 11);
106+
if (matchFailure.failed()) {
107+
const interval = matchFailure.getInterval();
108+
interval.startIdx = 1;
109+
interval.endIdx = inputStr.length;
110+
assert.is(interval.sourceString, ' Sup friend! ');
111+
assert.is(interval.contents, 'Sup friend! ');
112+
assert.is(interval.trimmed().contents, 'Sup friend!');
113+
114+
const left = interval.collapsedLeft();
115+
const right = interval.collapsedRight();
116+
assert.is(left.startIdx, interval.startIdx);
117+
assert.is(right.startIdx, interval.endIdx);
118+
119+
const fat = interval.minus(interval.trimmed());
120+
assert.is(fat.length, 1);
121+
assert.is(fat[0].contents, ' ');
122+
assert.is(fat[0].relativeTo(interval).startIdx, 11);
123+
} else {
124+
assert.unreachable('Expected match failure');
125+
}
121126
});
122127

123128
test.run();

0 commit comments

Comments
 (0)