-
Notifications
You must be signed in to change notification settings - Fork 950
feat(rules): add scope-delimiter-style #4580
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
what1s1ove
wants to merge
1
commit into
conventional-changelog:master
Choose a base branch
from
what1s1ove:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+487
−28
Open
Changes from all commits
Commits
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
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,194 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import parse from "@commitlint/parse"; | ||
| import { scopeDelimiterStyle } from "./scope-delimiter-style.js"; | ||
|
|
||
| const messages = { | ||
| noScope: "feat: subject", | ||
|
|
||
| kebabScope: "feat(lint-staged): subject", | ||
| snakeScope: "feat(my_scope): subject", | ||
|
|
||
| defaultSlash: "feat(core/api): subject", | ||
| defaultComma: "feat(core,api): subject", | ||
| defaultCommaSpace: "feat(core, api): subject", | ||
| defaultBackslash: "feat(core\\api): subject", | ||
|
|
||
| nonDefaultPipe: "feat(core|api): subject", | ||
| nonDefaultStar: "feat(core*api): subject", | ||
| mixedCustom: "feat(core|api/utils): subject", | ||
| } as const; | ||
|
|
||
| describe("Scope Delimiter Validation", () => { | ||
| describe("Messages without scopes", () => { | ||
| test('Succeeds for "always" when there is no scope', async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.noScope), | ||
| "always", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual(undefined); | ||
| }); | ||
|
|
||
| test('Succeeds for "never" when there is no scope', async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.noScope), | ||
| "never", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual(undefined); | ||
| }); | ||
| }); | ||
|
|
||
| describe('"always" with default configuration', () => { | ||
| test.each([ | ||
| { scenario: "kebab-case scope", commit: messages.kebabScope }, | ||
| { scenario: "snake_case scope", commit: messages.snakeScope }, | ||
| ] as const)( | ||
| "Treats $scenario as part of the scope and not a delimiter", | ||
| async ({ commit }) => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(commit), | ||
| "always", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); | ||
| }, | ||
| ); | ||
|
|
||
| test.each([ | ||
| { scenario: "comma ',' delimiter", commit: messages.defaultComma }, | ||
| { scenario: "slash '/' delimiter", commit: messages.defaultSlash }, | ||
| { | ||
| scenario: "backslash '\\' delimiter", | ||
| commit: messages.defaultBackslash, | ||
| }, | ||
| ] as const)("Succeeds when only $scenario is used", async ({ commit }) => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(commit), | ||
| "always", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { scenario: "comma without space", commit: messages.defaultComma }, | ||
| { scenario: "comma with space", commit: messages.defaultCommaSpace }, | ||
| ] as const)( | ||
| "Normalizes $scenario as the same delimiter ','", | ||
| async ({ commit }) => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(commit), | ||
| "always", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); | ||
| }, | ||
| ); | ||
|
|
||
| test("Fails when a non-default delimiter is used", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.nonDefaultStar), | ||
| "always", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(false); | ||
| expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe('"never" with default configuration', () => { | ||
| test("Fails when scope uses only default delimiters", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.defaultSlash), | ||
| "never", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(false); | ||
| expect(error).toEqual("scope delimiters must not be one of [/, \\, ,]"); | ||
| }); | ||
|
|
||
| test("Succeeds when scope uses only non-default delimiter", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.nonDefaultPipe), | ||
| "never", | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must not be one of [/, \\, ,]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Custom configuration", () => { | ||
| test("Falls back to default delimiters when delimiters is an empty array", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.defaultComma), | ||
| "always", | ||
| [], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [/, \\, ,]"); | ||
| }); | ||
|
|
||
| test("Succeeds when a custom single allowed delimiter is used", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.nonDefaultStar), | ||
| "always", | ||
| ["*"], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [*]"); | ||
| }); | ||
|
|
||
| test("Fails when ',' is used but only '/' is allowed", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.defaultComma), | ||
| "always", | ||
| ["/"], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(false); | ||
| expect(error).toEqual("scope delimiters must be one of [/]"); | ||
| }); | ||
|
|
||
| test("Succeeds when both '/' and '|' are allowed and used in the scope", async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.mixedCustom), | ||
| "always", | ||
| ["/", "|"], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must be one of [/, |]"); | ||
| }); | ||
|
|
||
| test('In "never" mode fails when explicitly forbidden delimiter is used', async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.nonDefaultPipe), | ||
| "never", | ||
| ["|"], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(false); | ||
| expect(error).toEqual("scope delimiters must not be one of [|]"); | ||
| }); | ||
|
|
||
| test('In "never" mode succeeds when delimiter is not in the forbidden list', async () => { | ||
| const [actual, error] = scopeDelimiterStyle( | ||
| await parse(messages.nonDefaultPipe), | ||
| "never", | ||
| ["/"], | ||
| ); | ||
|
|
||
| expect(actual).toEqual(true); | ||
| expect(error).toEqual("scope delimiters must not be one of [/]"); | ||
| }); | ||
| }); | ||
| }); |
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,41 @@ | ||
| import * as ensure from "@commitlint/ensure"; | ||
| import message from "@commitlint/message"; | ||
| import { SyncRule } from "@commitlint/types"; | ||
|
|
||
| export const scopeDelimiterStyle: SyncRule<string[]> = ( | ||
| { scope }, | ||
| when = "always", | ||
| value = [], | ||
| ) => { | ||
| if (!scope) { | ||
| return [true]; | ||
| } | ||
|
|
||
| const delimiters = value.length ? value : ["/", "\\", ","]; | ||
| const scopeRawDelimiters = scope.match(/[^A-Za-z0-9-_]+/g) ?? []; | ||
| const scopeDelimiters = [ | ||
| ...new Set( | ||
| scopeRawDelimiters.map((delimiter) => { | ||
| const trimmed = delimiter.trim(); | ||
|
|
||
| if (trimmed === ",") { | ||
| return ","; | ||
| } | ||
|
|
||
| return delimiter; | ||
| }), | ||
| ), | ||
| ]; | ||
|
|
||
| const isAllDelimitersAllowed = scopeDelimiters.every((delimiter) => { | ||
| return ensure.enum(delimiter, delimiters); | ||
| }); | ||
| const isNever = when === "never"; | ||
|
|
||
| return [ | ||
| isNever ? !isAllDelimitersAllowed : isAllDelimitersAllowed, | ||
| message([ | ||
| `scope delimiters must ${isNever ? "not " : ""}be one of [${delimiters.join(", ")}]`, | ||
| ]), | ||
| ]; | ||
| }; | ||
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.