[ci] npm run format

This commit is contained in:
matthewp 2021-04-09 18:09:44 +00:00 committed by GitHub Actions
parent ad9c3b1d8d
commit 62924b3162
5 changed files with 44 additions and 47 deletions

View file

@ -94,7 +94,7 @@ function getAttributes(attrs: Attribute[]): Record<string, string> {
/** Get value from a TemplateNode Attribute (text attributes only!) */ /** Get value from a TemplateNode Attribute (text attributes only!) */
function getTextFromAttribute(attr: any): string { function getTextFromAttribute(attr: any): string {
switch(attr.type) { switch (attr.type) {
case 'Text': { case 'Text': {
if (attr.raw !== undefined) { if (attr.raw !== undefined) {
return attr.raw; return attr.raw;
@ -479,7 +479,7 @@ function compileHtml(enterNode: TemplateNode, state: CodegenState, compileOption
switch (node.type) { switch (node.type) {
case 'Expression': { case 'Expression': {
let child = ''; let child = '';
if(node.children!.length) { if (node.children!.length) {
child = compileHtml(node.children![0], state, compileOptions); child = compileHtml(node.children![0], state, compileOptions);
} }
let raw = node.codeStart + child + node.codeEnd; let raw = node.codeStart + child + node.codeEnd;

View file

@ -65,8 +65,8 @@ export default function (module: Script): Transformer {
type: 'Expression', type: 'Expression',
codeStart: '`' + escape(code) + '`', codeStart: '`' + escape(code) + '`',
codeEnd: '', codeEnd: '',
children: [] children: [],
} },
}, },
], ],
}, },

View file

@ -1,4 +1,3 @@
import type { BaseNode, Expression } from '../../interfaces'; import type { BaseNode, Expression } from '../../interfaces';
import { Parser } from '../index.js'; import { Parser } from '../index.js';
import parseAstro from '../index.js'; import parseAstro from '../index.js';
@ -20,11 +19,11 @@ function peek_nonwhitespace(state: ParseState) {
let index = state.index; let index = state.index;
do { do {
let char = state.source[index]; let char = state.source[index];
if(!/\s/.test(char)) { if (!/\s/.test(char)) {
return char; return char;
} }
index++; index++;
} while(index < state.source.length); } while (index < state.source.length);
} }
function next_char(state: ParseState) { function next_char(state: ParseState) {
@ -40,38 +39,36 @@ function consume_string(state: ParseState, stringChar: string) {
do { do {
const char = next_char(state); const char = next_char(state);
if(inEscape) { if (inEscape) {
inEscape = false; inEscape = false;
} else if(char === '\\') { } else if (char === '\\') {
inEscape = true; inEscape = true;
} else if(char === stringChar) { } else if (char === stringChar) {
break; break;
} }
} while(in_bounds(state)); } while (in_bounds(state));
} }
function consume_multiline_comment(state: ParseState) { function consume_multiline_comment(state: ParseState) {
do { do {
const char = next_char(state); const char = next_char(state);
if(char === '*' && peek_char(state) === '/') { if (char === '*' && peek_char(state) === '/') {
break; break;
} }
} while(in_bounds(state)); } while (in_bounds(state));
} }
function consume_line_comment(state: ParseState) { function consume_line_comment(state: ParseState) {
do { do {
const char = next_char(state); const char = next_char(state);
if(char === '\n') { if (char === '\n') {
break; break;
} }
} while(in_bounds(state)); } while (in_bounds(state));
} }
const voidElements = new Set(['area', 'base', 'br', 'col', 'command', 'embed', const voidElements = new Set(['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source',
'track', 'wbr']);
function consume_tag(state: ParseState) { function consume_tag(state: ParseState) {
const start = state.index - 1; const start = state.index - 1;
@ -85,8 +82,8 @@ function consume_tag(state: ParseState) {
do { do {
const char = next_char(state); const char = next_char(state);
switch(char) { switch (char) {
case '\'': case "'":
case '"': { case '"': {
consume_string(state, char); consume_string(state, char);
break; break;
@ -95,7 +92,7 @@ function consume_tag(state: ParseState) {
inTag = false; inTag = false;
tagName = ''; tagName = '';
if(peek_nonwhitespace(state) === '/') { if (peek_nonwhitespace(state) === '/') {
inClose = true; inClose = true;
bracketIndex--; bracketIndex--;
} else { } else {
@ -106,7 +103,7 @@ function consume_tag(state: ParseState) {
} }
case '>': { case '>': {
// An arrow function, probably // An arrow function, probably
if(!inStart && !inClose) { if (!inStart && !inClose) {
break; break;
} }
@ -120,7 +117,7 @@ function consume_tag(state: ParseState) {
// If we're in a start tag, we expect to find 2 more brackets // If we're in a start tag, we expect to find 2 more brackets
!inClose; !inClose;
if(addExpectedBrackets) { if (addExpectedBrackets) {
bracketIndex += 2; bracketIndex += 2;
} }
@ -135,13 +132,13 @@ function consume_tag(state: ParseState) {
break; break;
} }
case '/': { case '/': {
if(inStart) { if (inStart) {
selfClosed = true; selfClosed = true;
} }
break; break;
} }
default: { default: {
if(!inTag) { if (!inTag) {
tagName += char; tagName += char;
} }
break; break;
@ -149,18 +146,17 @@ function consume_tag(state: ParseState) {
} }
// Unclosed tags // Unclosed tags
if(state.curlyCount <= 0) { if (state.curlyCount <= 0) {
break; break;
} }
if(bracketIndex === 0) { if (bracketIndex === 0) {
break; break;
} }
} while(in_bounds(state)); } while (in_bounds(state));
const source = state.source.substring(start, state.index); const source = state.source.substring(start, state.index);
const ast = parseAstro(source); const ast = parseAstro(source);
const fragment = ast.html; const fragment = ast.html;
@ -174,21 +170,23 @@ function consume_expression(source: string, start: number): Expression {
end: Number.NaN, end: Number.NaN,
codeStart: '', codeStart: '',
codeEnd: '', codeEnd: '',
children: [] children: [],
}; };
let codeEndStart: number = 0; let codeEndStart: number = 0;
const state: ParseState = { const state: ParseState = {
source, start, index: start, source,
start,
index: start,
curlyCount: 1, curlyCount: 1,
bracketCount: 0, bracketCount: 0,
root: expr root: expr,
}; };
do { do {
const char = next_char(state); const char = next_char(state);
switch(char) { switch (char) {
case '{': { case '{': {
state.curlyCount++; state.curlyCount++;
break; break;
@ -204,14 +202,14 @@ function consume_expression(source: string, start: number): Expression {
codeEndStart = state.index; codeEndStart = state.index;
break; break;
} }
case '\'': case "'":
case '"': case '"':
case '`': { case '`': {
consume_string(state, char); consume_string(state, char);
break; break;
} }
case '/': { case '/': {
switch(peek_char(state)) { switch (peek_char(state)) {
case '/': { case '/': {
consume_line_comment(state); consume_line_comment(state);
break; break;
@ -223,11 +221,11 @@ function consume_expression(source: string, start: number): Expression {
} }
} }
} }
} while(in_bounds(state) && state.curlyCount > 0); } while (in_bounds(state) && state.curlyCount > 0);
expr.end = state.index - 1; expr.end = state.index - 1;
if(codeEndStart) { if (codeEndStart) {
expr.codeEnd = source.substring(codeEndStart, expr.end); expr.codeEnd = source.substring(codeEndStart, expr.end);
} else { } else {
expr.codeStart = source.substring(start, expr.end); expr.codeStart = source.substring(start, expr.end);

View file

@ -4,7 +4,6 @@ import type { Node } from 'estree';
import { Parser } from '../index.js'; import { Parser } from '../index.js';
import { Script } from '../../interfaces.js'; import { Script } from '../../interfaces.js';
const script_closing_tag = '</script>'; const script_closing_tag = '</script>';
function get_context(parser: Parser, attributes: any[], start: number): 'runtime' | 'setup' { function get_context(parser: Parser, attributes: any[], start: number): 'runtime' | 'setup' {

View file

@ -11,10 +11,10 @@ Expressions('Can load page', async ({ runtime }) => {
const result = await runtime.load('/'); const result = await runtime.load('/');
assert.equal(result.statusCode, 200); assert.equal(result.statusCode, 200);
const $ = doc(result.contents); const $ = doc(result.contents);
for(let col of ['red', 'yellow', 'blue']) { for (let col of ['red', 'yellow', 'blue']) {
assert.equal($('#' + col).length, 1); assert.equal($('#' + col).length, 1);
} }
}); });
@ -23,10 +23,10 @@ Expressions('Ignores characters inside of strings', async ({ runtime }) => {
const result = await runtime.load('/strings'); const result = await runtime.load('/strings');
assert.equal(result.statusCode, 200); assert.equal(result.statusCode, 200);
const $ = doc(result.contents); const $ = doc(result.contents);
for(let col of ['red', 'yellow', 'blue']) { for (let col of ['red', 'yellow', 'blue']) {
assert.equal($('#' + col).length, 1); assert.equal($('#' + col).length, 1);
} }
}); });
@ -34,10 +34,10 @@ Expressions('Ignores characters inside of strings', async ({ runtime }) => {
Expressions('Ignores characters inside of line comments', async ({ runtime }) => { Expressions('Ignores characters inside of line comments', async ({ runtime }) => {
const result = await runtime.load('/line-comments'); const result = await runtime.load('/line-comments');
assert.equal(result.statusCode, 200); assert.equal(result.statusCode, 200);
const $ = doc(result.contents); const $ = doc(result.contents);
for(let col of ['red', 'yellow', 'blue']) { for (let col of ['red', 'yellow', 'blue']) {
assert.equal($('#' + col).length, 1); assert.equal($('#' + col).length, 1);
} }
}); });
@ -45,10 +45,10 @@ Expressions('Ignores characters inside of line comments', async ({ runtime }) =>
Expressions('Ignores characters inside of multiline comments', async ({ runtime }) => { Expressions('Ignores characters inside of multiline comments', async ({ runtime }) => {
const result = await runtime.load('/multiline-comments'); const result = await runtime.load('/multiline-comments');
assert.equal(result.statusCode, 200); assert.equal(result.statusCode, 200);
const $ = doc(result.contents); const $ = doc(result.contents);
for(let col of ['red', 'yellow', 'blue']) { for (let col of ['red', 'yellow', 'blue']) {
assert.equal($('#' + col).length, 1); assert.equal($('#' + col).length, 1);
} }
}); });