Skip to content

Commit

Permalink
Fix #74 Improve handling of embedded comments in semantic tokens
Browse files Browse the repository at this point in the history
The algorithm will no longer loop infinitely when encountering embedded
comments in ARG or ENV instructions.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Apr 10, 2021
1 parent ce25ab8 commit 29d560a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.

### Fixed
- do not validate variable substitutions if found in CMD and ENTRYPOINT ([rcjsuen/dockerfile-utils#89](https://github.com/rcjsuen/dockerfile-utils/issues/89))
- fix infinite loop issue when calculating semantic tokens for ARG or ENV instructions with nested comments ([#74](https://github.com/rcjsuen/dockerfile-language-service/issues/74))

## [0.2.0] - 2021-01-20
### Added
Expand Down
18 changes: 17 additions & 1 deletion src/dockerSemanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ export class DockerSemanticTokens {
switch (ch) {
case '#':
if (escaping) {
let commenting = true;
commentCheck: for (let j = i + 1; j < endOffset; j++) {
switch (this.content.charAt(j)) {
case ' ':
Expand All @@ -439,7 +440,9 @@ export class DockerSemanticTokens {
this.createToken(null, crComment, SemanticTokenTypes.comment, [], false);
i = j + 1;
startOffset = -1;
break commentCheck;
commenting = false;
j++;
break;
case '\n':
const lfComment = {
start: this.document.positionAt(i),
Expand All @@ -448,6 +451,19 @@ export class DockerSemanticTokens {
this.createToken(null, lfComment, SemanticTokenTypes.comment, [], false);
i = j;
startOffset = -1;
commenting = false;
break;
case '#':
if (!commenting) {
i = j;
}
commenting = true;
break;
default:
if (commenting) {
break;
}
i = j - 1;
break commentCheck;
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/dockerSemanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ describe("Dockerfile Semantic Token tests", () => {
assertEdit(tokens.data, SemanticTokenTypes.parameter, 20, 0, 7, 23);
assertEdit(tokens.data, SemanticTokenTypes.variable, 25, 0, 23, 5);
});

it(keyword + " a \\\\n b \\\\n # comment\\n # comment\\n c", () => {
const tokens = computeSemanticTokens(keyword + " a \\\n b \\\n # comment\n # comment\n c");
assert.equal(40, tokens.data.length);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, keyword.length);
assertEdit(tokens.data, SemanticTokenTypes.variable, 5, 0, keyword.length + 1, 1, [SemanticTokenModifiers.declaration]);
assertEdit(tokens.data, SemanticTokenTypes.macro, 10, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 1, 1, 2);
assertEdit(tokens.data, SemanticTokenTypes.macro, 20, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 25, 1, 1, 9);
assertEdit(tokens.data, SemanticTokenTypes.comment, 30, 1, 1, 9);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 35, 1, 1, 1);
});
});
}
createVariableDeclarationTests("ARG");
Expand Down

0 comments on commit 29d560a

Please sign in to comment.