Skip to content

Commit

Permalink
Fix #114 Prevent 0-length semantic tokens from being added
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Jun 1, 2023
1 parent 6195c0f commit c480cee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
### Fixed
- ignore quotes in comments if it's within an embedded argument with CRLF used as a newline delimiter ([#113](https://github.com/rcjsuen/dockerfile-language-service/issues/113))
- prevent 0-length semantic tokens from being added ([#114](https://github.com/rcjsuen/dockerfile-language-service/issues/114))

## [0.10.1] - 2023-05-31
### Fixed
Expand Down
10 changes: 6 additions & 4 deletions src/dockerSemanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,13 @@ export class DockerSemanticTokens {
case '\n':
if (!added) {
if (!intermediateAdded && startOffset !== -1) {
const intermediateRange = {
start: this.document.positionAt(startOffset),
end: this.document.positionAt(i),
if (i !== startOffset) {
const intermediateRange = {
start: this.document.positionAt(startOffset),
end: this.document.positionAt(i),
}
this.createToken(instruction, intermediateRange, tokenType, tokenModifiers);
}
this.createToken(instruction, intermediateRange, tokenType, tokenModifiers);
intermediateAdded = true;
}
this.createEscapeToken(instruction, i);
Expand Down
54 changes: 54 additions & 0 deletions test/dockerSemanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,60 @@ describe("Dockerfile Semantic Token tests", () => {
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 0, 7, 1);
assertEdit(tokens.data, SemanticTokenTypes.macro, 20, 0, 1, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 25, 1, 0, 5);

content = `RUN echo "test" a''\\\n# a'b\necho "test2"`;
tokens = computeSemanticTokens(content);
assert.strictEqual(tokens.data.length, 45);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 5, 0, 4, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 10, 0, 5, 6);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 0, 7, 1);
assertEdit(tokens.data, SemanticTokenTypes.string, 20, 0, 1, 2);
assertEdit(tokens.data, SemanticTokenTypes.macro, 25, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 30, 1, 0, 5);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 35, 1, 0, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 40, 0, 5, 7);

content = `RUN echo "test" a''\\\r\n# a'b\r\necho "test2"`;
tokens = computeSemanticTokens(content);
assert.strictEqual(tokens.data.length, 45);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 5, 0, 4, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 10, 0, 5, 6);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 0, 7, 1);
assertEdit(tokens.data, SemanticTokenTypes.string, 20, 0, 1, 2);
assertEdit(tokens.data, SemanticTokenTypes.macro, 25, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 30, 1, 0, 5);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 35, 1, 0, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 40, 0, 5, 7);

content = `RUN echo "test" a''b\\\n# a'b\necho "test2"`;
tokens = computeSemanticTokens(content);
assert.strictEqual(tokens.data.length, 50);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 5, 0, 4, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 10, 0, 5, 6);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 0, 7, 1);
assertEdit(tokens.data, SemanticTokenTypes.string, 20, 0, 1, 2);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 25, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.macro, 30, 0, 1, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 35, 1, 0, 5);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 40, 1, 0, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 45, 0, 5, 7);

content = `RUN echo "test" a''b\\\r\n# a'b\r\necho "test2"`;
tokens = computeSemanticTokens(content);
assert.strictEqual(tokens.data.length, 50);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 5, 0, 4, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 10, 0, 5, 6);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 0, 7, 1);
assertEdit(tokens.data, SemanticTokenTypes.string, 20, 0, 1, 2);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 25, 0, 2, 1);
assertEdit(tokens.data, SemanticTokenTypes.macro, 30, 0, 1, 1);
assertEdit(tokens.data, SemanticTokenTypes.comment, 35, 1, 0, 5);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 40, 1, 0, 4);
assertEdit(tokens.data, SemanticTokenTypes.string, 45, 0, 5, 7);
});
});
});
Expand Down

0 comments on commit c480cee

Please sign in to comment.