simplify JS parsing step

pull/353/head
Rich-Harris 8 years ago
parent 94dc6d26b6
commit 71cfb93384

@ -1,28 +1,14 @@
import { parse, tokenizer } from 'acorn'; import { parse } from 'acorn';
import spaces from '../../utils/spaces.js'; import spaces from '../../utils/spaces.js';
const scriptClosingTag = '<\/script>';
export default function readScript ( parser, start, attributes ) { export default function readScript ( parser, start, attributes ) {
const scriptStart = parser.index; const scriptStart = parser.index;
let scriptEnd = null; const scriptEnd = parser.template.indexOf( scriptClosingTag, scriptStart );
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 ); const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd );
parser.index = scriptEnd + scriptClosingTag.length;
let ast; let ast;
@ -38,7 +24,6 @@ export default function readScript ( parser, start, attributes ) {
if ( !ast.body.length ) return null; if ( !ast.body.length ) return null;
ast.start = scriptStart; ast.start = scriptStart;
return { return {
start, start,
end: parser.index, end: parser.index,

Loading…
Cancel
Save