Skip to content

Commit 148894d

Browse files
Restrict maximum number of lines per function (martijnversluis#1645)
1 parent 920e4c2 commit 148894d

File tree

7 files changed

+78
-82
lines changed

7 files changed

+78
-82
lines changed

eslint.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default tseslint.config(
4646
'max-depth': ['error', 2],
4747
'max-len': ['error', { code: 120, ignoreUrls: true }],
4848
'max-lines': ['error', 700],
49+
'max-lines-per-function': ['error', { max: 25, skipBlankLines: true, skipComments: true }],
4950
'max-statements': ['error', 10],
5051
'no-underscore-dangle': 'off',
5152
'no-unused-vars': 'off',
@@ -71,12 +72,14 @@ export default tseslint.config(
7172
files: ['src/formatter/templates/*.ts'],
7273
rules: {
7374
'indent': 'off',
75+
'max-lines-per-function': 'off',
7476
'template-curly-spacing': ['error', 'always'],
7577
},
7678
},
7779
{
7880
files: ['script/**/*.ts'],
7981
rules: {
82+
'max-lines-per-function': 'off',
8083
'no-console': 'off',
8184
},
8285
},
@@ -89,19 +92,22 @@ export default tseslint.config(
8992
'jest/no-standalone-expect': 'off',
9093
'jest/prefer-expect-assertions': 'off',
9194
'max-lines': 'off',
95+
'max-lines-per-function': 'off',
9296
'max-statements': 'off',
9397
},
9498
},
9599
{
96100
files: ['test/fixtures/**/*.ts'],
97101
rules: {
98102
'max-lines': 'off',
103+
'max-lines-per-function': 'off',
99104
},
100105
},
101106
{
102107
files: ['test/matchers.ts', 'test/utilities.ts', 'unibuild.ts'],
103108
rules: {
104109
'jest/no-export': 'off',
110+
'max-lines-per-function': 'off',
105111
'max-statements': 'off',
106112
},
107113
},

src/chord.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ interface ChordProperties {
1717
bass?: Key | null;
1818
}
1919

20+
export interface ChordConstructorOptions {
21+
base?: string | number | null;
22+
modifier?: Modifier | null;
23+
suffix?: string | null;
24+
bassBase?: string | number | null;
25+
bassModifier?: Modifier | null;
26+
root?: Key | null;
27+
bass?: Key | null;
28+
chordType?: ChordType | null;
29+
}
30+
2031
/**
2132
* Represents a Chord, consisting of a root, suffix (quality) and bass
2233
*/
@@ -351,16 +362,7 @@ class Chord implements ChordProperties {
351362
root = null,
352363
bass = null,
353364
chordType = null,
354-
}: {
355-
base?: string | number | null,
356-
modifier?: Modifier | null,
357-
suffix?: string | null,
358-
bassBase?: string | number | null,
359-
bassModifier?: Modifier | null,
360-
root?: Key | null,
361-
bass?: Key | null,
362-
chordType?: ChordType | null,
363-
},
365+
}: ChordConstructorOptions,
364366
) {
365367
this.suffix = suffix || null;
366368
this.root = Chord.determineRoot({

src/chord_sheet/song.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ class Song extends MetadataAccessors {
211211
return song.mapItems((item) => {
212212
if (item instanceof Tag && item.name === KEY) {
213213
transposedKey = Key.wrapOrFail(item.value).transpose(delta);
214-
215-
if (modifier) {
216-
transposedKey = transposedKey.useModifier(modifier);
217-
}
218-
214+
if (modifier) transposedKey = transposedKey.useModifier(modifier);
219215
return item.set({ value: transposedKey.toString() });
220216
}
221217

src/formatter/chord_renderer.ts

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ import { NullableChordStyle } from '../constants';
44
import { transposeDistance } from '../helpers';
55
import { callChain } from '../utilities';
66

7+
interface ConstructorOptions {
8+
capo: number;
9+
contextKey: Key | null;
10+
decapo: boolean;
11+
normalizeChords: boolean;
12+
renderKey: Key | null;
13+
songKey: Key | null;
14+
style: NullableChordStyle;
15+
transposeKey: string | null;
16+
useUnicodeModifier: boolean;
17+
}
18+
19+
const defaultConstructorOptions = {
20+
capo: 0,
21+
contextKey: null,
22+
decapo: false,
23+
normalizeChords: true,
24+
renderKey: null,
25+
songKey: null,
26+
style: null,
27+
transposeKey: null,
28+
useUnicodeModifier: false,
29+
};
30+
731
class ChordRenderer {
832
capo: number;
933

@@ -21,47 +45,17 @@ class ChordRenderer {
2145

2246
useUnicodeModifier: boolean;
2347

24-
constructor(
25-
{
26-
capo,
27-
contextKey,
28-
decapo,
29-
normalizeChords,
30-
renderKey,
31-
songKey,
32-
style,
33-
transposeKey,
34-
useUnicodeModifier,
35-
}: {
36-
capo: number,
37-
contextKey: Key | null,
38-
decapo: boolean,
39-
normalizeChords: boolean,
40-
renderKey: Key | null,
41-
songKey: Key | null,
42-
style: NullableChordStyle,
43-
transposeKey: string | null,
44-
useUnicodeModifier: boolean,
45-
} = {
46-
capo: 0,
47-
contextKey: null,
48-
decapo: false,
49-
normalizeChords: true,
50-
renderKey: null,
51-
songKey: null,
52-
style: null,
53-
transposeKey: null,
54-
useUnicodeModifier: false,
55-
},
56-
) {
57-
this.capo = decapo ? capo : 0;
58-
this.contextKey = contextKey;
59-
this.normalizeChords = normalizeChords;
60-
this.renderKey = renderKey;
61-
this.songKey = songKey;
62-
this.style = style;
63-
this.transposeKey = transposeKey;
64-
this.useUnicodeModifier = useUnicodeModifier;
48+
constructor(options: Partial<ConstructorOptions> = {}) {
49+
const config: ConstructorOptions = { ...defaultConstructorOptions, ...options };
50+
51+
this.capo = config.decapo ? config.capo : 0;
52+
this.contextKey = config.contextKey;
53+
this.normalizeChords = config.normalizeChords;
54+
this.renderKey = config.renderKey;
55+
this.songKey = config.songKey;
56+
this.style = config.style;
57+
this.transposeKey = config.transposeKey;
58+
this.useUnicodeModifier = config.useUnicodeModifier;
6559
}
6660

6761
render(chordString: string): string {

src/formatter/html_table_formatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import HtmlFormatter, { Template, CSS, HtmlTemplateCssClasses } from './html_formatter';
22
import template from './templates/html_table_formatter';
33

4+
/* eslint-disable-next-line max-lines-per-function */
45
function defaultCss(cssClasses: HtmlTemplateCssClasses): CSS {
56
const {
67
annotation,

src/key.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ const NO_FLAT_NUMBERS = [1, 4];
4242
const NO_SHARP_GRADES = [5, 0];
4343
const NO_SHARP_NUMBERS = [3, 7];
4444

45+
interface ConstructorOptions {
46+
grade?: number | null;
47+
number?: number | null;
48+
minor: boolean;
49+
type: ChordType;
50+
modifier: Modifier | null;
51+
referenceKeyGrade?: number | null;
52+
originalKeyString?: string | null;
53+
preferredModifier: Modifier | null;
54+
}
55+
4556
/**
4657
* Represents a key, such as Eb (symbol), #3 (numeric) or VII (numeral).
4758
*
@@ -105,6 +116,7 @@ class Key implements KeyProperties {
105116
});
106117
}
107118

119+
/* eslint-disable-next-line max-lines-per-function */
108120
static resolve(
109121
{
110122
key,
@@ -256,16 +268,7 @@ class Key implements KeyProperties {
256268
referenceKeyGrade = null,
257269
originalKeyString = null,
258270
preferredModifier = null,
259-
}: {
260-
grade?: number | null,
261-
number?: number | null,
262-
minor: boolean,
263-
type: ChordType,
264-
modifier: Modifier | null,
265-
referenceKeyGrade?: number | null,
266-
originalKeyString?: string | null,
267-
preferredModifier: Modifier | null,
268-
},
271+
}: ConstructorOptions,
269272
) {
270273
this.grade = grade;
271274
this.number = number;

src/utilities.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,33 +152,27 @@ function determineKey({
152152
return new GradeSet(grades).determineGrade(modifier, preferredModifier, grade);
153153
}
154154

155-
export function gradeToKey({
156-
type,
157-
modifier,
158-
preferredModifier,
159-
grade,
160-
minor,
161-
}: {
155+
export function gradeToKey(options: {
162156
type: ChordType,
163157
modifier: ModifierMaybe | null,
164158
preferredModifier: Modifier | null,
165159
grade: number,
166160
minor: boolean,
167161
}): string {
162+
const {
163+
type,
164+
modifier,
165+
preferredModifier,
166+
grade,
167+
minor,
168+
} = options;
169+
168170
let key = determineKey({
169171
type, modifier, preferredModifier, grade, minor,
170172
});
171173

172174
if (!key) {
173-
throw new Error(
174-
`Could not resolve
175-
type=${type}
176-
modifier=${modifier}
177-
grade=${grade}
178-
preferredModifier=${preferredModifier}
179-
minor=${minor}
180-
to a key`,
181-
);
175+
throw new Error(`Could not resolve ${options} to a key`);
182176
}
183177

184178
if (minor && type === NUMERAL) {

0 commit comments

Comments
 (0)