Skip to content

Commit a4994d9

Browse files
Add and abide by Airbnb ESLint config (#1361)
1 parent 6dc28bc commit a4994d9

38 files changed

+1017
-771
lines changed

eslint.config.mjs

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
// @ts-check
22

33
import eslint from '@eslint/js';
4+
import eslintPluginJest from 'eslint-plugin-jest';
5+
import globals from 'globals';
46
import tseslint from 'typescript-eslint';
57

8+
import bestPractices from 'eslint-config-airbnb-base/rules/best-practices';
9+
import errors from 'eslint-config-airbnb-base/rules/errors';
10+
import es6 from 'eslint-config-airbnb-base/rules/es6';
11+
import node from 'eslint-config-airbnb-base/rules/node';
12+
import strict from 'eslint-config-airbnb-base/rules/strict';
13+
import style from 'eslint-config-airbnb-base/rules/style';
14+
import variables from 'eslint-config-airbnb-base/rules/variables';
15+
16+
const bestPracticesRules = bestPractices.rules;
17+
const errorsRules = errors.rules;
18+
const es6Rules = es6.rules;
19+
const nodeRules = node.rules;
20+
const strictRules = strict.rules;
21+
const styleRules = style.rules;
22+
const variablesRules = variables.rules;
23+
624
export default tseslint.config(
725
eslint.configs.recommended,
826
...tseslint.configs.recommended,
927
...tseslint.configs.stylistic,
1028
{
29+
languageOptions: {
30+
globals: {
31+
...globals.node,
32+
},
33+
},
1134
rules: {
35+
...bestPracticesRules,
36+
...errorsRules,
37+
...es6Rules,
38+
...nodeRules,
39+
...strictRules,
40+
...styleRules,
41+
...variablesRules,
42+
43+
'class-methods-use-this': 'off',
44+
'linebreak-style': ['error', (process.platform === 'win32' ? 'windows' : 'unix')],
45+
'max-len': ['error', { code: 120, ignoreUrls: true }],
46+
'no-underscore-dangle': 'off',
47+
'no-unused-vars': 'off',
48+
'object-curly-spacing': ['error', 'always'],
49+
'operator-linebreak': ['error', 'after'],
1250
'quotes': ['error', 'single'],
51+
'quote-props': ['error', 'consistent'],
1352

1453
'@typescript-eslint/no-empty-function': 'off',
1554
'@typescript-eslint/no-explicit-any': 'off',
@@ -21,14 +60,42 @@ export default tseslint.config(
2160
destructuredArrayIgnorePattern: '^_',
2261
},
2362
],
24-
'object-curly-spacing': ['error', 'always'],
25-
}
63+
},
64+
},
65+
{
66+
files: ['src/formatter/templates/*.ts'],
67+
rules: {
68+
'indent': 'off',
69+
'template-curly-spacing': ['error', 'always'],
70+
},
71+
},
72+
{
73+
files: ['script/**/*.ts'],
74+
rules: {
75+
'no-console': 'off',
76+
},
77+
},
78+
{
79+
files: ['test/**/*.test.ts'],
80+
...eslintPluginJest.configs['flat/recommended'],
81+
rules: {
82+
...eslintPluginJest.configs['flat/recommended'].rules,
83+
'jest/no-disabled-tests': 'off',
84+
'jest/no-standalone-expect': 'off',
85+
'jest/prefer-expect-assertions': 'off',
86+
},
87+
},
88+
{
89+
files: ['test/utilities.ts'],
90+
rules: {
91+
'jest/no-export': 'off',
92+
},
2693
},
2794
{
2895
ignores: [
2996
'lib/**/*',
3097
'src/normalize_mappings/suffix-normalize-mapping.ts',
3198
'src/parser/*/peg_parser.ts',
32-
]
33-
}
99+
],
100+
},
34101
);

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"detect-newline": "^4.0.1",
4242
"esbuild": "^0.23.0",
4343
"eslint": "^9.11.0",
44+
"eslint-config-airbnb": "^19.0.4",
45+
"eslint-plugin-jest": "^28.8.3",
46+
"globals": "^15.9.0",
4447
"husky": "^9.0.11",
4548
"jest": "^27.0.1",
4649
"jsdoc-babel": "^0.5.0",
@@ -76,7 +79,7 @@
7679
"build:pegjs:chords-over-words": "tsx script/generate_parser.ts chords_over_words",
7780
"build:scales": "tsx script/generate_scales.ts && yarn linter:fix src/scales.ts",
7881
"build:sources": "parcel build",
79-
"build:suffix-normalize": "shx rm -rf src/normalize_mappings/suffix-normalize-mapping.ts && tsx src/normalize_mappings/generate-suffix-normalize-mapping.ts",
82+
"build:suffix-normalize": "shx rm -rf src/normalize_mappings/suffix-normalize-mapping.ts && tsx script/generate-suffix-normalize-mapping.ts",
8083
"ci": "yarn install && yarn lint && yarn test && yarn build && yarn readme",
8184
"clean": "shx rm -rf node_modules && shx rm -rf lib",
8285
"debug:chordpro": "tsx script/debug_parser.ts chord_pro --skip-chord-grammar",

