Skip to content

Commit

Permalink
Check trailing whitespace on CI (#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Dec 8, 2024
1 parent 559a478 commit f882bb0
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 188 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ jobs:
- name: Run tests
run: yarn ci

check-trailing-whitespace:
runs-on: ubuntu-latest
name: Check trailing whitespace
steps:
- uses: actions/checkout@v4
- name: Enable Corepack
run: corepack enable
- name: Install node modules
run: yarn install
- name: Check trailing whitespace
run: git ls-files | yarn tsx script/check_trailing_whitespace.ts

build-windows:
continue-on-error: true
runs-on: windows-latest
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ When your changes are complete, be sure to run `yarn readme` to regenerate `READ

## Pull request guidelines

N.B. I do not expect you to have all required knowledge and experience to meet these guidelines;
N.B. I do not expect you to have all required knowledge and experience to meet these guidelines;
I'm happy to help you out! ❤️
However, the better your PR meets these guidelines the sooner it will get merged.

Expand Down
8 changes: 4 additions & 4 deletions script/build_chord_pro_section_grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function sectionTags(sectionName: string, shortTag: boolean, type: 'start' | 'en
export default function buildChordProSectionGrammar(_: BuildOptions, _data: string): string {
const sectionsGrammars = sections.map(([name, shortTags]) => `
${capitalize(name)}Section
= startTag:${capitalize(name)}StartTag
NewLine
content:$(!${capitalize(name)}EndTag SectionCharacter)*
endTag:${capitalize(name)}EndTag
= startTag:${capitalize(name)}StartTag
NewLine
content:$(!${capitalize(name)}EndTag SectionCharacter)*
endTag:${capitalize(name)}EndTag
{
return helpers.buildSection(startTag, endTag, content);
}
Expand Down
18 changes: 9 additions & 9 deletions script/build_scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ export default function buildScales(_: BuildOptions): string {
- edit the data source, located in \`data/scales.ts\`
- run \`yarn build:code-generate\` to re-generate this file.
*/
import {
ChordType,
ChordType,
Mode,
ModifierMaybe,
FLAT,
ModifierMaybe,
FLAT,
MAJOR,
MINOR,
NO_MODIFIER,
NUMERAL,
NUMERIC,
SHARP,
NO_MODIFIER,
NUMERAL,
NUMERIC,
SHARP,
SYMBOL,
SOLFEGE
} from './constants';
Expand Down Expand Up @@ -83,7 +83,7 @@ export default function buildScales(_: BuildOptions): string {
`).join('\n')
}
};
export const GRADE_TO_KEY: Record<ChordType, Record<Mode, Record<ModifierMaybe, Record<number, string>>>> = {
${
Object.entries(KEY_TYPES).map(([keyTypeName, keyTypeValue]) => `
Expand Down
50 changes: 50 additions & 0 deletions script/check_trailing_whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EOL } from 'node:os';
import fs from 'node:fs';

const filesWithTrailingWhitespace: Record<string, number[]> = {};

const files =
fs
.readFileSync(0, 'utf8')
.split(EOL)
.filter((f) => f.length > 0);

console.log(`Check ${files.length} files for trailing whitespace`);

files.forEach((filename) => {
const contents = fs.readFileSync(filename, 'utf8');
const lines = contents.split(EOL);

lines.forEach((line, index) => {
if (line.endsWith(' ')) {
if (!(filename in filesWithTrailingWhitespace)) {
filesWithTrailingWhitespace[filename] = [];
}

filesWithTrailingWhitespace[filename].push(index + 1);
}
});
});

const fileNamesWithTrailingWhitespace = Object.keys(filesWithTrailingWhitespace);
const fileCount = fileNamesWithTrailingWhitespace.length;

if (fileCount === 0) {
console.log('No files with trailing whitespace');
process.exit(0);
}

const errorMessage =
fileNamesWithTrailingWhitespace
.map((filename) => {
const specification =
filesWithTrailingWhitespace[filename]
.map((lineNumber) => ` ${filename}:${lineNumber}`)
.join(EOL);

return ` ${filename}:${EOL}${specification}`;
})
.join(EOL);

console.error(`Found ${fileCount} files with trailing whitespace:${EOL}${errorMessage}`);
process.exit(1);
4 changes: 2 additions & 2 deletions src/formatter/templates/html_div_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default (
): string => stripHTML(`
${ when(title, () => `<h1>${ title }</h1>`) }
${ when(subtitle, () => `<h2>${ subtitle }</h2>`) }
<div class="chord-sheet">
${ each(bodyParagraphs, (paragraph) => `
<div class="${ paragraphClasses(paragraph) }">
Expand Down Expand Up @@ -75,7 +75,7 @@ export default (
${ when(isComment(item), () => `
<div class="comment">${ item.value }</div>
`) }
${ when(item.hasRenderableLabel(), () => `
<h3 class="label">${ item.label }</h3>
`) }
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/templates/html_table_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default (
`) }
</tr>
`) }
${ when(hasTextContents(line), () => `
<tr>
${ each(line.items, (item) => `
Expand All @@ -88,7 +88,7 @@ export default (
${ when(isComment(item), () => `
<td class="comment"${ fontStyleTag(line.textFont) }>${ item.value }</td>
`) }
${ when(item.hasRenderableLabel(), () => `
<td><h3 class="label"${ fontStyleTag(line.textFont) }>${ item.label }</h3></td>
`) }
Expand Down
10 changes: 5 additions & 5 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ export function gradeToKey({

if (!key) {
throw new Error(
`Could not resolve
type=${type}
modifier=${modifier}
grade=${grade}
preferredModifier=${preferredModifier}
`Could not resolve
type=${type}
modifier=${modifier}
grade=${grade}
preferredModifier=${preferredModifier}
minor=${minor}
to a key`,
);
Expand Down
4 changes: 2 additions & 2 deletions test/formatter/chords_over_words_formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('ChordsOverWordsFormatter', () => {
Let it be, let it be, let it be, let it be
D strong G A G D/F# Em D
Whisper words of wisdom, let it be
Breakdown
Em F C G
Whisper words of wisdom, let it be
Expand All @@ -51,7 +51,7 @@ describe('ChordsOverWordsFormatter', () => {
Tab 1
Tab line 1
Tab line 2
ABC 1
ABC line 1
ABC line 2
Expand Down
Loading

0 comments on commit f882bb0

Please sign in to comment.