Skip to content

Commit

Permalink
Rename isEs6Typed to parseTypeSyntax & start using it to avoid execut…
Browse files Browse the repository at this point in the history
…ing code.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125123344
  • Loading branch information
brad4d authored and blickly committed Jun 20, 2016
1 parent f223039 commit 6d01776
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/com/google/javascript/jscomp/parsing/parser/Parser.java
Expand Up @@ -206,7 +206,12 @@ public static enum Mode {
ES8
}

public final boolean is6Typed;
/**
* Indicates that the parser should look for TypeScript-like data type syntax.
*/
// TODO(bradfordcsmith): Make sure all of the type syntax handling code is avoided when
// this is false.
public final boolean parseTypeSyntax;
public final boolean atLeast6;
public final boolean atLeast5;
public final boolean isStrictMode;
Expand All @@ -215,7 +220,7 @@ public static enum Mode {
public final boolean warnES6NumberLiteral;

public Config(Mode mode) {
is6Typed = mode == Mode.ES6_TYPED;
parseTypeSyntax = mode == Mode.ES6_TYPED;
atLeast6 = mode == Mode.ES6 || mode == Mode.ES6_STRICT
|| mode == Mode.ES6_TYPED;
atLeast5 = atLeast6 || mode == Mode.ES5 || mode == Mode.ES5_STRICT;
Expand Down Expand Up @@ -747,7 +752,7 @@ private ParseTree parseClass(boolean isExpression, boolean isAmbient) {
}

ImmutableList.Builder<ParseTree> interfaces = ImmutableList.builder();
if (peek(TokenType.IMPLEMENTS)) {
if (config.parseTypeSyntax && peek(TokenType.IMPLEMENTS)) {
eat(TokenType.IMPLEMENTS);
ParseTree type = parseType();
interfaces.add(type);
Expand Down Expand Up @@ -823,7 +828,7 @@ private ParseTree parseClassMemberDeclaration(
nameExpr = null;
name = eatIdOrKeywordAsId();
} else {
if (peekIndexSignature()) {
if (config.parseTypeSyntax && peekIndexSignature()) {
ParseTree indexSignature = parseIndexSignature();
eatPossibleImplicitSemiColon();
return indexSignature;
Expand Down Expand Up @@ -1067,7 +1072,7 @@ private boolean peekFunction(int index) {
}

private boolean peekFunctionTypeExpression() {
if (peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
if (config.parseTypeSyntax && peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
// TODO(blickly): determine if we can parse this without the
// overhead of forking the parser.
Parser p = createLookaheadParser();
Expand Down Expand Up @@ -3666,11 +3671,8 @@ private boolean peekAccessibilityModifier() {
}

private TokenType maybeParseAccessibilityModifier() {
if (peekAccessibilityModifier()) {
if (config.parseTypeSyntax && peekAccessibilityModifier()) {
features = features.require(FeatureSet.TYPESCRIPT);
if (!config.is6Typed) {
reportError("Accessibility modifier is only supported in ES6 typed mode");
}
return nextToken().type;
} else {
return null;
Expand Down
21 changes: 10 additions & 11 deletions test/com/google/javascript/jscomp/parsing/TypeSyntaxTest.java
Expand Up @@ -401,8 +401,8 @@ public void testFunctionType_missingParens() {
}

public void testFunctionType_notEs6Typed() {
testNotEs6Typed("var n: (p1:string) => boolean;", "type annotation");
testNotEs6Typed("var n: (p1?) => boolean;", "type annotation", "optional parameter");
testNotEs6TypedFullError("var n: (p1:string) => boolean;", "Parse error. ')' expected");
testNotEs6TypedFullError("var n: (p1?) => boolean;", "Parse error. ')' expected");
}

public void testInterface() {
Expand Down Expand Up @@ -533,7 +533,7 @@ public void testImplements() {
parse("class Foo implements Bar, Baz {\n}");
parse("class Foo extends Bar implements Baz {\n}");

testNotEs6Typed("class Foo implements Bar {\n}", "implements");
testNotEs6TypedFullError("class Foo implements Bar {\n}", "Parse error. '{' expected");
}

public void testTypeAlias() {
Expand Down Expand Up @@ -615,22 +615,22 @@ public void testAccessibilityModifier() {

testNotEs6TypedFullError(
"class Foo { private constructor() {} }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
testNotEs6TypedFullError(
"class Foo { protected bar; }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
testNotEs6TypedFullError(
"class Foo { protected bar() {} }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
testNotEs6TypedFullError(
"class Foo { private get() {} }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
testNotEs6TypedFullError(
"class Foo { private set() {} }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
testNotEs6TypedFullError(
"class Foo { private [Symbol.iterator]() {} }",
"Parse error. Accessibility modifier is only supported in ES6 typed mode");
"Parse error. Semi-colon expected");
}

public void testOptionalProperty() {
Expand Down Expand Up @@ -679,8 +679,7 @@ public void testIndexSignature() {
expectErrors("Parse error. Index signature parameter type must be 'string' or 'number'");
parse("interface I {\n [foo: any]: number;\n}");

testNotEs6Typed("class C {\n [foo: number]: number;\n}",
"index signature", "type annotation", "type annotation");
testNotEs6TypedFullError("class C {\n [foo: number]: number;\n}", "Parse error. ']' expected");
}

public void testCallSignature() {
Expand Down

0 comments on commit 6d01776

Please sign in to comment.