Skip to content

Commit

Permalink
style: format ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Oct 25, 2023
1 parent 6284a1d commit b98a3e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
49 changes: 26 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Parser, ParserOptions } from 'htmlparser2';
import { LocationTracker, SourceLocation } from './location-tracker';
import { Parser, ParserOptions } from "htmlparser2";
import { LocationTracker, SourceLocation } from "./location-tracker";

export type Directive = {
name: string | RegExp;
Expand Down Expand Up @@ -30,15 +30,15 @@ export type Node = NodeText | NodeTag;
const defaultOptions: ParserOptions = {
lowerCaseTags: false,
lowerCaseAttributeNames: false,
decodeEntities: false
decodeEntities: false,
};

const defaultDirectives: Directive[] = [
{
name: '!doctype',
start: '<',
end: '>'
}
name: "!doctype",
start: "<",
end: ">",
},
];

export const parser = (html: string, options: Options = {}): Node[] => {
Expand All @@ -54,7 +54,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {

function isDirective(directive: Directive, tag: string): boolean {
if (directive.name instanceof RegExp) {
const regex = new RegExp(directive.name.source, 'i');
const regex = new RegExp(directive.name.source, "i");

return regex.test(tag);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
return;
}

if (typeof last === 'object') {
if (typeof last === "object") {
if (last.content === undefined) {
last.content = [];
}
Expand All @@ -118,7 +118,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
return;
}

if (typeof last === 'object') {
if (typeof last === "object") {
if (last.content === undefined) {
last.content = [];
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
if (options.sourceLocations) {
buf.location = {
start: locationTracker.getPosition(parser.startIndex),
end: locationTracker.getPosition(parser.endIndex)
end: locationTracker.getPosition(parser.endIndex),
};
lastOpenTagEndIndex = parser.endIndex;
}
Expand All @@ -165,7 +165,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
function onclosetag(name: string, isImplied: boolean) {
const buf: Node | undefined = bufArray.pop();

if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) {
if (buf && typeof buf === "object" && buf.location && parser.endIndex !== null) {
if (!isImplied) {
buf.location.end = locationTracker.getPosition(parser.endIndex);
} else if (lastOpenTagEndIndex < parser.startIndex) {
Expand All @@ -181,7 +181,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
return;
}

if (typeof last === 'object') {
if (typeof last === "object") {
if (last.content === undefined) {
last.content = [];
}
Expand All @@ -201,10 +201,10 @@ export const parser = (html: string, options: Options = {}): Node[] => {
return;
}

if (typeof last === 'object') {
if (typeof last === "object") {
if (last.content && Array.isArray(last.content) && last.content.length > 0) {
const lastContentNode = last.content[last.content.length - 1];
if (typeof lastContentNode === 'string' && !lastContentNode.startsWith('<!--')) {
if (typeof lastContentNode === "string" && !lastContentNode.startsWith("<!--")) {
last.content[last.content.length - 1] = `${lastContentNode}${text}`;
return;
}
Expand All @@ -220,14 +220,17 @@ export const parser = (html: string, options: Options = {}): Node[] => {
}
}

const parser = new Parser({
onprocessinginstruction,
oncomment,
onattribute,
onopentag,
onclosetag,
ontext
}, { ...defaultOptions, ...options });
const parser = new Parser(
{
onprocessinginstruction,
oncomment,
onattribute,
onopentag,
onclosetag,
ontext,
},
{ ...defaultOptions, ...options },
);

parser.write(html);
parser.end();
Expand Down
6 changes: 3 additions & 3 deletions src/location-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class LocationTracker {
this.source = source;
this.lastPosition = {
line: 1,
column: 1
column: 1,
};

this.lastIndex = 0;
}

getPosition(index: number): Position {
if (index < this.lastIndex) {
throw new Error('Source indices must be monotonic');
throw new Error("Source indices must be monotonic");
}

while (this.lastIndex < index) {
Expand All @@ -41,7 +41,7 @@ export class LocationTracker {

return {
line: this.lastPosition.line,
column: this.lastPosition.column
column: this.lastPosition.column,
};
}
}

0 comments on commit b98a3e0

Please sign in to comment.