Skip to content

Commit

Permalink
Fix #117 Prevent getHeredocs() from considering <<< as a heredoc
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <remy.suen@gmail.com>
  • Loading branch information
rcjsuen committed Nov 10, 2024
1 parent f0bdecf commit bc51eea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export class Instruction extends Line {
// instruction only on one line, if heredocs exist they would be incomplete
for (const arg of args) {
const value = arg.getValue();
if (value.startsWith("<<")) {
if (value.startsWith("<<") && Util.parseHeredocName(value) !== null) {
const startRange = arg.getRange();
const nameRange = this.getNameRange(startRange);
const name = this.getName(nameRange);
Expand Down
16 changes: 1 addition & 15 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,6 @@ export class Parser {
return this.buffer.length;
}

private parseHeredocName(value: string): string | null {
value = value.substring(2);
if (value.charAt(0) === '-') {
value = value.substring(1);
}
if (value.charAt(0) === '"' || value.charAt(0) === '\'') {
value = value.substring(1, value.length - 1);
}
if (value.charAt(0) === "<") {
return null;
}
return value;
}

private processHeredocs(instruction: Instruction, offset: number): number {
let keyword = instruction.getKeyword();
if (keyword === Keyword.ONBUILD) {
Expand All @@ -345,7 +331,7 @@ export class Parser {
for (const arg of instruction.getArguments()) {
const value = arg.getValue();
if (value.startsWith("<<") && value.length > 2) {
const name = this.parseHeredocName(value);
const name = Util.parseHeredocName(value);
if (name !== null) {
heredocs.push(name);
}
Expand Down
14 changes: 14 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,18 @@ export class Util {
}
return range.start.line < position.line && position.line < range.end.line;
}

public static parseHeredocName(value: string): string | null {
value = value.substring(2);
if (value.charAt(0) === '-') {
value = value.substring(1);
}
if (value.charAt(0) === '"' || value.charAt(0) === '\'') {
value = value.substring(1, value.length - 1);
}
if (value.charAt(0) === "<") {
return null;
}
return value;
}
}
10 changes: 10 additions & 0 deletions test/heredoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,16 @@ describe("Heredoc", () => {
assert.strictEqual(heredocs[0].getDelimiterRange(), null);
});

/**
* RUN <<<EOT
* <EOT
*/
it(`${keyword} <<<EOT\\n<EOT`, () => {
const instruction = DockerfileParser.parse(`${keyword} <<<EOT\n<EOT`).getInstructions()[0];
const heredocs = heredocsExtractor(instruction);
assert.strictEqual(heredocs.length, 0);
});

/**
* RUN <<\
* #EOT
Expand Down

0 comments on commit bc51eea

Please sign in to comment.