diff --git a/gulpfile.js b/gulpfile.js index 88a3152..38504f5 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -16,7 +16,9 @@ const replacements = require('./src/replacements'); const sources = [ 'src/header.js', - 'node_modules/uglify-js/lib/parse-js.js', + 'node_modules/uglify-js/lib/utils.js', + 'node_modules/uglify-js/lib/ast.js', + 'node_modules/uglify-js/lib/parse.js', 'src/api.js' ]; @@ -47,7 +49,7 @@ gulp.task('build-covered', ['build-covered-api'], function() { return gulp.src( [ 'src/header.js', - 'node_modules/uglify-js/lib/parse-js.js', + 'node_modules/uglify-js/lib/parse.js', 'tmp/api.covered.js' ]) .pipe(order(sources, { base: __dirname })) diff --git a/src/api.js b/src/api.js index 8afffe8..734542f 100644 --- a/src/api.js +++ b/src/api.js @@ -83,16 +83,16 @@ function qmlweb_tokenizer($TEXT) { parse_error = function(err) { throw new QMLParseError(err, S.tokline, S.tokcol, S.tokpos, S.text); }; + var filename, html5_comments, shebang; // WARNING: Here the original tokenizer() code gets embedded return tokenizer($TEXT); } function qmlweb_parse($TEXT, document_type, exigent_mode) { - var embed_tokens = false; // embed_tokens option is not supported - var TEXT = $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''); - $TEXT = qmlweb_tokenizer($TEXT, true); + $TEXT = qmlweb_tokenizer($TEXT); + var options; // WARNING: Here the original parse() code gets embedded parse($TEXT,exigent_mode,false); @@ -100,6 +100,10 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) { // Override UglifyJS methods + embed_tokens = function(parser) { + return parser; + }; + croak = function(msg, line, col, pos) { var ctx = S.input.context(); throw new QMLParseError(msg, @@ -118,7 +122,7 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) { }; var statement_js = statement; - statement = function() { + statement = embed_tokens(function() { switch (S.token.type) { case "punc": switch (S.token.value) { @@ -127,7 +131,7 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) { } } return statement_js(); - }; + }); array_ = function() { var from = S.token.pos; @@ -149,6 +153,10 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) { // QML-specific methods + function as() { + return slice(arguments); + } + function as_statement() { var res = slice(arguments); S.in_function++; @@ -363,11 +371,23 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) { next(); } + var tree; if (document_type === qmlweb_parse.JSResource) { - return jsdocument(); + tree = jsdocument(); } else { - return qmldocument(); + tree = qmldocument(); + } + return two2one(tree); +} + +function two2one(tree) { + if (tree == null || typeof tree !== 'object') + return tree; + + for (var key in tree) { + tree[key] = two2one(tree[key]); } + return tree; } qmlweb_parse.nowParsingFile = ''; // TODO: make a parameter of qmlweb_parse diff --git a/src/replacements.js b/src/replacements.js index e9cdd5c..0253e5f 100644 --- a/src/replacements.js +++ b/src/replacements.js @@ -1,6 +1,6 @@ 'use strict'; -const uglify = require('uglify-js/lib/parse-js'); +const uglify = require('uglify-js'); function source(fn) { return Function.prototype.toString.call(fn) @@ -12,9 +12,10 @@ function source(fn) { } const tokenizer = source(uglify.tokenizer); -const parse = source(uglify.parse).split('return as("toplevel"')[0]; +const parse = source(uglify.parse).split('return (function(){')[0]; module.exports = [ { from: 'return tokenizer($TEXT);', to: tokenizer }, + //{ from: 'aasdASDASasdf', to: '' }, { from: 'parse($TEXT,exigent_mode,false);', to: parse } ];