Skip to content

Commit

Permalink
fix: Do not allow TypeCastExpressions w/o parens SequenceExpressions
Browse files Browse the repository at this point in the history
This enables an already existing check again which was skipped because of some other changes.
Also adds another check for SequenceExpressions.
  • Loading branch information
danez committed Nov 2, 2018
1 parent 0d9e77f commit b3f3918
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 35 deletions.
14 changes: 11 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -804,7 +804,7 @@ export default class ExpressionParser extends LValParser {
this.state.yieldInPossibleArrowParameters = null;
const params = [this.parseIdentifier()];
this.expect(tt.arrow);
// let foo = bar => {};
// let foo = async bar => {};
this.parseArrowExpression(node, params, true);
this.state.yieldInPossibleArrowParameters = oldYield;
return node;
Expand Down Expand Up @@ -1165,6 +1165,14 @@ export default class ExpressionParser extends LValParser {
}
}

parseExpressionParenItem(
node: N.Expression,
startPos: number, // eslint-disable-line no-unused-vars
startLoc: Position, // eslint-disable-line no-unused-vars
): N.Expression {
return node;
}

parseParenItem(
node: N.Expression,
startPos: number, // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -1812,7 +1820,7 @@ export default class ExpressionParser extends LValParser {
} else if (this.match(tt.ellipsis)) {
const spreadNodeStartPos = this.state.start;
const spreadNodeStartLoc = this.state.startLoc;
elt = this.parseParenItem(
elt = this.parseExpressionParenItem(
this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos),
spreadNodeStartPos,
spreadNodeStartLoc,
Expand All @@ -1825,7 +1833,7 @@ export default class ExpressionParser extends LValParser {
elt = this.parseMaybeAssign(
false,
refShorthandDefaultPos,
this.parseParenItem,
this.parseExpressionParenItem,
refNeedsArrowPos,
);
}
Expand Down
81 changes: 54 additions & 27 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -1756,12 +1756,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return result;
}

parseParenItem(
flowTryParseOptionalTypeCastExpression(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
node = super.parseParenItem(node, startPos, startLoc);
if (this.eat(tt.question)) {
node.optional = true;
}
Expand All @@ -1777,6 +1776,36 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node;
}

parseExpressionParenItem(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
node = this.flowTryParseOptionalTypeCastExpression(
super.parseParenItem(node, startPos, startLoc),
startPos,
startLoc,
);

if (node.type === "TypeCastExpression") {
node._exprListItem = true;
}

return node;
}

parseParenItem(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
return this.flowTryParseOptionalTypeCastExpression(
super.parseParenItem(node, startPos, startLoc),
startPos,
startLoc,
);
}

assertModuleNodeAllowed(node: N.Node) {
if (
(node.type === "ImportDeclaration" &&
Expand Down Expand Up @@ -1932,36 +1961,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
for (let i = 0; i < exprList.length; i++) {
const expr = exprList[i];
if (expr && expr._exprListItem && expr.type === "TypeCastExpression") {
this.raise(expr.start, "Unexpected type cast");
this.raise(
expr.start,
"The type cast expression is expected to be wrapped with parenthesis",
);
}
}

return exprList;
}

// parse an item inside a expression list eg. `(NODE, NODE)` where NODE represents
// the position where this function is called
parseExprListItem(
allowEmpty: ?boolean,
refShorthandDefaultPos: ?Pos,
refNeedsArrowPos: ?Pos,
): ?N.Expression {
const container = this.startNode();
const node = super.parseExprListItem(
allowEmpty,
refShorthandDefaultPos,
refNeedsArrowPos,
);
if (this.match(tt.colon)) {
container._exprListItem = true;
container.expression = node;
container.typeAnnotation = this.flowParseTypeAnnotation();
return this.finishNode(container, "TypeCastExpression");
} else {
return node;
}
}

checkLVal(
expr: N.Expression,
isBinding: ?boolean,
Expand Down Expand Up @@ -2494,9 +2503,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression {
return super.parseParenAndDistinguishExpression(
const list = super.parseParenAndDistinguishExpression(
canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1,
);

// Ensure that TypeCastExpressions without parens are not allowed inside SequenceExpressions
// e.g. (A, B: T)
if (list.type === "SequenceExpression") {
const firstTypeCast = list.expressions.find(
({ type, extra }) =>
type === "TypeCastExpression" &&
(!extra || extra.parenthesized !== true),
);
if (firstTypeCast) {
this.unexpected(
firstTypeCast.typeAnnotation.start,
"The type cast expression is expected to be wrapped with parenthesis",
);
}
}

return list;
}

parseSubscripts(
Expand Down
33 changes: 29 additions & 4 deletions packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -1716,14 +1716,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

// Note: These "type casts" are *not* valid TS expressions.
// But we parse them here and change them when completing the arrow function.
parseParenItem(
tsTryParseParamType(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
node = super.parseParenItem(node, startPos, startLoc);
if (this.eat(tt.question)) {
node.optional = true;
}
Expand All @@ -1742,6 +1739,34 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node;
}

// Note: These "type casts" are *not* valid TS expressions.
// But we parse them here and change them when completing the arrow function.
parseExpressionParenItem(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
return this.tsTryParseParamType(
super.parseParenItem(node, startPos, startLoc),
startPos,
startLoc,
);
}

// Note: These "type casts" are *not* valid TS expressions.
// But we parse them here and change them when completing the arrow function.
parseParenItem(
node: N.Expression,
startPos: number,
startLoc: Position,
): N.Expression {
return this.tsTryParseParamType(
super.parseParenItem(node, startPos, startLoc),
startPos,
startLoc,
);
}

parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration {
// "export declare" is equivalent to just "export".
const isDeclare = this.eatContextual("declare");
Expand Down
@@ -0,0 +1 @@
funccall(a, b: string);
@@ -0,0 +1,3 @@
{
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:12)"
}
@@ -0,0 +1 @@
(A, B: T)
@@ -0,0 +1,3 @@
{
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:5)"
}
1 change: 0 additions & 1 deletion scripts/tests/flow/flow_tests_whitelist.txt
Expand Up @@ -31,7 +31,6 @@ types/annotations_in_comments_invalid/migrated_0003.js
types/annotations/void_is_reserved_param.js
types/member/reserved_words.js
types/parameter_defaults/migrated_0032.js
types/typecasts_invalid/migrated_0001.js
class_method_kinds/polymorphic_getter.js
numbers/underscored_bin.js
numbers/underscored_float.js
Expand Down

0 comments on commit b3f3918

Please sign in to comment.