Skip to content

Commit

Permalink
reimplement asserts-this
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 9, 2019
1 parent 56e4c01 commit e6bf63e
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -940,42 +940,41 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.tsParseTypePredicateAsserts.bind(this),
);

let typePredicateVariable = null;
if (this.tsIsIdentifier()) {
// TypePredicate
// : foo is type
typePredicateVariable = this.tsTryParse(
this.tsParseTypePredicatePrefix.bind(this),
);
} else if (asserts && this.match(tt._this)) {
// When asserts is false, thisKeyword is handled by tsParseNonArraytype
// TypePredicate
// : this is type
typePredicateVariable = this.tsTryParse(
this.tsParseThisTypeOrThisTypePredicate.bind(this),
);
if (asserts && this.match(tt._this)) {
// When asserts is false, thisKeyword is handled by tsParseNonArrayType
const node = this.startNodeAtNode(t);
// : asserts this is type
let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
// if it turns out to be a `TSThisType`, wrap it with `TSTypePredicate`
// : asserts this
if (thisTypePredicate.type === "TSThisType") {
node.parameterName = thisTypePredicate;
node.asserts = true;
thisTypePredicate = this.finishNode(node, "TSTypePredicate");
}
t.typeAnnotation = thisTypePredicate;
return this.finishNode(t, "TSTypeAnnotation");
}

const typePredicateVariable =
this.tsIsIdentifier() &&
this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));

if (!typePredicateVariable) {
if (!asserts) {
// : type
return this.tsParseTypeAnnotation(/* eatColon */ false, t);
}

const node = this.startNodeAtNode(t);
if (this.match(tt._this)) {
// : asserts this
node.parameterName = this.tsParseThisTypeNode();
} else {
// : asserts foo
node.parameterName = this.parseIdentifier();
}
// : asserts foo
node.parameterName = this.parseIdentifier();
node.asserts = asserts;
t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
return this.finishNode(t, "TSTypeAnnotation");
}

// : asserts TypePredicate
// : asserts foo is type
const type = this.tsParseTypeAnnotation(/* eatColon */ false);
const node = this.startNodeAtNode(t);
node.parameterName = typePredicateVariable;
Expand Down

0 comments on commit e6bf63e

Please sign in to comment.