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/compiler/parse/read/script.js

49 lines
913 B

import { parse, tokenizer } from 'acorn';
import spaces from '../../utils/spaces.js';
export default function readScript ( parser, start, attributes ) {
const scriptStart = parser.index;
let scriptEnd = null;
for ( const token of tokenizer( parser.remaining() ) ) {
parser.index = scriptStart + token.end;
parser.allowWhitespace();
scriptEnd = parser.index;
if ( parser.eat( '/script>' ) ) {
// this happens with trailing comments!
scriptEnd -= 1;
break;
}
if ( parser.eat( '</script>' ) ) {
break;
}
}
const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd );
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
};
}