How to use dockerfile-ast - 10 common examples

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 / dockerFormatter.ts View on Github external
public formatOnType(document: TextDocument, position: Position, ch: string, options: FormattingOptions): TextEdit[] {
		const dockerfile = DockerfileParser.parse(document.getText());
		// check that the inserted character is the escape character
		if (dockerfile.getEscapeCharacter() === ch) {
			for (let comment of dockerfile.getComments()) {
				// ignore if we're in a comment
				if (comment.getRange().start.line === position.line) {
					return [];
				}
			}

			const directive = dockerfile.getDirective();
			// ignore if we're in the parser directive
			if (directive && position.line === 0) {
				return [];
			}

			const content = document.getText();
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
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 === "") {
github rcjsuen / dockerfile-language-server-nodejs / src / dockerValidator.ts View on Github external
validate(document: TextDocument): Diagnostic[] {
		this.document = document;
		let problems: Diagnostic[] = [];
		let dockerfile = DockerfileParser.parse(document.getText());
		this.parseDirective(dockerfile, problems);
		let instructions = dockerfile.getInstructions();
		if (instructions.length === 0 || dockerfile.getARGs().length === instructions.length) {
			// no instructions in this file, or only ARGs
			problems.push(Validator.createNoSourceImage(document.positionAt(0), document.positionAt(0)));
		}

		let cmds = dockerfile.getCMDs();
		if (cmds.length > 1) {
			// more than one CMD found, warn the user
			for (let cmd of cmds) {
				let diagnostic = this.createMultipleInstructions(cmd.getInstructionRange(), this.settings.instructionCmdMultiple, "CMD");
				if (diagnostic) {
					problems.push(diagnostic);
				}
			}
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;
					}

dockerfile-ast

Parse a Dockerfile into an array of instructions and comments.

MIT
Latest version published 1 month ago

Package Health Score

65 / 100
Full package analysis