diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a8b827..7ea4b77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file. - default Docker ARG variables - add '$' as a trigger character - textDocument/hover - - HEALTHCHECK CMD flags ([#82](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/82)) + - HEALTHCHECK CMD flags ([#82](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/82), [#104](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/104)) - textDocument/publishDiagnostics - check the spelling of instruction flags ([#75](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/75)) - COPY's from diff --git a/src/dockerHover.ts b/src/dockerHover.ts index 29a2f80..57408c8 100644 --- a/src/dockerHover.ts +++ b/src/dockerHover.ts @@ -91,23 +91,54 @@ export class DockerHover { } } - if (instruction.getKeyword() === "HEALTHCHECK") { - let flags = (instruction as ModifiableInstruction).getFlags(); - for (let flag of flags) { - if (Util.isInsideRange(textDocumentPosition.position, flag.getNameRange())) { - switch (flag.getName()) { - case "interval": - return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval"); - case "retries": - return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries"); - case "start-period": - return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod"); - case "timeout": - return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout"); + switch (instruction.getKeyword()) { + case "HEALTHCHECK": + let flags = (instruction as ModifiableInstruction).getFlags(); + for (let flag of flags) { + if (Util.isInsideRange(textDocumentPosition.position, flag.getNameRange())) { + switch (flag.getName()) { + case "interval": + return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval"); + case "retries": + return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries"); + case "start-period": + return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod"); + case "timeout": + return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout"); + } + return null; } - return null; } - } + break; + case "ONBUILD": + if ((instruction as Onbuild).getTriggerInstruction() === "HEALTHCHECK") { + for (let arg of instruction.getArguments()) { + let value = arg.getValue().toUpperCase(); + if ((value === "CMD" || value === "NONE") && arg.isBefore(textDocumentPosition.position)) { + return null; + } + } + + for (let arg of instruction.getArguments()) { + if (Util.isInsideRange(textDocumentPosition.position, arg.getRange())) { + let value = arg.getValue(); + if (value.indexOf("--interval") === 0) { + return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval"); + } + if (value.indexOf("--retries") === 0) { + return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries"); + } + if (value.indexOf("--start-period") === 0) { + return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod"); + } + if (value.indexOf("--timeout") === 0) { + return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout"); + } + } + } + } + break; + } } diff --git a/test/dockerHover.test.ts b/test/dockerHover.test.ts index 0867cfe..346d73e 100644 --- a/test/dockerHover.test.ts +++ b/test/dockerHover.test.ts @@ -731,59 +731,70 @@ describe("Dockerfile hover", function() { }); }); - describe("HEALTHCHECK", function() { - it("--interval", function() { - let document = createDocument("HEALTHCHECK --interval"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); - }); + function createHealthcheckTest(trigger: boolean) { + let onbuild = trigger ? "ONBUILD " : ""; + let triggerOffset = onbuild.length; + + describe("HEALTHCHECK", function() { + it("--interval", function() { + let document = createDocument(onbuild + "HEALTHCHECK --interval"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); + }); - it("--interval=\\$x", function() { - let document = createDocument("HEALTHCHECK --interval=\\$x"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); - }); + it("--interval=\\$x", function() { + let document = createDocument(onbuild + "HEALTHCHECK --interval=\\$x"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); + }); - it("--interval=\\a", function() { - let document = createDocument("HEALTHCHECK --interval=\\a"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); - }); + it("--interval=\\a", function() { + let document = createDocument(onbuild + "HEALTHCHECK --interval=\\a"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval")); + }); - it("--retries", function() { - let document = createDocument("HEALTHCHECK --retries"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagRetries")); - }); + it("--retries", function() { + let document = createDocument(onbuild + "HEALTHCHECK --retries"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagRetries")); + }); - it("--start-period", function() { - let document = createDocument("HEALTHCHECK --start-period"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagStartPeriod")); - }); + it("--start-period", function() { + let document = createDocument(onbuild + "HEALTHCHECK --start-period"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagStartPeriod")); + }); - it("--timeout", function() { - let document = createDocument("HEALTHCHECK --timeout"); - let hover = onHover(document, 0, 17); - assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagTimeout")); + it("--timeout", function() { + let document = createDocument(onbuild + "HEALTHCHECK --timeout"); + let hover = onHover(document, 0, triggerOffset + 17); + assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagTimeout")); + }); + + function createFlagsAfterTest(subcommand: string) { + it("flags after " + subcommand, function() { + let document = createDocument(onbuild + "HEALTHCHECK " + subcommand + " \\\n--interval=30s\\\n--retries=3\\\n--start-period=30s\\\n--timeout=30s"); + let hover = onHover(document, 1, 4); + assert.equal(hover, null); + hover = onHover(document, 2, 4); + assert.equal(hover, null); + hover = onHover(document, 3, 4); + assert.equal(hover, null); + hover = onHover(document, 4, 4); + assert.equal(hover, null); + }); + } + + createFlagsAfterTest("CMD"); + createFlagsAfterTest("NONE"); }); + } + + createHealthcheckTest(false); - function createFlagsAfterTest(subcommand: string) { - it("flags after " + subcommand, function() { - let document = createDocument("HEALTHCHECK " + subcommand + " \\\n--interval=30s\\\n--retries=3\\\n--start-period=30s\\\n--timeout=30s"); - let hover = onHover(document, 1, 4); - assert.equal(hover, null); - hover = onHover(document, 2, 4); - assert.equal(hover, null); - hover = onHover(document, 3, 4); - assert.equal(hover, null); - hover = onHover(document, 4, 4); - assert.equal(hover, null); - }); - } - - createFlagsAfterTest("CMD"); - createFlagsAfterTest("NONE"); + describe("ONBUILD", function() { + createHealthcheckTest(true); }); });