Skip to content

Commit f75e614

Browse files
committed
chore: set non-null-assertions rule to error
1 parent 96c84a3 commit f75e614

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"prettier"
3737
],
3838
"rules": {
39-
"@typescript-eslint/no-non-null-assertion": "warn",
39+
"@typescript-eslint/no-non-null-assertion": "error",
4040
"@typescript-eslint/no-explicit-any": "warn",
4141
"@typescript-eslint/explicit-function-return-type": "error",
4242
"@typescript-eslint/no-unused-vars": "off"

packages/parse5-html-rewriting-stream/lib/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ export class RewritingStream extends SAXParser {
7575
// Events
7676
protected override emitIfListenerExists(eventName: string, token: SaxToken): boolean {
7777
if (!super.emitIfListenerExists(eventName, token)) {
78-
this.emitRaw(this._getRawHtml(token.sourceCodeLocation!));
78+
const html = token.sourceCodeLocation ? this._getRawHtml(token.sourceCodeLocation) : '';
79+
this.emitRaw(html);
7980
}
8081

8182
// NOTE: don't skip new lines after `<pre>` and other tags,
@@ -86,7 +87,8 @@ export class RewritingStream extends SAXParser {
8687

8788
// Emitter API
8889
protected override _emitToken(eventName: string, token: SaxToken): void {
89-
this.emit(eventName, token, this._getRawHtml(token.sourceCodeLocation!));
90+
const html = token.sourceCodeLocation ? this._getRawHtml(token.sourceCodeLocation) : '';
91+
this.emit(eventName, token, html);
9092
}
9193

9294
/** Emits a serialized document type token into the output stream. */

packages/parse5-htmlparser2-tree-adapter/lib/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,17 @@ export function insertTextBefore(parentNode: NodeWithChildren, text: string, ref
174174
export function adoptAttributes(recipient: Element, attrs: Attribute[]): void {
175175
for (let i = 0; i < attrs.length; i++) {
176176
const attrName = attrs[i].name;
177+
const { namespace } = attrs[i];
178+
const { prefix } = attrs[i];
177179

178180
if (typeof recipient.attribs[attrName] === 'undefined') {
179181
recipient.attribs[attrName] = attrs[i].value;
180-
recipient['x-attribsNamespace']![attrName] = attrs[i].namespace!;
181-
recipient['x-attribsPrefix']![attrName] = attrs[i].prefix!;
182+
if (recipient['x-attribsNamespace'] && namespace) {
183+
recipient['x-attribsNamespace'][attrName] = namespace;
184+
}
185+
if (recipient['x-attribsPrefix'] && prefix) {
186+
recipient['x-attribsPrefix'][attrName] = prefix;
187+
}
182188
}
183189
}
184190
}

packages/parse5-parser-stream/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap>
7676
}
7777

7878
while (this.pendingHtmlInsertions.length > 0) {
79-
const html = this.pendingHtmlInsertions.pop()!;
79+
const html = this.pendingHtmlInsertions.pop() ?? '';
8080

8181
this.parser.tokenizer.insertHtmlAtCurrentPos(html);
8282
}

packages/parse5/lib/parser/formatting-element-list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ export class FormattingElementList<T extends TreeAdapterTypeMap> {
114114
}
115115

116116
insertElementAfterBookmark(element: T['element'], token: TagToken): void {
117-
const bookmarkIdx = this.entries.indexOf(this.bookmark!);
117+
if (this.bookmark === null) {
118+
return;
119+
}
120+
121+
const bookmarkIdx = this.entries.indexOf(this.bookmark);
118122

119123
this.entries.splice(bookmarkIdx, 0, {
120124
type: EntryType.Element,

packages/parse5/lib/parser/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
294294
}
295295

296296
onItemPop(node: T['parentNode'], isTop: boolean): void {
297-
if (this.options.sourceCodeLocationInfo) {
298-
this._setEndLocation(node, this.currentToken!);
297+
if (this.options.sourceCodeLocationInfo && this.currentToken) {
298+
this._setEndLocation(node, this.currentToken);
299299
}
300300

301301
this.treeAdapter.onItemPop?.(node, this.openElements.current);
@@ -1729,9 +1729,13 @@ function startTagAfterHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Ta
17291729
case $.TEMPLATE:
17301730
case $.TITLE: {
17311731
p._err(token, ERR.abandonedHeadElementChild);
1732-
p.openElements.push(p.headElement!, $.HEAD);
1732+
if (p.headElement) {
1733+
p.openElements.push(p.headElement, $.HEAD);
1734+
}
17331735
startTagInHead(p, token);
1734-
p.openElements.remove(p.headElement!);
1736+
if (p.headElement) {
1737+
p.openElements.remove(p.headElement);
1738+
}
17351739
break;
17361740
}
17371741
case $.HEAD: {

packages/parse5/lib/tokenizer/preprocessor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ export class Preprocessor {
235235
this.pos -= count;
236236

237237
while (this.pos < this.lastGapPos) {
238-
this.lastGapPos = this.gapStack.pop()!;
238+
const lastGapPos = this.gapStack.pop();
239+
if (lastGapPos === undefined) {
240+
throw new Error('Gap stack was unexpectedly empty');
241+
}
242+
this.lastGapPos = lastGapPos;
239243
this.pos--;
240244
}
241245

0 commit comments

Comments
 (0)