summaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorGravatar Drew Powers <1369770+drwpow@users.noreply.github.com> 2021-04-01 10:25:28 -0600
committerGravatar GitHub <noreply@github.com> 2021-04-01 10:25:28 -0600
commitc26c244ca2634d462616d6cf71072fbe26becba2 (patch)
tree1bd5e83eff9ca88200aacb272c84439f192015ec /src/parser
parentf6a7ac67befff863e34133673efb78ea7ac0fe48 (diff)
downloadastro-c26c244ca2634d462616d6cf71072fbe26becba2.tar.gz
astro-c26c244ca2634d462616d6cf71072fbe26becba2.tar.zst
astro-c26c244ca2634d462616d6cf71072fbe26becba2.zip
Annoying Lint PR #2 (#47)
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/Stats.ts1
-rw-r--r--src/parser/parse/index.ts5
-rw-r--r--src/parser/utils/error.ts19
-rw-r--r--src/parser/utils/full_char_code_at.ts1
-rw-r--r--src/parser/utils/fuzzymatch.ts18
-rw-r--r--src/parser/utils/get_code_frame.ts2
-rw-r--r--src/parser/utils/list.ts1
-rw-r--r--src/parser/utils/names.ts3
-rw-r--r--src/parser/utils/nodes_match.ts1
-rw-r--r--src/parser/utils/trim.ts2
10 files changed, 37 insertions, 16 deletions
diff --git a/src/parser/Stats.ts b/src/parser/Stats.ts
index 33802a42b..5f7f991f8 100644
--- a/src/parser/Stats.ts
+++ b/src/parser/Stats.ts
@@ -15,6 +15,7 @@ interface Timing {
children: Timing[];
}
+/** Format benchmarks */
function collapse_timings(timings) {
const result = {};
timings.forEach((timing) => {
diff --git a/src/parser/parse/index.ts b/src/parser/parse/index.ts
index 052cf0317..124e125ef 100644
--- a/src/parser/parse/index.ts
+++ b/src/parser/parse/index.ts
@@ -217,6 +217,11 @@ export class Parser {
}
}
+/**
+ * Parse
+ * Step 1/3 in Astro SSR.
+ * This is the first pass over .astro files and the step at which we convert a string to an AST for us to crawl.
+ */
export default function parse(template: string, options: ParserOptions = {}): Ast {
const parser = new Parser(template, options);
diff --git a/src/parser/utils/error.ts b/src/parser/utils/error.ts
index 3c1b23e4c..8ebb5b093 100644
--- a/src/parser/utils/error.ts
+++ b/src/parser/utils/error.ts
@@ -16,6 +16,7 @@ export class CompileError extends Error {
}
}
+/** Throw CompileError */
export default function error(
message: string,
props: {
@@ -27,19 +28,19 @@ export default function error(
end?: number;
}
): never {
- const error = new CompileError(message);
- error.name = props.name;
+ const err = new CompileError(message);
+ err.name = props.name;
const start = locate(props.source, props.start, { offsetLine: 1 });
const end = locate(props.source, props.end || props.start, { offsetLine: 1 });
- error.code = props.code;
- error.start = start;
- error.end = end;
- error.pos = props.start;
- error.filename = props.filename;
+ err.code = props.code;
+ err.start = start;
+ err.end = end;
+ err.pos = props.start;
+ err.filename = props.filename;
- error.frame = get_code_frame(props.source, start.line - 1, start.column);
+ err.frame = get_code_frame(props.source, start.line - 1, start.column);
- throw error;
+ throw err;
}
diff --git a/src/parser/utils/full_char_code_at.ts b/src/parser/utils/full_char_code_at.ts
index fea5151b6..b62b2c77a 100644
--- a/src/parser/utils/full_char_code_at.ts
+++ b/src/parser/utils/full_char_code_at.ts
@@ -1,6 +1,7 @@
// Adapted from https://github.com/acornjs/acorn/blob/6584815dca7440e00de841d1dad152302fdd7ca5/src/tokenize.js
// Reproduced under MIT License https://github.com/acornjs/acorn/blob/master/LICENSE
+/** @url https://github.com/acornjs/acorn/blob/6584815dca7440e00de841d1dad152302fdd7ca5/src/tokenize.js */
export default function full_char_code_at(str: string, i: number): number {
const code = str.charCodeAt(i);
if (code <= 0xd7ff || code >= 0xe000) return code;
diff --git a/src/parser/utils/fuzzymatch.ts b/src/parser/utils/fuzzymatch.ts
index d24d0fd0a..4d17aafdf 100644
--- a/src/parser/utils/fuzzymatch.ts
+++ b/src/parser/utils/fuzzymatch.ts
@@ -1,5 +1,6 @@
// @ts-nocheck
+/** Utility for accessing FuzzySet */
export default function fuzzymatch(name: string, names: string[]) {
const set = new FuzzySet(names);
const matches = set.get(name);
@@ -13,7 +14,7 @@ export default function fuzzymatch(name: string, names: string[]) {
const GRAM_SIZE_LOWER = 2;
const GRAM_SIZE_UPPER = 3;
-// return an edit distance from 0 to 1
+/** Return an edit distance from 0 to 1 */
function _distance(str1: string, str2: string) {
if (str1 === null && str2 === null) {
throw 'Trying to compare two null values';
@@ -30,7 +31,7 @@ function _distance(str1: string, str2: string) {
}
}
-// helper functions
+/** @url https://github.com/Glench/fuzzyset.js/blob/master/lib/fuzzyset.js#L18 */
function levenshtein(str1: string, str2: string) {
const current: number[] = [];
let prev;
@@ -58,6 +59,7 @@ function levenshtein(str1: string, str2: string) {
const non_word_regex = /[^\w, ]+/;
+/** @url https://github.com/Glench/fuzzyset.js/blob/master/lib/fuzzyset.js#L53 */
function iterate_grams(value: string, gram_size = 2) {
const simplified = '-' + value.toLowerCase().replace(non_word_regex, '') + '-';
const len_diff = gram_size - simplified.length;
@@ -74,6 +76,7 @@ function iterate_grams(value: string, gram_size = 2) {
return results;
}
+/** @url https://github.com/Glench/fuzzyset.js/blob/master/lib/fuzzyset.js#L69 */
function gram_counter(value: string, gram_size = 2) {
// return an object where key=gram, value=number of occurrences
const result = {};
@@ -90,6 +93,7 @@ function gram_counter(value: string, gram_size = 2) {
return result;
}
+/** @url https://github.com/Glench/fuzzyset.js/blob/master/lib/fuzzyset.js#L158 */
function sort_descending(a, b) {
return b[0] - a[0];
}
@@ -211,16 +215,16 @@ class FuzzySet {
let new_results = [];
const end_index = Math.min(50, results.length);
// truncate somewhat arbitrarily to 50
- for (let i = 0; i < end_index; ++i) {
- new_results.push([_distance(results[i][1], normalized_value), results[i][1]]);
+ for (let j = 0; j < end_index; ++j) {
+ new_results.push([_distance(results[j][1], normalized_value), results[j][1]]);
}
results = new_results;
results.sort(sort_descending);
new_results = [];
- for (let i = 0; i < results.length; ++i) {
- if (results[i][0] == results[0][0]) {
- new_results.push([results[i][0], this.exact_set[results[i][1]]]);
+ for (let j = 0; j < results.length; ++j) {
+ if (results[j][0] == results[0][0]) {
+ new_results.push([results[j][0], this.exact_set[results[j][1]]]);
}
}
diff --git a/src/parser/utils/get_code_frame.ts b/src/parser/utils/get_code_frame.ts
index a0c296672..e4f1834fd 100644
--- a/src/parser/utils/get_code_frame.ts
+++ b/src/parser/utils/get_code_frame.ts
@@ -1,7 +1,9 @@
+/** Die you stupid tabs */
function tabs_to_spaces(str: string) {
return str.replace(/^\t+/, (match) => match.split('\t').join(' '));
}
+/** Display syntax error in pretty format in logs */
export default function get_code_frame(source: string, line: number, column: number) {
const lines = source.split('\n');
diff --git a/src/parser/utils/list.ts b/src/parser/utils/list.ts
index ba1ef9f4c..9388adb14 100644
--- a/src/parser/utils/list.ts
+++ b/src/parser/utils/list.ts
@@ -1,3 +1,4 @@
+/** Display an array of strings in a human-readable format */
export default function list(items: string[], conjunction = 'or') {
if (items.length === 1) return items[0];
return `${items.slice(0, -1).join(', ')} ${conjunction} ${items[items.length - 1]}`;
diff --git a/src/parser/utils/names.ts b/src/parser/utils/names.ts
index f2e1dfc8e..f041d20ce 100644
--- a/src/parser/utils/names.ts
+++ b/src/parser/utils/names.ts
@@ -113,10 +113,12 @@ export const reserved = new Set([
const void_element_names = /^(?:area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/;
+/** Is this a void HTML element? */
export function is_void(name: string) {
return void_element_names.test(name) || name.toLowerCase() === '!doctype';
}
+/** Is this a valid HTML element? */
export function is_valid(str: string): boolean {
let i = 0;
@@ -130,6 +132,7 @@ export function is_valid(str: string): boolean {
return true;
}
+/** Utility to normalize HTML */
export function sanitize(name: string) {
return name
.replace(/[^a-zA-Z0-9_]+/g, '_')
diff --git a/src/parser/utils/nodes_match.ts b/src/parser/utils/nodes_match.ts
index 563742635..7e4093994 100644
--- a/src/parser/utils/nodes_match.ts
+++ b/src/parser/utils/nodes_match.ts
@@ -1,5 +1,6 @@
// @ts-nocheck
+/** Compare two TemplateNodes to determine if they are equivalent */
export function nodes_match(a, b) {
if (!!a !== !!b) return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
diff --git a/src/parser/utils/trim.ts b/src/parser/utils/trim.ts
index 406a8c97f..480cc99a8 100644
--- a/src/parser/utils/trim.ts
+++ b/src/parser/utils/trim.ts
@@ -1,5 +1,6 @@
import { whitespace } from './patterns.js';
+/** Trim whitespace from start of string */
export function trim_start(str: string) {
let i = 0;
while (whitespace.test(str[i])) i += 1;
@@ -7,6 +8,7 @@ export function trim_start(str: string) {
return str.slice(i);
}
+/** Trim whitespace from end of string */
export function trim_end(str: string) {
let i = str.length;
while (whitespace.test(str[i - 1])) i -= 1;