[CALCITE-7424] LintTest should check that files marked with '// lint: sort' are sorted#4813
Closed
julianhyde wants to merge 6 commits intoapache:mainfrom
Closed
[CALCITE-7424] LintTest should check that files marked with '// lint: sort' are sorted#4813julianhyde wants to merge 6 commits intoapache:mainfrom
julianhyde wants to merge 6 commits intoapache:mainfrom
Conversation
… sort' are sorted Replace the hard-coded testContributorsFileIsSorted test with a general '// lint: sort' directive that files can embed to declare their own sort requirements. Add Sort and SortConsumer classes to parse and enforce the directive. Annotate .mailmap and contributors.yml with the directive. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
A man page-style specification follows.
LINT:SORT DIRECTIVE
NAME
lint:sort - Specification-based sorting directive for maintaining
alphabetically ordered code sections
SYNOPSIS
// lint: sort [until 'END_PATTERN'] [where 'FILTER_PATTERN'] [erase 'ERASE_PATTERN']
DESCRIPTION
The lint:sort directive enforces alphabetical ordering of lines
within a specified code section. It extracts sort keys from
matching lines, optionally filters and transforms them, then
validates alphabetical order.
PARAMETERS
until 'END_PATTERN'
Optional. Regular expression marking the end of the sorted
section. Supports '#' placeholder (parent indent, -2 spaces)
and '##' (current line indent).
where 'FILTER_PATTERN'
Optional. Regular expression to select lines for sorting. Only
matching lines are checked. Supports '#' and '##' placeholders.
If you want to match a literal '#' (as in a bash comment),
write '[#]'.
erase 'ERASE_PATTERN'
Optional. Regular expression to remove from lines before
comparing. Useful for ignoring type annotations, modifiers,
etc.
PLACEHOLDERS
# Parent indentation level (current indent minus 2 spaces)
## Current line's indentation level
MULTI-LINE SPECIFICATIONS
Long directives can span multiple lines by ending each line with
'\'. The backslash and newline are removed during parsing.
Example:
// lint: sort until '#}' \
// where '##private static final' \
// erase 'Applicable[0-4]*'
EXAMPLES
Sort cases of a switch:
switch (x) {
// lint: sort until '#}' where '##case '
case A:
// some code
case B:
// some code
}
Sort Maven dependencies:
<!-- lint: sort until '#</dependencies>' where '## <groupId>' -->
Sort constants, ignoring the type of those constants
// lint: sort until '#}' \
// where '##private static final [^ ]+ [^ ]+ =' \
// erase '##private static final [^ ]+ '
VIOLATIONS
Violations report:
- File path and line number
- The out-of-order line
- The line it should precede
NOTES
- Sorting is case-sensitive
- Empty lines and comments between sorted items are preserved
- The directive itself is not included in the sorted section
- Use specific patterns to avoid false matches (e.g., exact
indentation instead of \\s* for nested structures)
- Multi-line specs help with readability and avoid formatter issues
|
xiedeyantu
approved these changes
Mar 1, 2026
| } | ||
|
|
||
| /** Ensures that the {@code .mailmap} file is sorted. */ | ||
| @Test void testMailmapFile() { |
Member
There was a problem hiding this comment.
Could this case have been forgotten to be deleted?
Member
|
Please remember to make the Jira title, PR title, and commit message consistent. |
github-actions bot
pushed a commit
that referenced
this pull request
Mar 1, 2026
LintTest should check that files are sorted. A '// lint: sort'
directive lets a file declare its own sort requirements. Add Sort
and SortConsumer classes to parse and enforce the directive.
Annotate .mailmap and contributors.yml files with the directive,
and remove hard-coded tests that used to keep them sorted.
A man page-style specification follows.
LINT:SORT DIRECTIVE
NAME
lint:sort - Specification-based sorting directive for maintaining
alphabetically ordered code sections
SYNOPSIS
// lint: sort [until 'END_PATTERN'] [where 'FILTER_PATTERN'] [erase 'ERASE_PATTERN']
DESCRIPTION
The lint:sort directive enforces alphabetical ordering of lines
within a specified code section. It extracts sort keys from
matching lines, optionally filters and transforms them, then
validates alphabetical order.
PARAMETERS
until 'END_PATTERN'
Optional. Regular expression marking the end of the sorted
section. Supports '#' placeholder (parent indent, -2 spaces)
and '##' (current line indent).
where 'FILTER_PATTERN'
Optional. Regular expression to select lines for sorting. Only
matching lines are checked. Supports '#' and '##' placeholders.
If you want to match a literal '#' (as in a bash comment),
write '[#]'.
erase 'ERASE_PATTERN'
Optional. Regular expression to remove from lines before
comparing. Useful for ignoring type annotations, modifiers,
etc.
PLACEHOLDERS
# Parent indentation level (current indent minus 2 spaces)
## Current line's indentation level
MULTI-LINE SPECIFICATIONS
Long directives can span multiple lines by ending each line with
'\'. The backslash and newline are removed during parsing.
Example:
// lint: sort until '#}' \
// where '##private static final' \
// erase 'Applicable[0-4]*'
EXAMPLES
Sort cases of a switch:
switch (x) {
// lint: sort until '#}' where '##case '
case A:
// some code
case B:
// some code
}
Sort Maven dependencies:
<!-- lint: sort until '#</dependencies>' where '## <groupId>' -->
Sort constants, ignoring the type of those constants
// lint: sort until '#}' \
// where '##private static final [^ ]+ [^ ]+ =' \
// erase '##private static final [^ ]+ '
VIOLATIONS
Violations report:
- File path and line number
- The out-of-order line
- The line it should precede
NOTES
- Sorting is case-sensitive
- Empty lines and comments between sorted items are preserved
- The directive itself is not included in the sorted section
- Use specific patterns to avoid false matches (e.g., exact
indentation instead of \\s* for nested structures)
- Multi-line specs help with readability and avoid formatter issues
This feature is based on hydromatic/morel#316.
Close #4813
Contributor
Author
|
Thanks for your review, @xiedeyantu. I have made the changes you suggested, and have merged to main. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



See CALCITE-7424.
This is a prerequisite for CALCITE-7420, because we want rule tests to be sorted by test name within each
.iqfile.