Fix: remove constant context type check for TS (#9869)

This commit is contained in:
Tan Li Hau 2019-04-23 22:46:23 +08:00 committed by Henry Zhu
parent 338853b5d3
commit 4198d91b89
9 changed files with 0 additions and 66 deletions

View File

@ -331,36 +331,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return null; return null;
} }
tsCheckLiteralForConstantContext(node: N.Node) {
switch (node.type) {
case "StringLiteral":
case "TemplateLiteral":
case "NumericLiteral":
case "BooleanLiteral":
case "SpreadElement":
case "ObjectMethod":
case "ObjectExpression":
return;
case "ArrayExpression":
return (node: N.ArrayExpression).elements.forEach(element => {
if (element) {
this.tsCheckLiteralForConstantContext(element);
}
});
case "ObjectProperty":
return this.tsCheckLiteralForConstantContext(
(node: N.ObjectProperty).value,
);
case "UnaryExpression":
return this.tsCheckLiteralForConstantContext(node.argument);
default:
this.raise(
node.start,
"Only literal values are allowed in constant contexts",
);
}
}
// Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`, // Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`,
// but here it's always false, because this is only used for types. // but here it's always false, because this is only used for types.
tsFillSignature( tsFillSignature(
@ -1011,9 +981,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeAnnotation = _const || this.tsNextThenParseType(); node.typeAnnotation = _const || this.tsNextThenParseType();
this.expectRelational(">"); this.expectRelational(">");
node.expression = this.parseMaybeUnary(); node.expression = this.parseMaybeUnary();
if (_const) {
this.tsCheckLiteralForConstantContext(node.expression);
}
return this.finishNode(node, "TSTypeAssertion"); return this.finishNode(node, "TSTypeAssertion");
} }
@ -1679,7 +1646,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.expression = left; node.expression = left;
const _const = this.tsTryNextParseConstantContext(); const _const = this.tsTryNextParseConstantContext();
if (_const) { if (_const) {
this.tsCheckLiteralForConstantContext(node.expression);
node.typeAnnotation = _const; node.typeAnnotation = _const;
} else { } else {
node.typeAnnotation = this.tsNextThenParseType(); node.typeAnnotation = this.tsNextThenParseType();

View File

@ -1 +0,0 @@
let e = v as const; // Error

View File

@ -1,7 +0,0 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Only literal values are allowed in constant contexts (1:8)"
}

View File

@ -1 +0,0 @@
let e = (true ? 1 : 0) as const; // Error

View File

@ -1,7 +0,0 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Only literal values are allowed in constant contexts (1:9)"
}

View File

@ -1 +0,0 @@
let e = id(1) as const; // Error

View File

@ -1,7 +0,0 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Only literal values are allowed in constant contexts (1:8)"
}

View File

@ -1 +0,0 @@
let e = [v()] as const; // Error

View File

@ -1,7 +0,0 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Only literal values are allowed in constant contexts (1:9)"
}