Bug 1681055 - [devtools] Update beautify-js to 1.13.0. r=ochameau.

This fix pretty-printing of async functions in console. A test case is added
to make sure we don't regress.

We used to copy the library tests and run them in xpcshell. The tests changed
a lot, and I don't think we get much value running tests that are already ran on
the project CI (we do have a few tests that checks that we get the output we want)
, so this patch remove the xpcshell test and the associated files.

The upgrade documentation is updated to remove some unecessary steps:
- no need to rename the exported module for each file
- no need to replace the acorn module, since what's in the file is just a subset
  of the library (~100 lines)
- no need to update the test file, which doesn't seem to exist anymore

Differential Revision: https://phabricator.services.mozilla.com/D99320
This commit is contained in:
Nicolas Chevobbe 2020-12-15 08:48:19 +00:00
Родитель 5f456b2985
Коммит 36ec5c6448
15 изменённых файлов: 8696 добавлений и 5028 удалений

Просмотреть файл

@ -11,15 +11,12 @@ const FORMATTED_HTML = `<body>
div {
color: red;
}
span {
text-decoration: underline;
}
</style>
<div>
<span>
<em>Hello</em>
</span>
</div>
<div><span><em>Hello</em></span></div>
<script>
console.log("Hello!");
</script>

Просмотреть файл

@ -4,34 +4,18 @@
2. Copy `js/lib/beautify.js` to `devtools/shared/jsbeautify/src/beautify-js.js`
3. Remove the acorn section from the file and add the following to the top:
3. Copy `beautify-html.js` to `devtools/shared/jsbeautify/src/beautify-html.js`
```
const acorn = require("acorn/acorn");
```
4. Replace the following line at the bottom of the file:
4. Just above `function Beautifier(js_source_text, options) {` add:
```
var js_beautify = require('./beautify.js');
```
```
exports.jsBeautify = js_beautify;
```
with (changing `beautify.js` into `beautify-js.js`):
5. Copy `beautify-html.js` to `devtools/shared/jsbeautify/src/beautify-html.js`
```
var js_beautify = require('./beautify-js.js');
```
6. Replace the require blocks at the bottom of the file with:
```
var beautify = require('devtools/shared/jsbeautify/beautify');
exports.htmlBeautify = function(html_source, options) {
return style_html(html_source, options, beautify.js, beautify.css);
};
```
7. Copy `beautify-css.js` to `devtools/shared/jsbeautify/src/beautify-css.js`
8. Replace the global define block at the bottom of the file with:
```
exports.cssBeautify = css_beautify;
```
9. Copy `js/test/beautify-tests.js` to `devtools/shared/jsbeautify/src/beautify-tests.js`
6. Copy `beautify-css.js` to `devtools/shared/jsbeautify/src/beautify-css.js`

Просмотреть файл

@ -1,7 +1,9 @@
var { cssBeautify } = require("devtools/shared/jsbeautify/src/beautify-css");
var { htmlBeautify } = require("devtools/shared/jsbeautify/src/beautify-html");
var { jsBeautify } = require("devtools/shared/jsbeautify/src/beautify-js");
const { css_beautify } = require("devtools/shared/jsbeautify/src/beautify-css");
const {
html_beautify,
} = require("devtools/shared/jsbeautify/src/beautify-html");
const { js_beautify } = require("devtools/shared/jsbeautify/src/beautify-js");
exports.css = cssBeautify;
exports.html = htmlBeautify;
exports.js = jsBeautify;
exports.css = css_beautify;
exports.html = html_beautify;
exports.js = js_beautify;

Просмотреть файл

@ -1,10 +0,0 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DevToolsModules(
'sanitytest.js',
'urlencode_unpacker.js',
)

Просмотреть файл

@ -1,137 +0,0 @@
//
// simple testing interface
// written by Einar Lielmanis, einar@jsbeautifier.org
//
// usage:
//
// var t = new SanityTest(function (x) { return x; }, 'my function');
// t.expect('input', 'output');
// t.expect('a', 'a');
// output_somewhere(t.results()); // good for <pre>, html safe-ish
// alert(t.results_raw()); // html unescaped
function SanityTest (func, name_of_test) {
var test_func = func || function (x) {
return x;
};
var test_name = name_of_test || '';
var n_failed = 0;
var n_succeeded = 0;
this.failures = [];
this.successes = [];
this.test_function = function(func, name) {
test_func = func;
test_name = name || '';
};
this.get_exitcode = function() {
return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
};
this.expect = function(parameters, expected_value) {
// multi-parameter calls not supported (I don't need them now).
var result = test_func(parameters);
// proper array checking is a pain. i'll maybe do it later, compare strings representations instead
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') == expected_value.join(', '))) {
n_succeeded += 1;
this.successes.push([test_name, parameters, expected_value, result]);
} else {
n_failed += 1;
this.failures.push([test_name, parameters, expected_value, result]);
}
};
this.results_raw = function() {
var results = '';
if (n_failed === 0) {
if (n_succeeded === 0) {
results = 'No tests run.';
} else {
results = 'All ' + n_succeeded + ' tests passed.';
}
} else {
for (var i = 0 ; i < this.failures.length; i++) {
var f = this.failures[i];
if (f[0]) {
f[0] = f[0] + ' ';
}
results += '---- ' + f[0] + 'input -------\n' + this.prettyprint(f[1]) + '\n';
results += '---- ' + f[0] + 'expected ----\n' + this.prettyprint(f[2]) + '\n';
results += '---- ' + f[0] + 'output ------\n' + this.prettyprint(f[3]) + '\n\n';
}
results += n_failed + ' tests failed.\n';
}
return results;
};
this.results = function() {
return this.lazy_escape(this.results_raw());
};
this.prettyprint = function(something, quote_strings) {
var type = typeof something;
switch(type.toLowerCase()) {
case 'string':
if (quote_strings) {
return "'" + something.replace("'", "\\'") + "'";
} else {
return something;
}
case 'number':
return '' + something;
case 'boolean':
return something ? 'true' : 'false';
case 'undefined':
return 'undefined';
case 'object':
if (something instanceof Array) {
var x = [];
var expected_index = 0;
for (var k in something) {
if (k == expected_index) {
x.push(this.prettyprint(something[k], true));
expected_index += 1;
} else {
x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
}
}
return '[' + x.join(', ') + ']';
} else {
return 'object: ' + something;
}
default:
return type + ': ' + something;
}
};
this.lazy_escape = function (str) {
return str.replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\n/g, '<br />');
};
this.log = function () {
if (window.console) {
if (console.firebug) {
console.log.apply(console, Array.prototype.slice.call(arguments));
} else {
console.log.call(console, Array.prototype.slice.call(arguments));
}
}
};
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = SanityTest;
}

Просмотреть файл

@ -1,73 +0,0 @@
/*global unescape */
/*jshint curly: false, scripturl: true */
//
// trivial bookmarklet/escaped script detector for the javascript beautifier
// written by Einar Lielmanis <einar@jsbeautifier.org>
//
// usage:
//
// if (Urlencoded.detect(some_string)) {
// var unpacked = Urlencoded.unpack(some_string);
// }
//
//
var isNode = (typeof module !== 'undefined' && module.exports);
if (isNode) {
var SanityTest = require("devtools/shared/jsbeautify/lib/sanitytest");
}
var Urlencoded = {
detect: function (str) {
// the fact that script doesn't contain any space, but has %20 instead
// should be sufficient check for now.
if (!str.includes(' ')) {
if (str.includes('%2')) return true;
if (str.replace(/[^%]+/g, '').length > 3) return true;
}
return false;
},
unpack: function (str) {
if (Urlencoded.detect(str)) {
if (str.includes('%2B') || str.includes('%2b')) {
// "+" escaped as "%2B"
return unescape(str.replace(/\+/g, '%20'));
} else {
return unescape(str);
}
}
return str;
},
run_tests: function (sanity_test) {
var t = sanity_test || new SanityTest();
t.test_function(Urlencoded.detect, "Urlencoded.detect");
t.expect('', false);
t.expect('var a = b', false);
t.expect('var%20a+=+b', true);
t.expect('var%20a=b', true);
t.expect('var%20%21%22', true);
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', true);
t.test_function(Urlencoded.unpack, 'Urlencoded.unpack');
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();',
'javascript:(function(){var whatever={init:function(){alert("a"+"b")}};whatever.init()})();'
);
t.expect('', '');
t.expect('abcd', 'abcd');
t.expect('var a = b', 'var a = b');
t.expect('var%20a=b', 'var a=b');
t.expect('var%20a=b+1', 'var a=b+1');
t.expect('var%20a=b%2b1', 'var a=b+1');
return t;
}
};
if (isNode) {
module.exports = Urlencoded;
}

Просмотреть файл

@ -5,12 +5,9 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DIRS += [
'lib',
'src',
]
XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
DevToolsModules(
'beautify.js',
)

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -7,6 +7,5 @@
DevToolsModules(
'beautify-css.js',
'beautify-html.js',
'beautify-js.js',
'beautify-tests.js'
'beautify-js.js'
)

Просмотреть файл

@ -1,11 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
var beautify = require("devtools/shared/jsbeautify/beautify");
var SanityTest = require('devtools/shared/jsbeautify/lib/sanitytest');
var Urlencoded = require('devtools/shared/jsbeautify/lib/urlencode_unpacker');
var {run_beautifier_tests} = require('devtools/shared/jsbeautify/src/beautify-tests');

Просмотреть файл

@ -1,21 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
function run_test() {
var sanityTest = new SanityTest();
var results = run_beautifier_tests(sanityTest,
Urlencoded,
beautify.js,
beautify.html,
beautify.css);
for (let [test_name, parameters, expected_value, result] of sanityTest.successes) {
equal(result, expected_value, "actual result matches expected");
}
for (let [test_name, parameters, expected_value, result] of sanityTest.failures) {
equal(result, expected_value, "actual result matches expected");
}
}

Просмотреть файл

@ -1,7 +0,0 @@
[DEFAULT]
tags = devtools
head = head_jsbeautify.js
firefox-appdir = browser
skip-if = toolkit == 'android'
[test.js]