How to use the dockerfile-ast.Directive.escape function in dockerfile-ast

To help you get started, we’ve selected a few dockerfile-ast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rcjsuen / dockerfile-language-server-nodejs / src / dockerAssist.ts View on Github external
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
github rcjsuen / dockerfile-language-server-nodejs / src / dockerValidator.ts View on Github external
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);
				}
			}
		}
	}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerHover.ts View on Github external
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;
					}
				}
			}
		}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerValidator.ts View on Github external
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);
				}
			}
		}
	}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerAssist.ts View on Github external
// 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 [];
						}
					}
				}
			}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerHover.ts View on Github external
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;
					}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerAssist.ts View on Github external
createEscape(prefixLength: number, offset: number, markdown: string): CompletionItem {
		return this.createKeywordCompletionItem(Directive.escape, "escape=`", prefixLength, offset, "escape=${1:`}", markdown);
	}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerSignatures.ts View on Github external
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
				}

dockerfile-ast

Parse a Dockerfile into an array of instructions and comments.

MIT
Latest version published 1 year ago

Package Health Score

51 / 100
Full package analysis