Skip to content

[CALCITE-7424] LintTest should check that files marked with '// lint: sort' are sorted#4813

Closed
julianhyde wants to merge 6 commits intoapache:mainfrom
julianhyde:7424-lint-sort
Closed

[CALCITE-7424] LintTest should check that files marked with '// lint: sort' are sorted#4813
julianhyde wants to merge 6 commits intoapache:mainfrom
julianhyde:7424-lint-sort

Conversation

@julianhyde
Copy link
Contributor

See CALCITE-7424.

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

julianhyde and others added 5 commits February 27, 2026 22:36
… 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
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 1, 2026

}

/** Ensures that the {@code .mailmap} file is sorted. */
@Test void testMailmapFile() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this case have been forgotten to be deleted?

@xiedeyantu
Copy link
Member

Please remember to make the Jira title, PR title, and commit message consistent.

@julianhyde julianhyde closed this in 80e717a Mar 1, 2026
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
@julianhyde
Copy link
Contributor Author

Thanks for your review, @xiedeyantu. I have made the changes you suggested, and have merged to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants