Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public computeProposals(document: TextDocument, position: Position): CompletionItem[] | PromiseLike {
let buffer = document.getText();
let offset = document.offsetAt(position);
let dockerfile = DockerfileParser.parse(buffer);
let escapeCharacter = dockerfile.getEscapeCharacter();
let directive = dockerfile.getDirective();
if (directive !== null && position.line === 0) {
let range = directive.getNameRange();
if (position.character <= range.start.character) {
// in whitespace before the directive's name
return [this.createEscape(0, offset, Directive.escape)];
} else if (position.character <= range.end.character) {
// in the name
return [this.createEscape(position.character - range.start.character, offset, Directive.escape)];
}
return [];
}
// directive only possible on the first line
let comments = dockerfile.getComments();
if (comments.length !== 0) {
if (position.line === 0) {
let commentRange = comments[0].getRange();
// check if the first comment is on the first line
if (commentRange.start.line === 0) {
// is the user inside the comment
if (commentRange.start.character < position.character) {
let range = comments[0].getContentRange();
if (range === null || position.character <= range.start.character) {
// in whitespace
parseDirective(dockerfile: Dockerfile, problems: Diagnostic[]) {
let directive = dockerfile.getDirective();
if (directive === null) {
return;
}
let directiveName = directive.getDirective();
let value = directive.getValue();
if (directiveName === Directive.escape) {
if (value !== '\\' && value !== '`' && value !== "") {
// if the directive's value is invalid or isn't the empty string, flag it
let range = directive.getValueRange();
problems.push(Validator.createInvalidEscapeDirective(range.start, range.end, value));
}
if (directive.getName() !== Directive.escape) {
let range = directive.getNameRange();
let diagnostic = this.createLowercaseDirective(range.start, range.end);
if (diagnostic) {
problems.push(diagnostic);
}
}
}
}
onHover(document: TextDocument, textDocumentPosition: TextDocumentPositionParams): Hover | null {
let dockerfile = DockerfileParser.parse(document.getText());
let directive = dockerfile.getDirective();
let image = dockerfile.getContainingImage(textDocumentPosition.position);
if (textDocumentPosition.position.line === 0 && directive !== null && directive.getDirective() === Directive.escape) {
let range = directive.getNameRange();
if (Util.isInsideRange(textDocumentPosition.position, range)) {
return this.markdown.getMarkdown(Directive.escape);
}
}
for (let instruction of image.getInstructions()) {
for (let variable of instruction.getVariables()) {
// are we hovering over a variable
if (Util.isInsideRange(textDocumentPosition.position, variable.getNameRange())) {
let resolved = image.resolveVariable(variable.getName(), variable.getNameRange().start.line);
if (resolved || resolved === "") {
return { contents: resolved };
} else if (resolved === null) {
return null;
}
}
}
}
parseDirective(dockerfile: Dockerfile, problems: Diagnostic[]) {
let directive = dockerfile.getDirective();
if (directive === null) {
return;
}
let directiveName = directive.getDirective();
let value = directive.getValue();
if (directiveName === Directive.escape) {
if (value !== '\\' && value !== '`' && value !== "") {
// if the directive's value is invalid or isn't the empty string, flag it
let range = directive.getValueRange();
problems.push(Validator.createInvalidEscapeDirective(range.start, range.end, value));
}
if (directive.getName() !== Directive.escape) {
let range = directive.getNameRange();
let diagnostic = this.createLowercaseDirective(range.start, range.end);
if (diagnostic) {
problems.push(diagnostic);
}
}
}
}
// check if the first comment is on the first line
if (commentRange.start.line === 0) {
// is the user inside the comment
if (commentRange.start.character < position.character) {
let range = comments[0].getContentRange();
if (range === null || position.character <= range.start.character) {
// in whitespace
return [this.createEscape(0, offset, Directive.escape)];
}
let comment = comments[0].getContent();
if (position.character <= range.end.character) {
// within the content
let prefix = comment.substring(0, position.character - range.start.character);
// substring check
if (Directive.escape.indexOf(prefix.toLowerCase()) === 0) {
return [this.createEscape(prefix.length, offset, Directive.escape)];
}
}
return [];
}
}
} else {
for (let comment of comments) {
let range = comment.getRange();
if (range.start.line === position.line) {
if (range.start.character < position.character && position.character <= range.end.character) {
// inside a comment
return [];
}
}
}
}
onHover(document: TextDocument, textDocumentPosition: TextDocumentPositionParams): Hover | null {
let dockerfile = DockerfileParser.parse(document.getText());
let directive = dockerfile.getDirective();
let image = dockerfile.getContainingImage(textDocumentPosition.position);
if (textDocumentPosition.position.line === 0 && directive !== null && directive.getDirective() === Directive.escape) {
let range = directive.getNameRange();
if (Util.isInsideRange(textDocumentPosition.position, range)) {
return this.markdown.getMarkdown(Directive.escape);
}
}
for (let instruction of image.getInstructions()) {
for (let variable of instruction.getVariables()) {
// are we hovering over a variable
if (Util.isInsideRange(textDocumentPosition.position, variable.getNameRange())) {
let resolved = image.resolveVariable(variable.getName(), variable.getNameRange().start.line);
if (resolved || resolved === "") {
return { contents: resolved };
} else if (resolved === null) {
return null;
}
createEscape(prefixLength: number, offset: number, markdown: string): CompletionItem {
return this.createKeywordCompletionItem(Directive.escape, "escape=`", prefixLength, offset, "escape=${1:`}", markdown);
}
public computeSignatures(document: TextDocument, position: Position): SignatureHelp {
let dockerfile = DockerfileParser.parse(document.getText());
if (position.line === 0) {
let directive = dockerfile.getDirective();
if (directive !== null && directive.getDirective() === Directive.escape) {
return {
signatures: [
{
label: "escape=`\\`",
documentation: this.documentation.getDocumentation("signatureEscape"),
parameters: [
{
label: "\\",
documentation: this.documentation.getDocumentation("signatureEscape_Param")
}
]
}
],
activeSignature: 0,
activeParameter: 0
}