summaryrefslogtreecommitdiff
path: root/packages/astro-parser/src/parse/read
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-04-30 16:33:35 -0500
committerGravatar GitHub <noreply@github.com> 2021-04-30 16:33:35 -0500
commit4df1347156cf2632ea2f3475d3a5f8f08d197cc3 (patch)
tree9d50de89dfe62827c32a8a4046120af4ab61dc0c /packages/astro-parser/src/parse/read
parent1d498facc8f78a3ffbfecd05cc6ecd45e8a4a1ae (diff)
downloadastro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.gz
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.zst
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.zip
Migrate to `yarn` monorepo (#157)
* chore: use monorepo * chore: scaffold astro-scripts * chore: move tests inside packages/astro * chore: refactor tests, add scripts * chore: move parser to own module * chore: move runtime to packages/astro * fix: move parser to own package * test: fix prettier-plugin-astro tests * fix: tests * chore: update package-lock * chore: add changesets * fix: cleanup examples * fix: starter example * chore: update changeset config * chore: update changeset config * chore: setup changeset release workflow * chore: bump lockfiles * chore: prism => astro-prism * fix: tsc --emitDeclarationOnly * chore: final cleanup, switch to yarn * chore: add lerna * chore: update workflows to yarn * chore: update workflows * chore: remove lint workflow * chore: add astro-dev script * chore: add symlinked README
Diffstat (limited to 'packages/astro-parser/src/parse/read')
-rw-r--r--packages/astro-parser/src/parse/read/context.ts72
-rw-r--r--packages/astro-parser/src/parse/read/expression.ts251
-rw-r--r--packages/astro-parser/src/parse/read/script.ts60
-rw-r--r--packages/astro-parser/src/parse/read/style.ts40
4 files changed, 423 insertions, 0 deletions
diff --git a/packages/astro-parser/src/parse/read/context.ts b/packages/astro-parser/src/parse/read/context.ts
new file mode 100644
index 000000000..565c66d18
--- /dev/null
+++ b/packages/astro-parser/src/parse/read/context.ts
@@ -0,0 +1,72 @@
+// @ts-nocheck
+
+import { Parser } from '../index.js';
+import { isIdentifierStart } from 'acorn';
+import full_char_code_at from '../../utils/full_char_code_at.js';
+import { is_bracket_open, is_bracket_close, is_bracket_pair, get_bracket_close } from '../utils/bracket.js';
+import { parse_expression_at } from './expression.js';
+import { Pattern } from 'estree';
+
+export default function read_context(parser: Parser): Pattern & { start: number; end: number } {
+ const start = parser.index;
+ let i = parser.index;
+
+ const code = full_char_code_at(parser.template, i);
+ if (isIdentifierStart(code, true)) {
+ return {
+ type: 'Identifier',
+ name: parser.read_identifier(),
+ start,
+ end: parser.index,
+ };
+ }
+
+ if (!is_bracket_open(code)) {
+ parser.error({
+ code: 'unexpected-token',
+ message: 'Expected identifier or destructure pattern',
+ });
+ }
+
+ const bracket_stack = [code];
+ i += code <= 0xffff ? 1 : 2;
+
+ while (i < parser.template.length) {
+ const code = full_char_code_at(parser.template, i);
+ if (is_bracket_open(code)) {
+ bracket_stack.push(code);
+ } else if (is_bracket_close(code)) {
+ if (!is_bracket_pair(bracket_stack[bracket_stack.length - 1], code)) {
+ parser.error({
+ code: 'unexpected-token',
+ message: `Expected ${String.fromCharCode(get_bracket_close(bracket_stack[bracket_stack.length - 1]))}`,
+ });
+ }
+ bracket_stack.pop();
+ if (bracket_stack.length === 0) {
+ i += code <= 0xffff ? 1 : 2;
+ break;
+ }
+ }
+ i += code <= 0xffff ? 1 : 2;
+ }
+
+ parser.index = i;
+
+ const pattern_string = parser.template.slice(start, i);
+ try {
+ // the length of the `space_with_newline` has to be start - 1
+ // because we added a `(` in front of the pattern_string,
+ // which shifted the entire string to right by 1
+ // so we offset it by removing 1 character in the `space_with_newline`
+ // to achieve that, we remove the 1st space encountered,
+ // so it will not affect the `column` of the node
+ let space_with_newline = parser.template.slice(0, start).replace(/[^\n]/g, ' ');
+ const first_space = space_with_newline.indexOf(' ');
+ space_with_newline = space_with_newline.slice(0, first_space) + space_with_newline.slice(first_space + 1);
+
+ return (parse_expression_at(`${space_with_newline}(${pattern_string} = 1)`, start - 1) as any).left;
+ } catch (error) {
+ parser.acorn_error(error);
+ }
+}
diff --git a/packages/astro-parser/src/parse/read/expression.ts b/packages/astro-parser/src/parse/read/expression.ts
new file mode 100644
index 000000000..9d0d09175
--- /dev/null
+++ b/packages/astro-parser/src/parse/read/expression.ts
@@ -0,0 +1,251 @@
+import type { BaseNode, Expression } from '../../interfaces';
+import { Parser } from '../index.js';
+import parseAstro from '../index.js';
+
+interface ParseState {
+ source: string;
+ start: number;
+ index: number;
+ curlyCount: number;
+ bracketCount: number;
+ root: Expression;
+}
+
+function peek_char(state: ParseState) {
+ return state.source[state.index];
+}
+
+function peek_nonwhitespace(state: ParseState) {
+ let index = state.index;
+ do {
+ let char = state.source[index];
+ if (!/\s/.test(char)) {
+ return char;
+ }
+ index++;
+ } while (index < state.source.length);
+}
+
+function next_char(state: ParseState) {
+ return state.source[state.index++];
+}
+
+function in_bounds(state: ParseState) {
+ return state.index < state.source.length;
+}
+
+function consume_string(state: ParseState, stringChar: string) {
+ let inEscape;
+ do {
+ const char = next_char(state);
+
+ if (inEscape) {
+ inEscape = false;
+ } else if (char === '\\') {
+ inEscape = true;
+ } else if (char === stringChar) {
+ break;
+ }
+ } while (in_bounds(state));
+}
+
+function consume_multiline_comment(state: ParseState) {
+ do {
+ const char = next_char(state);
+
+ if (char === '*' && peek_char(state) === '/') {
+ break;
+ }
+ } while (in_bounds(state));
+}
+
+function consume_line_comment(state: ParseState) {
+ do {
+ const char = next_char(state);
+ if (char === '\n') {
+ break;
+ }
+ } while (in_bounds(state));
+}
+
+const voidElements = new Set(['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
+
+function consume_tag(state: ParseState) {
+ const start = state.index - 1;
+ let tagName = '';
+ let inTag = false;
+ let inStart = true;
+ let selfClosed = false;
+ let inClose = false;
+
+ let bracketIndex = 1;
+ do {
+ const char = next_char(state);
+
+ switch (char) {
+ case "'":
+ case '"': {
+ consume_string(state, char);
+ break;
+ }
+ case '<': {
+ inTag = false;
+ tagName = '';
+
+ if (peek_nonwhitespace(state) === '/') {
+ inClose = true;
+ bracketIndex--;
+ } else {
+ inStart = true;
+ bracketIndex++;
+ }
+ break;
+ }
+ case '>': {
+ // An arrow function, probably
+ if (!inStart && !inClose) {
+ break;
+ }
+
+ bracketIndex--;
+
+ const addExpectedBrackets =
+ // Void elements don't need a closing
+ !voidElements.has(tagName.toLowerCase()) &&
+ // Self-closing don't need a closing
+ !selfClosed &&
+ // If we're in a start tag, we expect to find 2 more brackets
+ !inClose;
+
+ if (addExpectedBrackets) {
+ bracketIndex += 2;
+ }
+
+ inTag = false;
+ selfClosed = false;
+ inStart = false;
+ inClose = false;
+ break;
+ }
+ case ' ': {
+ inTag = true;
+ break;
+ }
+ case '/': {
+ if (inStart) {
+ selfClosed = true;
+ }
+ break;
+ }
+ default: {
+ if (!inTag) {
+ tagName += char;
+ }
+ break;
+ }
+ }
+
+ // Unclosed tags
+ if (state.curlyCount <= 0) {
+ break;
+ }
+
+ if (bracketIndex === 0) {
+ break;
+ }
+ } while (in_bounds(state));
+
+ const source = state.source.substring(start, state.index);
+
+ const ast = parseAstro(source);
+ const fragment = ast.html;
+
+ return fragment;
+}
+
+function consume_expression(source: string, start: number): Expression {
+ const expr: Expression = {
+ type: 'Expression',
+ start,
+ end: Number.NaN,
+ codeChunks: [],
+ children: [],
+ };
+
+ let codeStart: number = start;
+
+ const state: ParseState = {
+ source,
+ start,
+ index: start,
+ curlyCount: 1,
+ bracketCount: 0,
+ root: expr,
+ };
+
+ do {
+ const char = next_char(state);
+
+ switch (char) {
+ case '{': {
+ state.curlyCount++;
+ break;
+ }
+ case '}': {
+ state.curlyCount--;
+ break;
+ }
+ case '<': {
+ const chunk = source.substring(codeStart, state.index - 1);
+ expr.codeChunks.push(chunk);
+ const tag = consume_tag(state);
+ expr.children.push(tag);
+ codeStart = state.index;
+ break;
+ }
+ case "'":
+ case '"':
+ case '`': {
+ consume_string(state, char);
+ break;
+ }
+ case '/': {
+ switch (peek_char(state)) {
+ case '/': {
+ consume_line_comment(state);
+ break;
+ }
+ case '*': {
+ consume_multiline_comment(state);
+ break;
+ }
+ }
+ }
+ }
+ } while (in_bounds(state) && state.curlyCount > 0);
+
+ expr.end = state.index - 1;
+
+ if (expr.children.length || !expr.codeChunks.length) {
+ expr.codeChunks.push(source.substring(codeStart, expr.end));
+ }
+
+ return expr;
+}
+
+export const parse_expression_at = (source: string, index: number): Expression => {
+ const expression = consume_expression(source, index);
+
+ return expression;
+};
+
+// @ts-ignore
+export default function read_expression(parser: Parser) {
+ try {
+ const expression = parse_expression_at(parser.template, parser.index);
+ parser.index = expression.end;
+ return expression;
+ } catch (err) {
+ parser.acorn_error(err);
+ }
+}
diff --git a/packages/astro-parser/src/parse/read/script.ts b/packages/astro-parser/src/parse/read/script.ts
new file mode 100644
index 000000000..9b8d71110
--- /dev/null
+++ b/packages/astro-parser/src/parse/read/script.ts
@@ -0,0 +1,60 @@
+// @ts-nocheck
+
+import type { Node } from 'estree';
+import { Parser } from '../index.js';
+import { Script } from '../../interfaces.js';
+
+const script_closing_tag = '</script>';
+
+function get_context(parser: Parser, attributes: any[], start: number): 'runtime' | 'setup' {
+ const context = attributes.find((attribute) => attribute.name === 'astro');
+ if (!context) return 'runtime';
+ if (context.value === true) return 'setup';
+
+ if (context.value.length !== 1 || context.value[0].type !== 'Text') {
+ parser.error(
+ {
+ code: 'invalid-script',
+ message: 'astro attribute must be static',
+ },
+ start
+ );
+ }
+
+ const value = context.value[0].data;
+
+ if (value !== 'setup') {
+ parser.error(
+ {
+ code: 'invalid-script',
+ message: 'If the "astro" attribute has a value, its value must be "setup"',
+ },
+ context.start
+ );
+ }
+
+ return value;
+}
+
+export default function read_script(parser: Parser, start: number, attributes: Node[]): Script {
+ const script_start = parser.index;
+ const script_end = parser.template.indexOf(script_closing_tag, script_start);
+
+ if (script_end === -1) {
+ parser.error({
+ code: 'unclosed-script',
+ message: '<script> must have a closing tag',
+ });
+ }
+
+ const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + parser.template.slice(script_start, script_end);
+ parser.index = script_end + script_closing_tag.length;
+
+ return {
+ type: 'Script',
+ start,
+ end: parser.index,
+ context: get_context(parser, attributes, start),
+ content: source,
+ };
+}
diff --git a/packages/astro-parser/src/parse/read/style.ts b/packages/astro-parser/src/parse/read/style.ts
new file mode 100644
index 000000000..f23d7b10e
--- /dev/null
+++ b/packages/astro-parser/src/parse/read/style.ts
@@ -0,0 +1,40 @@
+import { Parser } from '../index.js';
+import { Style } from '../../interfaces.js';
+
+interface Attribute {
+ start: number;
+ end: number;
+ type: 'Attribute';
+ name: string;
+ value: {
+ raw: string;
+ data: string;
+ }[];
+}
+
+export default function read_style(parser: Parser, start: number, attributes: Attribute[]): Style {
+ const content_start = parser.index;
+ const styles = parser.read_until(/<\/style>/);
+ const content_end = parser.index;
+ parser.eat('</style>', true);
+ const end = parser.index;
+
+ return {
+ type: 'Style',
+ start,
+ end,
+ attributes,
+ content: {
+ start: content_start,
+ end: content_end,
+ styles,
+ },
+ };
+}
+
+function is_ref_selector(a: any, b: any) {
+ // TODO add CSS node types
+ if (!b) return false;
+
+ return a.type === 'TypeSelector' && a.name === 'ref' && b.type === 'PseudoClassSelector';
+}