Repository navigation
feat: add dockerfile language #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+986
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59cad05
feat: add dockerfile language
nzakas a7abb90
fix: typecheck tests in CI
nzakas c0e0cf6
refactor: rename DockerLanguage and DockerSourceCode
nzakas 66fa05d
refactor: rename DockerLanguageOptions and DockerSourceCodeOptions
nzakas 78084af
fix: ignore unknown language options
nzakas 534b1af
refactor: drop the duplicate comments from the parse result
nzakas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @fileoverview Tests for the DockerFileLanguage class. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import { DockerFileLanguage } from "./docker-language.js"; | ||
| import { DockerFileSourceCode } from "./docker-source-code.js"; | ||
| import type { OkParseResult } from "@eslint/core"; | ||
| import type { DockerfileNode } from "../types.js"; | ||
|
|
||
| const language = new DockerFileLanguage(); | ||
|
|
||
| describe("DockerFileLanguage", () => { | ||
| it("should describe itself as a 1-based text language", () => { | ||
| expect(language.fileType).toBe("text"); | ||
| expect(language.lineStart).toBe(1); | ||
| expect(language.columnStart).toBe(1); | ||
| expect(language.nodeTypeKey).toBe("type"); | ||
| expect(language.visitorKeys).toBeTruthy(); | ||
| }); | ||
|
|
||
| describe("validateLanguageOptions", () => { | ||
| it("should accept an empty object", () => { | ||
| expect(() => language.validateLanguageOptions({})).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should ignore unknown options", () => { | ||
| expect(() => | ||
| language.validateLanguageOptions({ variant: "podman" }), | ||
| ).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parse", () => { | ||
| it("should parse a Dockerfile into an ast with comments", () => { | ||
| const result = language.parse({ | ||
| body: "# start\nFROM node:20\n", | ||
| path: "Dockerfile", | ||
| physicalPath: "Dockerfile", | ||
| bom: false, | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
|
|
||
| const okResult = result as OkParseResult<DockerfileNode>; | ||
|
|
||
| expect(okResult.ast.type).toBe("Dockerfile"); | ||
| expect(okResult.ast.body).toHaveLength(1); | ||
| expect(okResult.ast.comments).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("should parse malformed input without fatal errors", () => { | ||
| const result = language.parse({ | ||
| body: "THIS IS NOT [ VALID \\ anything\n\t%%", | ||
| path: "Dockerfile", | ||
| physicalPath: "Dockerfile", | ||
| bom: false, | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| }); | ||
|
|
||
| it("should report unexpected parser exceptions as errors", () => { | ||
| const result = language.parse({ | ||
| body: null as never, | ||
| path: "Dockerfile", | ||
| physicalPath: "Dockerfile", | ||
| bom: false, | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
|
|
||
| if (!result.ok) { | ||
| expect(result.errors).toHaveLength(1); | ||
| expect(result.errors[0]).toMatchObject({ line: 1, column: 1 }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("createSourceCode", () => { | ||
| it("should create a DockerFileSourceCode from a parse result", () => { | ||
| const file = { | ||
| body: "FROM node:20\n", | ||
| path: "Dockerfile", | ||
| physicalPath: "Dockerfile", | ||
| bom: false, | ||
| }; | ||
| const result = language.parse( | ||
| file, | ||
| ) as OkParseResult<DockerfileNode>; | ||
| const sourceCode = language.createSourceCode(file, result); | ||
|
|
||
| expect(sourceCode).toBeInstanceOf(DockerFileSourceCode); | ||
| expect(sourceCode.text).toBe(file.body); | ||
| expect(sourceCode.ast).toBe(result.ast); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * @fileoverview The DockerFileLanguage class, the ESLint Language implementation | ||
| * for Dockerfiles. | ||
| */ | ||
|
|
||
| import type { | ||
| File, | ||
| Language, | ||
| LanguageContext, | ||
| OkParseResult, | ||
| ParseResult, | ||
| } from "@eslint/core"; | ||
| import { parseDocker } from "../parser/parse.js"; | ||
| import { DockerFileSourceCode } from "./docker-source-code.js"; | ||
| import { visitorKeys } from "../visitor-keys.js"; | ||
| import type { | ||
| DockerFileLanguageOptions, | ||
| DockerNode, | ||
| DockerfileNode, | ||
| } from "../types.js"; | ||
|
|
||
| /** | ||
| * ESLint Language implementation for Dockerfiles. | ||
| */ | ||
| export class DockerFileLanguage implements Language<{ | ||
| LangOptions: DockerFileLanguageOptions; | ||
| Code: DockerFileSourceCode; | ||
| RootNode: DockerfileNode; | ||
| Node: DockerNode; | ||
| }> { | ||
| fileType = "text" as const; | ||
| lineStart = 1 as const; | ||
| columnStart = 1 as const; | ||
| nodeTypeKey = "type"; | ||
| visitorKeys = visitorKeys; | ||
|
|
||
| defaultLanguageOptions: DockerFileLanguageOptions = {}; | ||
|
|
||
| validateLanguageOptions(_languageOptions: DockerFileLanguageOptions): void { | ||
| // The Docker language currently has no options. By convention, | ||
| // unknown language options are ignored rather than rejected. | ||
| } | ||
|
|
||
| parse( | ||
| file: File, | ||
| _context?: LanguageContext<DockerFileLanguageOptions>, | ||
| ): ParseResult<DockerfileNode> { | ||
| const text = file.body as string; | ||
|
|
||
| try { | ||
| // The Dockerfile parser is error-tolerant and never throws for | ||
| // malformed input, so parsing always succeeds. | ||
| const { ast } = parseDocker(text); | ||
|
|
||
| return { ok: true, ast }; | ||
| } catch (error) { | ||
| return { | ||
| ok: false, | ||
| errors: [ | ||
| { | ||
| message: | ||
| error instanceof Error | ||
| ? error.message | ||
| : String(error), | ||
| line: 1, | ||
| column: 1, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| createSourceCode( | ||
| file: File, | ||
| parseResult: OkParseResult<DockerfileNode>, | ||
| ): DockerFileSourceCode { | ||
| return new DockerFileSourceCode({ | ||
| text: file.body as string, | ||
| ast: parseResult.ast, | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.