script/combine_files.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import process from 'process';
23

34
const fileAPath = process.argv[2];
45
const fileBPath = process.argv[3];

script/debug_parser.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// eslint no-console: "off"
2+
13
import fs from 'fs';
4+
import process from 'process';
25
import puppeteer from 'puppeteer';
36
import esbuild from 'esbuild';
47

@@ -34,7 +37,7 @@ const parserSource = [
3437

3538
async function run() {
3639
const browser = await puppeteer.launch({
37-
args:['--start-maximized'],
40+
args: ['--start-maximized'],
3841
defaultViewport: null,
3942
headless: false,
4043
});
@@ -43,15 +46,16 @@ async function run() {
4346
await browser.close();
4447
}
4548

46-
for (const event of ['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM']) {
49+
['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach((event) => {
4750
process.on(event, shutdownHandler);
48-
}
51+
});
4952

5053
const [page] = await browser.pages();
5154
await page.setViewport({ width: 0, height: 0 });
5255
await page.goto('https://peggyjs.org/online.html');
5356

5457
await page.evaluate((grammar) => {
58+
// eslint-disable-next-line no-undef
5559
const textarea = document.getElementById('grammar');
5660
if (!textarea) return;
5761

@@ -70,4 +74,4 @@ async function run() {
7074

7175
run()
7276
.then(() => console.log('Done'))
73-
.catch(e => console.error(e));
77+
.catch((e) => console.error(e));

script/generate_parser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import peggy from 'peggy';
4+
import process from 'process';
45
import tspegjs from 'ts-pegjs';
56
import fs from 'fs';
67

script/generate_scales.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ const keyToGradeMapping = `
109109
`).join('\n')
110110
}
111111
};
112-
`;
112+
`.substring(1);
113113

114114
fs.writeFileSync('src/scales.ts', keyToGradeMapping);

src/chord.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -363,21 +363,21 @@ class Chord implements ChordProperties {
363363
},
364364
) {
365365
this.suffix = suffix || null;
366-
this.root = this.determineRoot({
366+
this.root = Chord.determineRoot({
367367
root, base, modifier, suffix, chordType,
368368
});
369-
this.bass = this.determineBass({
369+
this.bass = Chord.determineBass({
370370
bass, bassBase, bassModifier, chordType,
371371
});
372372
}
373373

374374
equals(otherChord: Chord): boolean {
375-
return this.suffix === otherChord.suffix
376-
&& Key.equals(this.root, otherChord.root)
377-
&& Key.equals(this.bass, otherChord.bass);
375+
return this.suffix === otherChord.suffix &&
376+
Key.equals(this.root, otherChord.root) &&
377+
Key.equals(this.bass, otherChord.bass);
378378
}
379379

380-
determineRoot(
380+
static determineRoot(
381381
{
382382
root,
383383
base,
@@ -404,7 +404,7 @@ class Chord implements ChordProperties {
404404
});
405405
}
406406

407-
determineBass(
407+
static determineBass(
408408
{
409409
bass,
410410
bassBase,

src/chord_sheet/song.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ class Song extends MetadataAccessors {
7373
*/
7474
metadata: Metadata;
7575

76-
currentLine: Line | null = null;
76+
_bodyLines: Line[] | null = null;
7777

78-
warnings: ParserWarning[] = [];
78+
_bodyParagraphs: Paragraph[] | null = null;
7979

80-
sectionType: ParagraphType = NONE;
80+
currentKey: string | null = null;
81+
82+
currentLine: Line | null = null;
8183

8284
fontStack: FontStack = new FontStack();
8385

84-
currentKey: string | null = null;
86+
sectionType: ParagraphType = NONE;
8587

8688
transposeKey: string | null = null;
8789

88-
_bodyParagraphs: Paragraph[] | null = null;
89-
90-
_bodyLines: Line[] | null = null;
90+
warnings: ParserWarning[] = [];
9191

9292
/**
9393
* Creates a new {Song} instance

src/chord_sheet/tag.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,14 @@ const translateTagNameAlias = (name: string) => {
398398
* Represents a tag/directive. See https://www.chordpro.org/chordpro/chordpro-directives/
399399
*/
400400
class Tag extends AstComponent {
401+
_isMetaTag = false;
402+
401403
_originalName = '';
402404

403405
_name = '';
404406

405407
_value = '';
406408

407-
_isMetaTag = false;
408-
409409
constructor(name: string, value: string | null = null, traceInfo: TraceInfo | null = null) {
410410
super(traceInfo);
411411
this.parseNameValue(name, value);

src/chord_sheet_serializer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
SerializedTag, SerializedTernary,
2121
} from './serialized_types';
2222
import SoftLineBreak from './chord_sheet/soft_line_break';
23+
import { warn } from './utilities';
2324

2425
const CHORD_LYRICS_PAIR = 'chordLyricsPair';
2526
const CHORD_SHEET = 'chordSheet';
@@ -154,7 +155,7 @@ class ChordSheetSerializer {
154155
this.parseLine(astComponent);
155156
break;
156157
default:
157-
console.warn(`Unhandled AST component "${astComponent.type}"`, astComponent);
158+
warn(`Unhandled AST component "${astComponent.type}"`);
158159
}
159160

160161
return null;

src/formatter/templates/html_div_formatter.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ export default (
5454
${ when(isChordLyricsPair(item), () => `
5555
<div class="column">
5656
${ when(item.annotation).then(() => `
57-
<div class="annotation"${ fontStyleTag(line.chordFont) }>${item.annotation}</div>
57+
<div class="annotation"${ fontStyleTag(line.chordFont) }>${ item.annotation }</div>
5858
`).else(() => `
59-
<div class="chord"${ fontStyleTag(line.chordFont) }>${
60-
renderChord(
59+
<div class="chord"${ fontStyleTag(line.chordFont) }>
60+
${ renderChord(
6161
item.chords,
6262
line,
6363
song,
6464
{
6565
renderKey: key,
6666
useUnicodeModifier: configuration.useUnicodeModifiers,
6767
normalizeChords: configuration.normalizeChords,
68-
}
69-
)
70-
}</div>
68+
},
69+
) }
70+
</div>
7171
`) }
7272
<div class="lyrics"${ fontStyleTag(line.textFont) }>${ item.lyrics }</div>
7373
</div>
@@ -82,7 +82,9 @@ export default (
8282
`).elseWhen(isEvaluatable(item), () => `
8383
<div class="column">
8484
<div class="chord"></div>
85-
<div class="lyrics"${ fontStyleTag(line.textFont) }>${ evaluate(item, metadata, configuration) }</div>
85+
<div class="lyrics"${ fontStyleTag(line.textFont) }>
86+
${ evaluate(item, metadata, configuration) }
87+
</div>
8688
</div>
8789
`) }
8890
`) }

src/formatter/templates/html_table_formatter.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export default (
3434
bodyParagraphs,
3535
}: HtmlTemplateArgs,
3636
): string => stripHTML(`
37-
${ when(title, () => `<h1>${ title}</h1>`) }
38-
${ when(subtitle, () => `<h2>${ subtitle}</h2>`) }
37+
${ when(title, () => `<h1>${ title }</h1>`) }
38+
${ when(subtitle, () => `<h2>${ subtitle }</h2>`) }
3939
4040
${ when(bodyLines.length > 0, () => `
4141
<div class="chord-sheet">
@@ -53,15 +53,15 @@ export default (
5353
`).else(() => `
5454
${ each(paragraph.lines, (line) => `
5555
${ when(renderBlankLines || lineHasContents(line), () => `
56-
<table class="${ lineClasses(line)}">
56+
<table class="${ lineClasses(line) }">
5757
${ when(hasChordContents(line), () => `
5858
<tr>
5959
${ each(line.items, (item) => `
6060
${ when(isChordLyricsPair(item), () => `
6161
${ when(item.annotation).then(() => `
62-
<td class="annotation"${fontStyleTag(line.chordFont)}>${item.annotation}</td>
62+
<td class="annotation"${ fontStyleTag(line.chordFont) }>${ item.annotation }</td>
6363
`).else(() => `
64-
<td class="chord"${fontStyleTag(line.chordFont)}>${
64+
<td class="chord"${ fontStyleTag(line.chordFont) }>${
6565
renderChord(
6666
item.chords,
6767
line,
@@ -77,25 +77,27 @@ export default (
7777
`) }
7878
`) }
7979
</tr>
80-
`)}
80+
`) }
8181
8282
${ when(hasTextContents(line), () => `
8383
<tr>
8484
${ each(line.items, (item) => `
8585
${ when(isChordLyricsPair(item), () => `
86-
<td class="lyrics"${fontStyleTag(line.textFont)}>${ item.lyrics}</td>
86+
<td class="lyrics"${ fontStyleTag(line.textFont) }>${ item.lyrics }</td>
8787
`).elseWhen(isTag(item), () => `
8888
${ when(isComment(item), () => `
89-
<td class="comment"${fontStyleTag(line.textFont)}>${ item.value }</td>
89+
<td class="comment"${ fontStyleTag(line.textFont) }>${ item.value }</td>
9090
`) }
9191
9292
${ when(item.hasRenderableLabel(), () => `
93-
<td><h3 class="label"${fontStyleTag(line.textFont)}>${ item.value }</h3></td>
93+
<td><h3 class="label"${ fontStyleTag(line.textFont) }>${ item.value }</h3></td>
9494
`) }
9595
`).elseWhen(isLiteral(item), () => `
96-
<td class="literal">${item.string}</td>
96+
<td class="literal">${ item.string }</td>
9797
`).elseWhen(isEvaluatable(item), () => `
98-
<td class="lyrics"${fontStyleTag(line.textFont)}>${ evaluate(item, metadata, configuration) }</td>
98+
<td class="lyrics"${ fontStyleTag(line.textFont) }>
99+
${ evaluate(item, metadata, configuration) }
100+
</td>
99101
`) }
100102
`) }
101103
</tr>
@@ -107,5 +109,5 @@ export default (
107109
</div>
108110
`) }
109111
</div>
110-
`)}
112+
`) }
111113
`);

0 commit comments

Comments
 (0)