Skip to content

Commit

Permalink
Use defined() in Glyph, Modifier, Note, Ornament.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyeh committed Aug 31, 2021
1 parent 9ca2f0a commit 9f0f53d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
4 changes: 1 addition & 3 deletions src/glyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ export class Glyph extends Element {
}

static lookupGlyph(fontStack: Font[], code: string): { font: Font; glyph: FontGlyph } {
if (!fontStack) {
throw new RuntimeError('BAD_FONTSTACK', 'Font stack is misconfigured');
}
defined(fontStack, 'BadFontStack', 'Font stack is misconfigured');

let glyph: FontGlyph;
let font: Font;
Expand Down
19 changes: 8 additions & 11 deletions src/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ export class Modifier extends Element {

/** Get attached note (`StaveNote`, `TabNote`, etc.) */
getNote(): Note {
if (!this.note) throw new RuntimeError('NoNote', 'Modifier has no note.');
return this.note;
return defined(this.note, 'NoNote', 'Modifier has no note.');
}

/** Check and get attached note (`StaveNote`, `TabNote`, etc.) */
/**
* Used in draw() to check and get the attached note (`StaveNote`, `TabNote`, etc.).
* Also verifies that the index is valid.
*/
checkAttachedNote(): Note {
if (!this.note || this.index === undefined) {
throw new RuntimeError('NoAttachedNote', `Can't draw ${this.getCategory()} without a note and index.`);
}
return this.note;
defined(this.index, 'NoIndex', `Can't draw ${this.getCategory()} without an index.`);
return defined(this.note, 'NoNote', `Can't draw ${this.getCategory()} without a note.`);
}

/**
Expand All @@ -131,10 +131,7 @@ export class Modifier extends Element {

/** Check and get note index, which is a specific note in a chord. */
checkIndex(): number {
if (this.index === undefined) {
throw new RuntimeError('NoIndex', 'Modifier has an invalid index.');
}
return this.index;
return defined(this.index, 'NoIndex', 'Modifier has an invalid index.');
}

/** Set note index, which is a specific note in a chord. */
Expand Down
5 changes: 1 addition & 4 deletions src/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,7 @@ export abstract class Note extends Tickable {

/** Check and get the beam. */
checkBeam(): Beam {
if (!this.beam) {
throw new RuntimeError('NoBeam', 'No beam attached to instance');
}
return this.beam;
return defined(this.beam, 'NoBeam', 'No beam attached to instance');
}

/** Check it has a beam. */
Expand Down
8 changes: 3 additions & 5 deletions src/ornament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Author: Cyril Silverman
// MIT License

import { RuntimeError, log } from './util';
import { RuntimeError, log, defined } from './util';
import { Flow } from './flow';
import { Modifier } from './modifier';
import { TickContext } from './tickcontext';
Expand Down Expand Up @@ -83,10 +83,8 @@ export class Ornament extends Modifier {
if (Ornament.ornamentArticulation.indexOf(ornament.type) >= 0) {
// Unfortunately we don't know the stem direction. So we base it
// on the line number, but also allow it to be overridden.
if (!ornament.note) {
throw new RuntimeError('NoAttachedNote');
}
if (ornament.note.getLineNumber() >= 3 || ornament.getPosition() === Modifier.Position.ABOVE) {
const ornamentNote = defined(ornament.note, 'NoAttachedNote');
if (ornamentNote.getLineNumber() >= 3 || ornament.getPosition() === Modifier.Position.ABOVE) {
state.top_text_line += increment;
ornament.y_shift += yOffset;
yOffset -= ornament.glyph.bbox.getH();
Expand Down

0 comments on commit 9f0f53d

Please sign in to comment.