You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/parse/read/script.ts

36 lines
800 B

import { parse } from 'acorn';
import spaces from '../../utils/spaces.js';
const scriptClosingTag = '<\/script>';
export default function readScript ( parser, start, attributes ) {
const scriptStart = parser.index;
const scriptEnd = parser.template.indexOf( scriptClosingTag, scriptStart );
if ( scriptEnd === -1 ) parser.error( `<script> must have a closing tag` );
const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd );
parser.index = scriptEnd + scriptClosingTag.length;
let ast;
try {
ast = parse( source, {
ecmaVersion: 8,
sourceType: 'module'
});
} catch ( err ) {
parser.acornError( err );
}
if ( !ast.body.length ) return null;
ast.start = scriptStart;
return {
start,
end: parser.index,
attributes,
content: ast
};
}