From cb9f436b54d2899655db9f8fff89d692ebd0a9d0 Mon Sep 17 00:00:00 2001 From: Wenlu Wang Date: Thu, 29 Mar 2018 01:59:09 +0800 Subject: [PATCH] fix accessor parse with line terminator (#22893) --- src/compiler/parser.ts | 38 +++++++++---------- .../reference/accessorWithLineTerminator.js | 21 ++++++++++ .../accessorWithLineTerminator.symbols | 13 +++++++ .../accessorWithLineTerminator.types | 14 +++++++ .../compiler/accessorWithLineTerminator.ts | 9 +++++ 5 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 tests/baselines/reference/accessorWithLineTerminator.js create mode 100644 tests/baselines/reference/accessorWithLineTerminator.symbols create mode 100644 tests/baselines/reference/accessorWithLineTerminator.types create mode 100644 tests/cases/compiler/accessorWithLineTerminator.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c3709fa4e0c..e72ce3ad5ac 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1344,26 +1344,26 @@ namespace ts { } function nextTokenCanFollowModifier() { - if (token() === SyntaxKind.ConstKeyword) { - // 'const' is only a modifier if followed by 'enum'. - return nextToken() === SyntaxKind.EnumKeyword; + switch (token()) { + case SyntaxKind.ConstKeyword: + // 'const' is only a modifier if followed by 'enum'. + return nextToken() === SyntaxKind.EnumKeyword; + case SyntaxKind.ExportKeyword: + nextToken(); + if (token() === SyntaxKind.DefaultKeyword) { + return lookAhead(nextTokenCanFollowDefaultKeyword); + } + return token() !== SyntaxKind.AsteriskToken && token() !== SyntaxKind.AsKeyword && token() !== SyntaxKind.OpenBraceToken && canFollowModifier(); + case SyntaxKind.DefaultKeyword: + return nextTokenCanFollowDefaultKeyword(); + case SyntaxKind.StaticKeyword: + case SyntaxKind.GetKeyword: + case SyntaxKind.SetKeyword: + nextToken(); + return canFollowModifier(); + default: + return nextTokenIsOnSameLineAndCanFollowModifier(); } - if (token() === SyntaxKind.ExportKeyword) { - nextToken(); - if (token() === SyntaxKind.DefaultKeyword) { - return lookAhead(nextTokenCanFollowDefaultKeyword); - } - return token() !== SyntaxKind.AsteriskToken && token() !== SyntaxKind.AsKeyword && token() !== SyntaxKind.OpenBraceToken && canFollowModifier(); - } - if (token() === SyntaxKind.DefaultKeyword) { - return nextTokenCanFollowDefaultKeyword(); - } - if (token() === SyntaxKind.StaticKeyword) { - nextToken(); - return canFollowModifier(); - } - - return nextTokenIsOnSameLineAndCanFollowModifier(); } function parseAnyContextualModifier(): boolean { diff --git a/tests/baselines/reference/accessorWithLineTerminator.js b/tests/baselines/reference/accessorWithLineTerminator.js new file mode 100644 index 00000000000..51310c101a8 --- /dev/null +++ b/tests/baselines/reference/accessorWithLineTerminator.js @@ -0,0 +1,21 @@ +//// [accessorWithLineTerminator.ts] +class C { + get + x() { return 1 } + + set + x(v) { } +} + +//// [accessorWithLineTerminator.js] +var C = /** @class */ (function () { + function C() { + } + Object.defineProperty(C.prototype, "x", { + get: function () { return 1; }, + set: function (v) { }, + enumerable: true, + configurable: true + }); + return C; +}()); diff --git a/tests/baselines/reference/accessorWithLineTerminator.symbols b/tests/baselines/reference/accessorWithLineTerminator.symbols new file mode 100644 index 00000000000..c73192977f6 --- /dev/null +++ b/tests/baselines/reference/accessorWithLineTerminator.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/accessorWithLineTerminator.ts === +class C { +>C : Symbol(C, Decl(accessorWithLineTerminator.ts, 0, 0)) + + get + x() { return 1 } +>x : Symbol(C.x, Decl(accessorWithLineTerminator.ts, 0, 9), Decl(accessorWithLineTerminator.ts, 2, 20)) + + set + x(v) { } +>x : Symbol(C.x, Decl(accessorWithLineTerminator.ts, 0, 9), Decl(accessorWithLineTerminator.ts, 2, 20)) +>v : Symbol(v, Decl(accessorWithLineTerminator.ts, 5, 6)) +} diff --git a/tests/baselines/reference/accessorWithLineTerminator.types b/tests/baselines/reference/accessorWithLineTerminator.types new file mode 100644 index 00000000000..327ff8e4234 --- /dev/null +++ b/tests/baselines/reference/accessorWithLineTerminator.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/accessorWithLineTerminator.ts === +class C { +>C : C + + get + x() { return 1 } +>x : number +>1 : 1 + + set + x(v) { } +>x : number +>v : number +} diff --git a/tests/cases/compiler/accessorWithLineTerminator.ts b/tests/cases/compiler/accessorWithLineTerminator.ts new file mode 100644 index 00000000000..184a2e9660a --- /dev/null +++ b/tests/cases/compiler/accessorWithLineTerminator.ts @@ -0,0 +1,9 @@ +// @target: es5 + +class C { + get + x() { return 1 } + + set + x(v) { } +} \ No newline at end of file