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 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 atLeast6;
public final boolean atLeast5; public final boolean atLeast5;
public final boolean isStrictMode; public final boolean isStrictMode;
Expand All @@ -215,7 +220,7 @@ public static enum Mode {
public final boolean warnES6NumberLiteral; public final boolean warnES6NumberLiteral;


public Config(Mode mode) { public Config(Mode mode) {
is6Typed = mode == Mode.ES6_TYPED; parseTypeSyntax = mode == Mode.ES6_TYPED;
atLeast6 = mode == Mode.ES6 || mode == Mode.ES6_STRICT atLeast6 = mode == Mode.ES6 || mode == Mode.ES6_STRICT
|| mode == Mode.ES6_TYPED; || mode == Mode.ES6_TYPED;
atLeast5 = atLeast6 || mode == Mode.ES5 || mode == Mode.ES5_STRICT; 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(); ImmutableList.Builder<ParseTree> interfaces = ImmutableList.builder();
if (peek(TokenType.IMPLEMENTS)) { if (config.parseTypeSyntax && peek(TokenType.IMPLEMENTS)) {
eat(TokenType.IMPLEMENTS); eat(TokenType.IMPLEMENTS);
ParseTree type = parseType(); ParseTree type = parseType();
interfaces.add(type); interfaces.add(type);
Expand Down Expand Up @@ -823,7 +828,7 @@ private ParseTree parseClassMemberDeclaration(
nameExpr = null; nameExpr = null;
name = eatIdOrKeywordAsId(); name = eatIdOrKeywordAsId();
} else { } else {
if (peekIndexSignature()) { if (config.parseTypeSyntax && peekIndexSignature()) {
ParseTree indexSignature = parseIndexSignature(); ParseTree indexSignature = parseIndexSignature();
eatPossibleImplicitSemiColon(); eatPossibleImplicitSemiColon();
return indexSignature; return indexSignature;
Expand Down Expand Up @@ -1067,7 +1072,7 @@ private boolean peekFunction(int index) {
} }


private boolean peekFunctionTypeExpression() { 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 // TODO(blickly): determine if we can parse this without the
// overhead of forking the parser. // overhead of forking the parser.
Parser p = createLookaheadParser(); Parser p = createLookaheadParser();
Expand Down Expand Up @@ -3666,11 +3671,8 @@ private boolean peekAccessibilityModifier() {
} }


private TokenType maybeParseAccessibilityModifier() { private TokenType maybeParseAccessibilityModifier() {
if (peekAccessibilityModifier()) { if (config.parseTypeSyntax && peekAccessibilityModifier()) {
features = features.require(FeatureSet.TYPESCRIPT); features = features.require(FeatureSet.TYPESCRIPT);
if (!config.is6Typed) {
reportError("Accessibility modifier is only supported in ES6 typed mode");
}
return nextToken().type; return nextToken().type;
} else { } else {
return null; 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() { public void testFunctionType_notEs6Typed() {
testNotEs6Typed("var n: (p1:string) => boolean;", "type annotation"); testNotEs6TypedFullError("var n: (p1:string) => boolean;", "Parse error. ')' expected");
testNotEs6Typed("var n: (p1?) => boolean;", "type annotation", "optional parameter"); testNotEs6TypedFullError("var n: (p1?) => boolean;", "Parse error. ')' expected");
} }


public void testInterface() { public void testInterface() {
Expand Down Expand Up @@ -533,7 +533,7 @@ public void testImplements() {
parse("class Foo implements Bar, Baz {\n}"); parse("class Foo implements Bar, Baz {\n}");
parse("class Foo extends Bar implements 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() { public void testTypeAlias() {
Expand Down Expand Up @@ -615,22 +615,22 @@ public void testAccessibilityModifier() {


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


public void testOptionalProperty() { 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'"); expectErrors("Parse error. Index signature parameter type must be 'string' or 'number'");
parse("interface I {\n [foo: any]: number;\n}"); parse("interface I {\n [foo: any]: number;\n}");


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


public void testCallSignature() { public void testCallSignature() {
Expand Down

0 comments on commit 6d01776

Please sign in to comment.