Repository navigation
feat: add consistent-instruction-casing rule #5
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
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
131eb9e
feat: add consistent-instruction-casing rule
nzakas 0ed88b1
fix: correct casing rule range and recommended config types
nzakas 63ce8e1
refactor: use DockerFileLanguage in rule tests
nzakas fdc1360
docs: add JSDoc tags to consistent-instruction-casing and rule doc tests
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # consistent-instruction-casing | ||
|
|
||
| Require instruction keywords to be uppercase. | ||
|
|
||
| ## Background | ||
|
|
||
| Dockerfile instruction keywords are not case-sensitive, so `from`, `From`, | ||
| and `FROM` all work. The convention, used throughout the Docker | ||
| documentation and enforced by BuildKit's build checks, is to write | ||
| keywords in uppercase to distinguish them from their arguments. Mixed | ||
| casing within one Dockerfile makes the file harder to scan. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns when an instruction keyword is not written entirely in | ||
| uppercase. It checks every instruction, including the trigger instruction | ||
| of `ONBUILD` and unknown keywords, and it is automatically fixable. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```dockerfile | ||
| # eslint docker/consistent-instruction-casing: "error" | ||
|
|
||
| from node:20-alpine | ||
| workdir /app | ||
| Copy . . | ||
| onbuild run npm ci | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```dockerfile | ||
| # eslint docker/consistent-instruction-casing: "error" | ||
|
|
||
| FROM node:20-alpine | ||
| WORKDIR /app | ||
| COPY . . | ||
| ONBUILD RUN npm ci | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has no options. | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If your project deliberately uses lowercase instruction keywords and you | ||
| are consistent about it, you can safely disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [`ConsistentInstructionCasing`](https://docs.docker.com/reference/build-checks/consistent-instruction-casing/) | ||
| build check | ||
| - [`FromAsCasing`](https://docs.docker.com/reference/build-checks/from-as-casing/) | ||
| build check |
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,79 @@ | ||
| /** | ||
| * @fileoverview Tests for the consistent-instruction-casing rule. | ||
| */ | ||
|
|
||
| import { RuleTester } from "eslint"; | ||
| import { DockerFileLanguage } from "../languages/docker-language.js"; | ||
| import rule from "./consistent-instruction-casing.js"; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| docker: { | ||
| languages: { dockerfile: new DockerFileLanguage() }, | ||
| }, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. | ||
| } as any, | ||
| language: "docker/dockerfile", | ||
| }); | ||
|
|
||
| ruleTester.run("consistent-instruction-casing", rule as never, { | ||
| valid: [ | ||
| "FROM node:20", | ||
| "FROM node:20 AS base\nRUN echo hi", | ||
| 'CMD ["node", "app.js"]', | ||
| "# a comment\nWORKDIR /app", | ||
| "ONBUILD COPY . /app", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "straße value", | ||
| output: "STRASSE value", | ||
| errors: [{ messageId: "expectedUppercase" }], | ||
| }, | ||
| { | ||
| code: "from node:20", | ||
| output: "FROM node:20", | ||
| errors: [ | ||
| { | ||
| messageId: "expectedUppercase", | ||
| data: { actual: "from", expected: "FROM" }, | ||
| line: 1, | ||
| column: 1, | ||
| endColumn: 5, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "FROM node:20\nrun echo hi", | ||
| output: "FROM node:20\nRUN echo hi", | ||
| errors: [ | ||
| { | ||
| messageId: "expectedUppercase", | ||
| line: 2, | ||
| column: 1, | ||
| endColumn: 4, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "Workdir /app", | ||
| output: "WORKDIR /app", | ||
| errors: [{ messageId: "expectedUppercase" }], | ||
| }, | ||
| { | ||
| // Both the ONBUILD keyword and its trigger are checked. | ||
| code: "onbuild copy . /app", | ||
| output: "ONBUILD COPY . /app", | ||
| errors: [ | ||
| { messageId: "expectedUppercase", column: 1, endColumn: 8 }, | ||
| { messageId: "expectedUppercase", column: 9, endColumn: 13 }, | ||
| ], | ||
| }, | ||
| { | ||
| // Unknown instructions are still normalized. | ||
| code: "bogus one two", | ||
| output: "BOGUS one two", | ||
| errors: [{ messageId: "expectedUppercase" }], | ||
| }, | ||
| ], | ||
| }); |
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,85 @@ | ||
| /** | ||
| * @fileoverview Rule to require instruction keywords to be written in | ||
| * uppercase, per the Dockerfile format conventions. | ||
| */ | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Imports | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| import { INSTRUCTION_TYPES } from "../types.js"; | ||
| import type { | ||
| DockerNode, | ||
| DockerRuleDefinition, | ||
| DockerRuleVisitor, | ||
| InstructionNode, | ||
| } from "../types.js"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule: DockerRuleDefinition<{ MessageIds: "expectedUppercase" }> = { | ||
| meta: { | ||
| type: "layout", | ||
| docs: { | ||
| description: "Require instruction keywords to be uppercase", | ||
| recommended: true, | ||
| url: "https://github.com/eslint/docker/blob/main/docs/rules/consistent-instruction-casing.md", | ||
| }, | ||
| fixable: "code", | ||
| schema: [], | ||
| messages: { | ||
| expectedUppercase: | ||
| "Instruction keyword '{{actual}}' should be written as '{{expected}}'.", | ||
| }, | ||
| }, | ||
|
|
||
| /** | ||
| * Creates the rule's visitor. | ||
| * @param context The rule context. | ||
| * @returns A visitor that checks the keyword of every instruction. | ||
| */ | ||
| create(context) { | ||
| const { sourceCode } = context; | ||
|
|
||
| /** | ||
| * Reports an instruction whose keyword is not written in | ||
| * uppercase. | ||
| * @param node The instruction to check. | ||
| */ | ||
| function check(node: InstructionNode): void { | ||
| // The keyword in the source is the first whitespace-delimited | ||
| // word. Its length can differ from the normalized keyword's | ||
| // (for example, "straße" normalizes to "STRASSE"). | ||
| const written = /^\S*/u.exec( | ||
| sourceCode.text.slice(node.start, node.end), | ||
| )?.[0] as string; | ||
| const keywordEnd = node.start + written.length; | ||
|
|
||
| if (written === node.keyword) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| loc: sourceCode.getLoc({ | ||
| start: node.start, | ||
| end: keywordEnd, | ||
| } as DockerNode), | ||
| messageId: "expectedUppercase", | ||
| data: { actual: written, expected: node.keyword }, | ||
| fix: fixer => | ||
| fixer.replaceTextRange( | ||
| [node.start, keywordEnd], | ||
| node.keyword, | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| return Object.fromEntries( | ||
| INSTRUCTION_TYPES.map(type => [type, check]), | ||
| ) as DockerRuleVisitor; | ||
| }, | ||
| }; | ||
|
|
||
| export default rule; | ||
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,23 @@ | ||
| /** | ||
| * @fileoverview Type-level tests for the exported configurations. | ||
| */ | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Imports | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| import type { Linter } from "eslint"; | ||
| import { describe, expectTypeOf, it } from "vitest"; | ||
| import docker from "../src/index.js"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| describe("configs", () => { | ||
| it("should type recommended rule severities as ESLint severities", () => { | ||
| expectTypeOf( | ||
| docker.configs.recommended.rules, | ||
| ).toMatchTypeOf<Linter.RulesRecord>(); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keyword span excludes continuation sequences before editing.
The rule takes the keyword span from the first run of non-whitespace characters. That run includes the escape character, so it can end inside a line continuation. Two inputs are affected:
R\followed by a newline andUN echo ok: the span isR\, the reported keyword isR\, and the autofix producesRUNfollowed by a newline andUN echo ok. The instruction is split.RUN\followed by a newline andecho ok: the already-uppercase keyword is reported, and the autofix removes the continuation backslash. The instruction changes meaning.The span must cover the logical keyword characters from
node.keyword, and it must not include the escape character or the line continuation that follows it. The rule must not report an instruction whose logical keyword is already uppercase, and it must not edit characters outside the logical keyword. The contract insrc/types.tsalready requires this comparison before editing. Keep the lowercase control,run echo ok, fixed toRUN echo ok.OpenGrep (1.33.1)
[ERROR] 55-57: A nonliteral argument is passed to exec/execSync. Identify the receiver: Node child_process methods execute shell commands, while regular-expression and database methods have different contracts. For shell execution, prefer execFile or spawn with an argument array.
(coderabbit.command-injection.exec-js)