/** @import { Program } from 'estree' */ /** @import { AST } from '#compiler' */ /** @import { Parser } from '../index.js' */ import * as acorn from '../acorn.js'; import { regex_not_newline_characters } from '../../patterns.js'; import * as e from '../../../errors.js'; import * as w from '../../../warnings.js'; import { is_text_attribute } from '../../../utils/ast.js'; import { locator } from '../../../state.js'; const regex_closing_script_tag = /<\/script\s*>/; const regex_starts_with_closing_script_tag = /<\/script\s*>/y; const RESERVED_ATTRIBUTES = ['server', 'client', 'worker', 'test', 'default']; const ALLOWED_ATTRIBUTES = ['context', 'generics', 'lang', 'module']; /** * @param {Parser} parser * @param {number} start * @param {Array} attributes * @returns {AST.Script} */ export function read_script(parser, start, attributes) { const script_start = parser.index; const data = parser.read_until_regex(regex_closing_script_tag); if (parser.index >= parser.template.length) { e.element_unclosed(parser.template.length, 'script'); } const source = parser.template.slice(0, script_start).replace(regex_not_newline_characters, ' ') + data; parser.read(regex_starts_with_closing_script_tag); const ast = acorn.parse(source, parser.root.comments, parser.ts, true); ast.start = script_start; if (ast.loc) { // Acorn always uses `0` as the start of a `Program`, but for sourcemap purposes // we need it to be the start of the `