Skip to content

Commit

Permalink
chore: set non-null-assertions rule to error
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Mar 13, 2022
1 parent 96c84a3 commit f1a65f3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"prettier"
],
"rules": {
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-unused-vars": "off"
Expand Down
6 changes: 4 additions & 2 deletions packages/parse5-html-rewriting-stream/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class RewritingStream extends SAXParser {
// Events
protected override emitIfListenerExists(eventName: string, token: SaxToken): boolean {
if (!super.emitIfListenerExists(eventName, token)) {
this.emitRaw(this._getRawHtml(token.sourceCodeLocation!));
const html = token.sourceCodeLocation ? this._getRawHtml(token.sourceCodeLocation) : '';
this.emitRaw(html);
}

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

// Emitter API
protected override _emitToken(eventName: string, token: SaxToken): void {
this.emit(eventName, token, this._getRawHtml(token.sourceCodeLocation!));
const html = token.sourceCodeLocation ? this._getRawHtml(token.sourceCodeLocation) : '';
this.emit(eventName, token, html);
}

/** Emits a serialized document type token into the output stream. */
Expand Down
9 changes: 7 additions & 2 deletions packages/parse5-htmlparser2-tree-adapter/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,16 @@ export function insertTextBefore(parentNode: NodeWithChildren, text: string, ref
export function adoptAttributes(recipient: Element, attrs: Attribute[]): void {
for (let i = 0; i < attrs.length; i++) {
const attrName = attrs[i].name;
const { namespace, prefix } = attrs[i];

if (typeof recipient.attribs[attrName] === 'undefined') {
recipient.attribs[attrName] = attrs[i].value;
recipient['x-attribsNamespace']![attrName] = attrs[i].namespace!;
recipient['x-attribsPrefix']![attrName] = attrs[i].prefix!;
if (recipient['x-attribsNamespace'] && namespace) {
recipient['x-attribsNamespace'][attrName] = namespace;
}
if (recipient['x-attribsPrefix'] && prefix) {
recipient['x-attribsPrefix'][attrName] = prefix;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/parse5-parser-stream/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap>
}

while (this.pendingHtmlInsertions.length > 0) {
const html = this.pendingHtmlInsertions.pop()!;
const html = this.pendingHtmlInsertions.pop() ?? '';

this.parser.tokenizer.insertHtmlAtCurrentPos(html);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/parse5/lib/parser/formatting-element-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export class FormattingElementList<T extends TreeAdapterTypeMap> {
}

insertElementAfterBookmark(element: T['element'], token: TagToken): void {
const bookmarkIdx = this.entries.indexOf(this.bookmark!);
if (this.bookmark === null) {
return;
}

const bookmarkIdx = this.entries.indexOf(this.bookmark);

this.entries.splice(bookmarkIdx, 0, {
type: EntryType.Element,
Expand Down
12 changes: 8 additions & 4 deletions packages/parse5/lib/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
}

onItemPop(node: T['parentNode'], isTop: boolean): void {
if (this.options.sourceCodeLocationInfo) {
this._setEndLocation(node, this.currentToken!);
if (this.options.sourceCodeLocationInfo && this.currentToken) {
this._setEndLocation(node, this.currentToken);
}

this.treeAdapter.onItemPop?.(node, this.openElements.current);
Expand Down Expand Up @@ -1729,9 +1729,13 @@ function startTagAfterHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Ta
case $.TEMPLATE:
case $.TITLE: {
p._err(token, ERR.abandonedHeadElementChild);
p.openElements.push(p.headElement!, $.HEAD);
if (p.headElement) {
p.openElements.push(p.headElement, $.HEAD);
}
startTagInHead(p, token);
p.openElements.remove(p.headElement!);
if (p.headElement) {
p.openElements.remove(p.headElement);
}
break;
}
case $.HEAD: {
Expand Down
6 changes: 5 additions & 1 deletion packages/parse5/lib/tokenizer/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export class Preprocessor {
this.pos -= count;

while (this.pos < this.lastGapPos) {
this.lastGapPos = this.gapStack.pop()!;
const lastGapPos = this.gapStack.pop();
if (lastGapPos === undefined) {
throw new Error('Gap stack was unexpectedly empty');
}
this.lastGapPos = lastGapPos;
this.pos--;
}

Expand Down

0 comments on commit f1a65f3

Please sign in to comment.