Skip to content

Commit

Permalink
Add support for basic TS assertion signature syntax (#504)
Browse files Browse the repository at this point in the history
Fixes #503

The previous code didn't handle the basic case `asserts x`, which I just
overlooked when backporting Babel parser changes. The new code has a special
case for that and conditionally tells the caller whether to parse a type
afterward. I also made the contract a little simpler; it either fully parses the
return type or it doesn't.
  • Loading branch information
alangpierce authored Jan 13, 2020
1 parent 8e4afa1 commit c1976a6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,10 @@ function tsIsUnambiguouslyStartOfFunctionType(): boolean {
function tsParseTypeOrTypePredicateAnnotation(returnToken: TokenType): void {
const oldIsType = pushTypeContext(0);
expect(returnToken);
tsParseTypePredicateOrAssertsPrefix();
// Regardless of whether we found an "asserts" or "is" token, there's now just a regular type in
// front of us.
tsParseType();
const finishedReturn = tsParseTypePredicateOrAssertsPrefix();
if (!finishedReturn) {
tsParseType();
}
popTypeContext(oldIsType);
}

Expand All @@ -600,33 +600,49 @@ function tsTryParseType(): void {
}
}

function tsParseTypePredicateOrAssertsPrefix(): void {
/**
* Detect a few special return syntax cases: `x is T`, `asserts x`, `asserts x is T`,
* `asserts this is T`.
*
* Returns true if we parsed the return type, false if there's still a type to be parsed.
*/
function tsParseTypePredicateOrAssertsPrefix(): boolean {
const snapshot = state.snapshot();
if (isContextual(ContextualKeyword._asserts) && !hasPrecedingLineBreak()) {
// Normally this is `asserts x is T`, but at this point, it might be `asserts is T` (a user-
// defined type guard on the `asserts` variable) or just a type called `asserts`.
next();
if (isContextual(ContextualKeyword._is)) {
if (eatContextual(ContextualKeyword._is)) {
// If we see `asserts is`, then this must be of the form `asserts is T`, since
// `asserts is is T` isn't valid.
next();
tsParseType();
return true;
} else if (tsIsIdentifier() || match(tt._this)) {
next();
expectContextual(ContextualKeyword._is);
if (eatContextual(ContextualKeyword._is)) {
// If we see `is`, then this is `asserts x is T`. Otherwise, it's `asserts x`.
tsParseType();
}
return true;
} else {
// Regular type, so bail out and start type parsing from scratch.
state.restoreFromSnapshot(snapshot);
return false;
}
} else if (tsIsIdentifier() || match(tt._this)) {
// This is a regular identifier, which may or may not have "is" after it.
next();
if (isContextual(ContextualKeyword._is) && !hasPrecedingLineBreak()) {
next();
tsParseType();
return true;
} else {
// Regular type, so bail out and start type parsing from scratch.
state.restoreFromSnapshot(snapshot);
return false;
}
}
return false;
}

export function tsParseTypeAnnotation(): void {
Expand Down
19 changes: 19 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,25 @@ describe("typescript transform", () => {
});

it("allows assertion signature syntax", () => {
assertTypeScriptResult(
`
function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new AssertionError(msg)
}
}
`,
`"use strict";
function assert(condition, msg) {
if (!condition) {
throw new AssertionError(msg)
}
}
`,
);
});

it("allows assertion signature syntax with is", () => {
assertTypeScriptResult(
`
function assertIsDefined<T>(x: T): asserts x is NonNullable<T> {
Expand Down

0 comments on commit c1976a6

Please sign in to comment.