From c2eeeff84de6192514d57567ea4c266b4f89a9d9 Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen Date: Sat, 2 Mar 2019 14:47:20 +0200 Subject: [PATCH] Fix corner cases in definition finding --- server/src/intellisense/definition-finder.ts | 19 ++++++++---- .../test/data/definition-finder.data.ts | 4 +++ .../test/definition-finder.test.ts | 30 +++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/server/src/intellisense/definition-finder.ts b/server/src/intellisense/definition-finder.ts index 8965965..cece9d8 100644 --- a/server/src/intellisense/definition-finder.ts +++ b/server/src/intellisense/definition-finder.ts @@ -165,11 +165,13 @@ export function findKeywordDefinition( workspaceTree ); - return { - node: definition.keyword, - uri: definition.file.uri, - range: nodeLocationToRange(definition.keyword), - }; + if (definition) { + return { + node: definition.keyword, + uri: definition.file.uri, + range: nodeLocationToRange(definition.keyword), + }; + } } return null; @@ -396,7 +398,12 @@ function tryFindKeywordDefinitionFromWorkspace( function tryGetKeywordNameFromBddDefinition( keywordName: Identifier ): Identifier | null { - const [, firstWord, rest] = /([^ ]+)(?: )(.*)/.exec(keywordName.name); + const matches = /([^ ]+)(?: )(.*)/.exec(keywordName.name); + if (!matches) { + return null; + } + + const [, firstWord, rest] = matches; if (gherkingIdentifiers.has(firstWord.toLowerCase())) { // Let's just use the same location even tho it's not 100% accurate return new Identifier(rest, keywordName.location); diff --git a/server/src/intellisense/test/data/definition-finder.data.ts b/server/src/intellisense/test/data/definition-finder.data.ts index 071823e..fd2d062 100644 --- a/server/src/intellisense/test/data/definition-finder.data.ts +++ b/server/src/intellisense/test/data/definition-finder.data.ts @@ -33,5 +33,9 @@ Test cases for findinging gherking style keyword definition Then Simple Keyword and Simple Keyword but Simple Keyword + +Test cases for not existing keywords + Non Existing Keyword + Given Non Existing Gherkin Keyword `, }; diff --git a/server/src/intellisense/test/definition-finder.test.ts b/server/src/intellisense/test/definition-finder.test.ts index f3fdfc1..dde7163 100644 --- a/server/src/intellisense/test/definition-finder.test.ts +++ b/server/src/intellisense/test/definition-finder.test.ts @@ -113,6 +113,36 @@ describe("Definition finder", () => { runTest(15, 2); }); }); + + it("non existing keyword", () => { + const actual = findDefinition( + { + filePath: "tests.robot", + position: { + line: 18, + column: 2, + }, + }, + workspace + ); + + assert.isNull(actual); + }); + + it("non existing gherkin keyword", () => { + const actual = findDefinition( + { + filePath: "tests.robot", + position: { + line: 19, + column: 2, + }, + }, + workspace + ); + + assert.isNull(actual); + }); }); }); });