From 5b89c3482f07fed64d594a3a5f90e725b89e8f93 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 1 Dec 2017 17:42:56 +0100 Subject: [PATCH] one-line: avoid crash on parse error [bugfix] `one-line` fixed crash on syntax error on class and interface Fixes: #3537 --- src/rules/oneLineRule.ts | 5 ++++- test/rules/one-line/all/test.ts.fix | 13 +++++++++++++ test/rules/one-line/all/test.ts.lint | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/rules/oneLineRule.ts b/src/rules/oneLineRule.ts index 7c311c35f14..818b40bb0a9 100644 --- a/src/rules/oneLineRule.ts +++ b/src/rules/oneLineRule.ts @@ -97,7 +97,10 @@ class OneLineWalker extends Lint.AbstractWalker { case ts.SyntaxKind.InterfaceDeclaration: case ts.SyntaxKind.ClassDeclaration: case ts.SyntaxKind.ClassExpression: { - this.check(getChildOfKind(node, ts.SyntaxKind.OpenBraceToken, sourceFile)!); + const openBrace = getChildOfKind(node, ts.SyntaxKind.OpenBraceToken, sourceFile); + if (openBrace !== undefined) { + this.check(openBrace); + } break; } case ts.SyntaxKind.IfStatement: { diff --git a/test/rules/one-line/all/test.ts.fix b/test/rules/one-line/all/test.ts.fix index ec67c2b0cbd..1a9d1bda0c7 100644 --- a/test/rules/one-line/all/test.ts.fix +++ b/test/rules/one-line/all/test.ts.fix @@ -136,3 +136,16 @@ if (true) { } else { false; } + +// don't crash on parse error +interface InvalidInterface = { + foo: string, +} + +class InvalidClass = { + foo: string, +} + +foo = class InvalidClassExpression = { + foo: string, +} diff --git a/test/rules/one-line/all/test.ts.lint b/test/rules/one-line/all/test.ts.lint index 037fd008c79..12223e901fa 100644 --- a/test/rules/one-line/all/test.ts.lint +++ b/test/rules/one-line/all/test.ts.lint @@ -179,3 +179,16 @@ if (true) { ~~~~ [missing whitespace] false; } + +// don't crash on parse error +interface InvalidInterface = { + foo: string, +} + +class InvalidClass = { + foo: string, +} + +foo = class InvalidClassExpression = { + foo: string, +}