Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup eslint config #454

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": true,
"jest": true
},
"extends": ["eslint:recommended", "prettier", "plugin:unicorn/recommended"],
"extends": ["eslint:recommended", "plugin:unicorn/recommended", "prettier"],
"rules": {
"no-console": "error",
"curly": ["error", "all"],
Expand Down Expand Up @@ -38,17 +38,18 @@
"overrides": [
{
"files": "*.ts",
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"extends": ["plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/explicit-function-return-type": "error",

"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
"@typescript-eslint/no-unused-vars": "off"
}
},
{
"files": "*.test.ts",
"rules": {
"@typescript-eslint/no-non-null-assertion": "off"
}
}
]
Expand Down
4 changes: 3 additions & 1 deletion packages/parse5-htmlparser2-tree-adapter/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ export function getNodeSourceCodeLocation(node: Node): ElementLocation | null |
}

export function updateNodeSourceCodeLocation(node: Node, endLocation: ElementLocation): void {
if (endLocation.endOffset != null) node.endIndex = endLocation.endOffset;
if (endLocation.endOffset != null) {
node.endIndex = endLocation.endOffset;
}

node.sourceCodeLocation = {
...node.sourceCodeLocation,
Expand Down
3 changes: 2 additions & 1 deletion packages/parse5-sax-parser/test/sax-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const hugePage = new URL('../../../test/data/huge-page/huge-page.html', import.m

describe('SAX parser', () => {
//Basic tests
for (const [idx, data] of loadSAXParserTestData().entries())
for (const [idx, data] of loadSAXParserTestData().entries()) {
it(`${idx + 1}.${data.name}`, createBasicTest(data.src, data.expected));
}

it('Piping and .stop()', async () => {
const parser = new SAXParser();
Expand Down
8 changes: 6 additions & 2 deletions packages/parse5/lib/parser/formatting-element-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ export class FormattingElementList<T extends TreeAdapterTypeMap> {
}

private _ensureNoahArkCondition(newElement: T['element']): void {
if (this.entries.length < NOAH_ARK_CAPACITY) return;
if (this.entries.length < NOAH_ARK_CAPACITY) {
return;
}

const neAttrs = this.treeAdapter.getAttrList(newElement);
const candidates = this._getNoahArkConditionCandidates(newElement, neAttrs);

if (candidates.length < NOAH_ARK_CAPACITY) return;
if (candidates.length < NOAH_ARK_CAPACITY) {
return;
}

//NOTE: build attrs map for the new element, so we can perform fast lookups
const neAttrsMap = new Map(neAttrs.map((neAttr: Attribute) => [neAttr.name, neAttr.value]));
Expand Down
32 changes: 24 additions & 8 deletions packages/parse5/lib/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack

//Errors
_err(token: Token, code: ERR, beforeToken?: boolean): void {
if (!this.onParseError) return;
if (!this.onParseError) {
return;
}

const loc = token.location ?? BASE_LOC;
const err = {
Expand All @@ -254,7 +256,9 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
//Stack events
onItemPush(node: T['parentNode'], tid: number, isTop: boolean): void {
this.treeAdapter.onItemPush?.(node);
if (isTop && this.openElements.stackTop > 0) this._setContextModes(node, tid);
if (isTop && this.openElements.stackTop > 0) {
this._setContextModes(node, tid);
}
}

onItemPop(node: T['parentNode'], isTop: boolean): void {
Expand Down Expand Up @@ -419,12 +423,16 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
this.treeAdapter.setTemplateContent(tmpl, content);
this._attachElementToTree(tmpl, token.location);
this.openElements.push(tmpl, token.tagID);
if (this.options.sourceCodeLocationInfo) this.treeAdapter.setNodeSourceCodeLocation(content, null);
if (this.options.sourceCodeLocationInfo) {
this.treeAdapter.setNodeSourceCodeLocation(content, null);
}
}

_insertFakeRootElement(): void {
const element = this.treeAdapter.createElement(TN.HTML, NS.HTML, []);
if (this.options.sourceCodeLocationInfo) this.treeAdapter.setNodeSourceCodeLocation(element, null);
if (this.options.sourceCodeLocationInfo) {
this.treeAdapter.setNodeSourceCodeLocation(element, null);
}

this.treeAdapter.appendChild(this.openElements.current, element);
this.openElements.push(element, $.HTML);
Expand Down Expand Up @@ -457,7 +465,9 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
this.treeAdapter.insertText(parent, token.chars);
}

if (!token.location) return;
if (!token.location) {
return;
}

const siblings = this.treeAdapter.getChildNodes(parent);
const textNodeIdx = beforeElement ? siblings.lastIndexOf(beforeElement) : siblings.length;
Expand Down Expand Up @@ -509,7 +519,9 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
//Token processing
private shouldProcessStartTagTokenInForeignContent(token: TagToken): boolean {
// Check that neither current === document, or ns === NS.HTML
if (!this.currentNotInHTML) return false;
if (!this.currentNotInHTML) {
return false;
}

let current: T['parentNode'];
let currentTagId: number;
Expand Down Expand Up @@ -1365,7 +1377,9 @@ function callAdoptionAgency<T extends TreeAdapterTypeMap>(p: Parser<T>, token: T
const commonAncestor = p.openElements.getCommonAncestor(formattingElementEntry.element);

p.treeAdapter.detachNode(lastElement);
if (commonAncestor) aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement);
if (commonAncestor) {
aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement);
}
aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry);
}
}
Expand Down Expand Up @@ -2421,7 +2435,9 @@ function genericEndTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token:
// Compare the tag name here, as the tag might not be a known tag with an ID.
if (tid === elementId && (tid !== $.UNKNOWN || p.treeAdapter.getTagName(element) === tn)) {
p.openElements.generateImpliedEndTagsWithExclusion(tid);
if (p.openElements.stackTop >= i) p.openElements.shortenToLength(i);
if (p.openElements.stackTop >= i) {
p.openElements.shortenToLength(i);
}
break;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/parse5/lib/serializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function serializeAttributes<T extends TreeAdapterTypeMap>(

if (!attr.namespace) {
html += attr.name;
} else
} else {
switch (attr.namespace) {
case NS.XML: {
html += `xml:${attr.name}`;
Expand All @@ -207,6 +207,7 @@ function serializeAttributes<T extends TreeAdapterTypeMap>(
html += `${attr.prefix}:${attr.name}`;
}
}
}

html += `="${escapeString(attr.value, true)}"`;
}
Expand Down
21 changes: 15 additions & 6 deletions packages/parse5/lib/tokenizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ export class Tokenizer {
}

private _runParsingLoop(): void {
if (this.inLoop) return;
if (this.inLoop) {
return;
}

this.inLoop = true;

Expand Down Expand Up @@ -306,7 +308,9 @@ export class Tokenizer {
this.paused = false;

// Necessary for synchronous resume.
if (this.inLoop) return;
if (this.inLoop) {
return;
}

this._runParsingLoop();

Expand Down Expand Up @@ -610,7 +614,9 @@ export class Tokenizer {
for (let i = 0, current = htmlDecodeTree[0]; i >= 0; cp = this._consume()) {
i = determineBranch(htmlDecodeTree, current, i + 1, cp);

if (i < 0) break;
if (i < 0) {
break;
}

excess += 1;

Expand Down Expand Up @@ -1132,7 +1138,7 @@ export class Tokenizer {
this._createStartTagToken();
this.state = State.TAG_NAME;
this._stateTagName(cp);
} else
} else {
switch (cp) {
case $.EXCLAMATION_MARK: {
this.state = State.MARKUP_DECLARATION_OPEN;
Expand Down Expand Up @@ -1162,6 +1168,7 @@ export class Tokenizer {
this._stateData(cp);
}
}
}
}

// End tag open state
Expand All @@ -1171,7 +1178,7 @@ export class Tokenizer {
this._createEndTagToken();
this.state = State.TAG_NAME;
this._stateTagName(cp);
} else
} else {
switch (cp) {
case $.GREATER_THAN_SIGN: {
this._err(ERR.missingEndTagName);
Expand All @@ -1191,6 +1198,7 @@ export class Tokenizer {
this._stateBogusComment(cp);
}
}
}
}

// Tag name state
Expand Down Expand Up @@ -2316,7 +2324,7 @@ export class Tokenizer {
if (isAsciiUpper(cp)) {
this._createDoctypeToken(String.fromCharCode(toAsciiLower(cp)));
this.state = State.DOCTYPE_NAME;
} else
} else {
switch (cp) {
case $.SPACE:
case $.LINE_FEED:
Expand Down Expand Up @@ -2354,6 +2362,7 @@ export class Tokenizer {
this.state = State.DOCTYPE_NAME;
}
}
}
}

// DOCTYPE name state
Expand Down