Skip to content

Commit

Permalink
refactor: Improve code quality (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Mar 7, 2022
1 parent a2c7240 commit d81782f
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 29 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"unicorn/prefer-string-slice": "off",
"unicorn/prefer-code-point": "off",
"unicorn/no-array-push-push": "off",
"unicorn/no-array-reduce": "off",
"unicorn/no-for-loop": "off",
"unicorn/consistent-destructuring": "off",
"unicorn/prefer-switch": ["error", { "emptyDefaultCase": "do-nothing-comment" }]
Expand Down
13 changes: 8 additions & 5 deletions packages/parse5-html-rewriting-stream/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ export class RewritingStream extends SAXParser {

/** Emits a serialized start tag token into the output stream. */
public emitStartTag(token: StartTag): void {
const res = token.attrs.reduce(
(res, attr) => `${res} ${attr.name}="${escapeString(attr.value, true)}"`,
`<${token.tagName}`
);
let res = `<${token.tagName}`;

for (const attr of token.attrs) {
res += ` ${attr.name}="${escapeString(attr.value, true)}"`;
}

this.push(res + (token.selfClosing ? '/>' : '>'));
res += token.selfClosing ? '/>' : '>';

this.push(res);
}

/** Emits a serialized end tag token into the output stream. */
Expand Down
2 changes: 1 addition & 1 deletion packages/parse5-parser-stream/test/location-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { generateLocationInfoParserTests } from 'parse5-test-utils/utils/generat
import { generateTestsForEachTreeAdapter } from 'parse5-test-utils/utils/common.js';
import { parseChunked } from './utils/parse-chunked.js';

generateLocationInfoParserTests('location-info', 'ParserStream', (input, opts) =>
generateLocationInfoParserTests('location-info', (input, opts) =>
// NOTE: because of performance use bigger chunks here
parseChunked(input, opts, 100, 400)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ParserFeedbackSimulator } from '../lib/parser-feedback-simulator.js';
const feedbackPath = new URL('../../../test/data/parser-feedback', import.meta.url);

generateTokenizationTests(
'ParserFeedbackSimulator',
'ParserFeedbackSimulator',
feedbackPath.pathname,
(handler) => new ParserFeedbackSimulator({}, handler).tokenizer
Expand Down
7 changes: 3 additions & 4 deletions packages/parse5-sax-parser/test/sax-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ function createBasicTest(html: string, expected: string, options?: SAXParserOpti

parser.on('startTag', ({ tagName, attrs, selfClosing }) => {
actual += `<${tagName}`;
actual += attrs.reduce(
(res: string, attr: { name: string; value: string }) => `${res} ${attr.name}="${attr.value}"`,
''
);
for (const attr of attrs) {
actual += ` ${attr.name}="${attr.value}"`;
}
actual += selfClosing ? '/>' : '>';
});

Expand Down
4 changes: 2 additions & 2 deletions packages/parse5/lib/common/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export interface Location {
startOffset: number;
/** One-based line index of the last character. */
endLine: number;
/** One-based column index of the last character. */
/** One-based column index of the last character. Points directly *after* the last character. */
endCol: number;
/** Zero-based last character index. */
/** Zero-based last character index. Points directly *after* the last character. */
endOffset: number;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/parse5/lib/parser/parser-location-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/interface.js';

generateLocationInfoParserTests(
'location-info-parser',
'Parser',
(input: string, opts: parse5.ParserOptions<TreeAdapterTypeMap>) => ({
node: parse5.parse(input, opts),
})
Expand All @@ -28,8 +27,9 @@ generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => {

const fragment = parse5.parseFragment(html, opts);
const firstP = treeAdapter.getChildNodes(fragment)[0];
const firstPLocation = treeAdapter.getNodeSourceCodeLocation(firstP)!;
const firstPLocation = treeAdapter.getNodeSourceCodeLocation(firstP);

assert.ok(firstPLocation);
assert.strictEqual(html.substring(firstPLocation.startOffset, firstPLocation.endOffset), '<p>1');
});

Expand Down
7 changes: 1 addition & 6 deletions packages/parse5/lib/tokenizer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ const tokenizerOpts = {
sourceCodeLocationInfo: true,
};

generateTokenizationTests(
'tokenizer',
'Tokenizer',
dataPath.pathname,
(handler) => new Tokenizer(tokenizerOpts, handler)
);
generateTokenizationTests('Tokenizer', dataPath.pathname, (handler) => new Tokenizer(tokenizerOpts, handler));

describe('tokenizer', () => {
it('Regression - `<<` in comment parses correctly (GH-325)', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function makeChunks(str: string, minSize?: number, maxSize?: number): str
const chunks = [];
let start = 0;

// NOTE: add 1 as well, so we avoid situation when we have just one huge chunk
let end = Math.min(getRandomChunkSize(minSize, maxSize), str.length, 1);
// NOTE: start with 1, so we avoid situation when we have just one huge chunk
let end = 1;

while (start < str.length) {
chunks.push(str.substring(start, end));
Expand Down
1 change: 0 additions & 1 deletion test/utils/generate-location-info-parser-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ function loadParserLocationInfoTestData(): { name: string; data: string }[] {

export function generateLocationInfoParserTests(
name: string,
_prefix: string,
parse: (html: string, opts: ParserOptions<TreeAdapterTypeMap>) => { node: TreeAdapterTypeMap['node'] }
): void {
generateTestsForEachTreeAdapter(name, (treeAdapter) => {
Expand Down
1 change: 0 additions & 1 deletion test/utils/generate-tokenization-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ function loadTests(dataDirPath: string): LoadedTest[] {
}

export function generateTokenizationTests(
_name: string,
prefix: string,
testSuite: string,
createTokenSource: TokenSourceCreator
Expand Down
6 changes: 3 additions & 3 deletions test/utils/parse-dat-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface DatFile<T extends TreeAdapterTypeMap> {
export function parseDatFile<T extends TreeAdapterTypeMap>(testSet: string, treeAdapter: TreeAdapter<T>): DatFile<T>[] {
const testDescrs: Record<string, number | string[]>[] = [];
let curDirective = '';
let curDescr: Record<string, number | string[]> | null = null;
let curDescr: Record<string, number | string[]> = {};

for (const [idx, line] of testSet.split(/\r?\n/).entries()) {
if (line === '#data') {
Expand All @@ -49,9 +49,9 @@ export function parseDatFile<T extends TreeAdapterTypeMap>(testSet: string, tree

if (line[0] === '#') {
curDirective = line;
curDescr![curDirective] = [];
curDescr[curDirective] = [];
} else {
(curDescr![curDirective] as string[]).push(line);
(curDescr[curDirective] as string[]).push(line);
}
}

Expand Down

0 comments on commit d81782f

Please sign in to comment.