Skip to content

Commit

Permalink
asserts modifier as boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Oct 16, 2019
1 parent 637bdbc commit 128cd3d
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 153 deletions.
5 changes: 1 addition & 4 deletions packages/babel-generator/src/generators/typescript.js
Expand Up @@ -162,9 +162,6 @@ export function TSNullKeyword() {
export function TSNeverKeyword() {
this.word("never");
}
export function TSAssertsKeyword() {
this.word("asserts");
}

export function TSThisType() {
this.word("this");
Expand Down Expand Up @@ -201,7 +198,7 @@ export function TSTypeReference(node) {

export function TSTypePredicate(node) {
if (node.assertsModifier) {
this.print(node.assertsModifier);
this.word("asserts");
this.space();
}
this.print(node.parameterName);
Expand Down
20 changes: 6 additions & 14 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -919,7 +919,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

// : asserts foo
const node = this.startNodeAtNode(assertsModifier);
const node = this.startNodeAtNode(t);
node.parameterName = this.parseIdentifier();
node.assertsModifier = assertsModifier;
t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
Expand All @@ -928,9 +928,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

// : foo is type
const type = this.tsParseTypeAnnotation(/* eatColon */ false);
const node = this.startNodeAtNode(
assertsModifier ? assertsModifier : typePredicateVariable,
);
const node = this.startNodeAtNode(t);
node.parameterName = typePredicateVariable;
node.typeAnnotation = type;
node.assertsModifier = assertsModifier;
Expand Down Expand Up @@ -961,9 +959,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

tsParseTypePredicateAssertsModifier(): ?N.TsKeywordType {
tsParseTypePredicateAssertsModifier(): boolean {
if (!this.tsIsIdentifier()) {
return;
return false;
}

const id = this.parseIdentifier();
Expand All @@ -972,16 +970,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.hasPrecedingLineBreak() ||
!this.tsIsIdentifier()
) {
return;
return false;
}

const assertsKeyword = this.startNodeAtNode(id);
return this.finishNodeAt(
assertsKeyword,
"TSAssertsKeyword",
id.end,
id.loc.end,
);
return true;
}

tsParseTypeAnnotation(
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-parser/src/types.js
Expand Up @@ -1186,8 +1186,7 @@ export type TsKeywordTypeType =
| "TSVoidKeyword"
| "TSUndefinedKeyword"
| "TSNullKeyword"
| "TSNeverKeyword"
| "TSAssertsKeyword";
| "TSNeverKeyword";
export type TsKeywordType = TsTypeBase & {
type: TsKeywordTypeType,
};
Expand Down
Expand Up @@ -73,12 +73,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 10,
"start": 8,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 10
"column": 8
},
"end": {
"line": 1,
Expand Down
Expand Up @@ -105,12 +105,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 34,
"start": 32,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 34
"column": 32
},
"end": {
"line": 1,
Expand Down Expand Up @@ -164,21 +164,7 @@
}
}
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 34,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 34
},
"end": {
"line": 1,
"column": 41
}
}
}
"assertsModifier": true
}
},
"id": null,
Expand Down Expand Up @@ -331,12 +317,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 98,
"start": 96,
"end": 111,
"loc": {
"start": {
"line": 2,
"column": 34
"column": 32
},
"end": {
"line": 2,
Expand All @@ -360,21 +346,7 @@
},
"name": "value"
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 98,
"end": 105,
"loc": {
"start": {
"line": 2,
"column": 34
},
"end": {
"line": 2,
"column": 41
}
}
}
"assertsModifier": true
}
},
"id": null,
Expand Down
Expand Up @@ -127,12 +127,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 49,
"start": 47,
"end": 62,
"loc": {
"start": {
"line": 1,
"column": 49
"column": 47
},
"end": {
"line": 1,
Expand All @@ -156,21 +156,7 @@
},
"name": "value"
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 49,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 49
},
"end": {
"line": 1,
"column": 56
}
}
}
"assertsModifier": true
}
},
"declare": true
Expand Down
Expand Up @@ -127,12 +127,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 49,
"start": 47,
"end": 72,
"loc": {
"start": {
"line": 1,
"column": 49
"column": 47
},
"end": {
"line": 1,
Expand Down Expand Up @@ -186,21 +186,7 @@
}
}
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 49,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 49
},
"end": {
"line": 1,
"column": 56
}
}
}
"assertsModifier": true
}
},
"declare": true
Expand Down
Expand Up @@ -127,12 +127,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 36,
"start": 34,
"end": 59,
"loc": {
"start": {
"line": 1,
"column": 36
"column": 34
},
"end": {
"line": 1,
Expand Down Expand Up @@ -186,21 +186,7 @@
}
}
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 36,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 36
},
"end": {
"line": 1,
"column": 43
}
}
}
"assertsModifier": true
}
},
"body": {
Expand Down Expand Up @@ -319,12 +305,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 100,
"start": 98,
"end": 113,
"loc": {
"start": {
"line": 2,
"column": 36
"column": 34
},
"end": {
"line": 2,
Expand All @@ -348,21 +334,7 @@
},
"name": "value"
},
"assertsModifier": {
"type": "TSAssertsKeyword",
"start": 100,
"end": 107,
"loc": {
"start": {
"line": 2,
"column": 36
},
"end": {
"line": 2,
"column": 43
}
}
}
"assertsModifier": true
}
},
"body": {
Expand Down
Expand Up @@ -127,12 +127,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 20,
"start": 18,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 20
"column": 18
},
"end": {
"line": 1,
Expand Down Expand Up @@ -302,12 +302,12 @@
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 55,
"start": 53,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 19
"column": 17
},
"end": {
"line": 2,
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-types/src/asserts/generated/index.js
Expand Up @@ -836,9 +836,6 @@ export function assertTSUnknownKeyword(node: Object, opts?: Object = {}): void {
export function assertTSVoidKeyword(node: Object, opts?: Object = {}): void {
assert("TSVoidKeyword", node, opts);
}
export function assertTSAssertsKeyword(node: Object, opts?: Object = {}): void {
assert("TSAssertsKeyword", node, opts);
}
export function assertTSThisType(node: Object, opts?: Object = {}): void {
assert("TSThisType", node, opts);
}
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-types/src/builders/generated/index.js
Expand Up @@ -781,11 +781,6 @@ export function TSVoidKeyword(...args: Array<any>): Object {
}
export { TSVoidKeyword as tsVoidKeyword };
export { TSVoidKeyword as tSVoidKeyword };
export function TSAssertsKeyword(...args: Array<any>): Object {
return builder("TSAssertsKeyword", ...args);
}
export { TSAssertsKeyword as tsAssertsKeyword };
export { TSAssertsKeyword as tSAssertsKeyword };
export function TSThisType(...args: Array<any>): Object {
return builder("TSThisType", ...args);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-types/src/definitions/typescript.js
Expand Up @@ -139,7 +139,6 @@ const tsKeywordTypes = [
"TSUndefinedKeyword",
"TSUnknownKeyword",
"TSVoidKeyword",
"TSAssertsKeyword",
];

for (const type of tsKeywordTypes) {
Expand Down Expand Up @@ -178,9 +177,9 @@ defineType("TSTypePredicate", {
aliases: ["TSType"],
visitor: ["parameterName", "typeAnnotation", "assertsModifier"],
fields: {
assertsModifier: validateOptionalType("TSAssertsKeyword"),
parameterName: validateType(["Identifier", "TSThisType"]),
typeAnnotation: validateOptionalType("TSTypeAnnotation"),
assertsModifier: validate(bool),
},
});

Expand Down

0 comments on commit 128cd3d

Please sign in to comment.