Skip to content

Commit

Permalink
minor clean-ups (#5701)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Oct 5, 2022
1 parent be8ccc3 commit 94a954c
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 77 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributing

## Documentation

Every new feature and API change should be accompanied by a README additon.
Every new feature and API change should be accompanied by a README addition.

## Testing

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ to be `false` and all symbol names will be omitted.
- `bare_returns` (default: `false`) — support top level `return` statements

- `html5_comments` (default: `true`) — process HTML comment as workaround for
browsers which do not recognise `<script>` tags
browsers which do not recognize `<script>` tags

- `module` (default: `false`) — set to `true` if you wish to process input as
ES module, i.e. implicit `"use strict";` and support for top-level `await`.
Expand Down Expand Up @@ -753,7 +753,7 @@ to be `false` and all symbol names will be omitted.
ES module, i.e. implicit `"use strict";` alongside with `toplevel` enabled.

- `negate_iife` (default: `true`) — negate "Immediately-Called Function Expressions"
where the return value is discarded, to avoid the parens that the
where the return value is discarded, to avoid the parentheses that the
code generator would insert.

- `objects` (default: `true`) — compact duplicate keys in object literals.
Expand Down Expand Up @@ -851,7 +851,7 @@ to be `false` and all symbol names will be omitted.
- `unused` (default: `true`) — drop unreferenced functions and variables (simple
direct variable assignments do not count as references unless set to `"keep_assign"`)

- `varify` (default: `true`) — convert block-scoped declaractions into `var`
- `varify` (default: `true`) — convert block-scoped declarations into `var`
whenever safe to do so

- `yields` (default: `true`) — apply optimizations to `yield` expressions
Expand Down
2 changes: 1 addition & 1 deletion lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
var AST_BlockScope = DEFNODE("BlockScope", "_var_names enclosed functions make_def parent_scope variables", {
$documentation: "Base class for all statements introducing a lexical scope",
$propdoc: {
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any inner scopes",
functions: "[Dictionary/S] like `variables`, but only lists function declarations",
parent_scope: "[AST_Scope?/S] link to the parent scope",
variables: "[Dictionary/S] a map of name ---> SymbolDef for all variables/functions defined in this scope",
Expand Down
5 changes: 2 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ Compressor.prototype.compress = function(node) {
args: [],
});
}
return self;
});
AST_Node.DEFMETHOD("wrap_expression", function() {
var self = this;
Expand Down Expand Up @@ -5083,7 +5082,7 @@ Compressor.prototype.compress = function(node) {
return static_fn && (static_fn[node.property] || expr.name == "Math" && node.property == "random");
}

// Accomodate when compress option evaluate=false
// Accommodate when compress option evaluate=false
// as well as the common constant expressions !0 and -1
(function(def) {
def(AST_Node, return_false);
Expand Down Expand Up @@ -11882,7 +11881,7 @@ Compressor.prototype.compress = function(node) {
if (nullish ? ll == null : !ll) {
AST_Node.warn("Condition left of {operator} always {value} [{start}]", {
operator: self.operator,
value: nullish ? "nulish" : "false",
value: nullish ? "nullish" : "false",
start: self.start,
});
return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
Expand Down
20 changes: 10 additions & 10 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function OutputStream(options) {
wrap_iife : false,
}, true);

// Convert comment option to RegExp if neccessary and set up comments filter
// Convert comment option to RegExp if necessary and set up comments filter
var comment_filter = return_false; // Default case, throw all comments away
if (options.comments) {
var comments = options.comments;
Expand Down Expand Up @@ -997,7 +997,7 @@ function OutputStream(options) {
if (self.init instanceof AST_Definitions) {
self.init.print(output);
} else {
parenthesize_for_noin(self.init, output, true);
parenthesize_for_no_in(self.init, output, true);
}
output.print(";");
output.space();
Expand Down Expand Up @@ -1413,7 +1413,7 @@ function OutputStream(options) {
print_braced(this, output);
});

function print_definitinos(type) {
function print_definitions(type) {
return function(output) {
var self = this;
output.print(type);
Expand All @@ -1426,15 +1426,15 @@ function OutputStream(options) {
if (!(p instanceof AST_IterationStatement && p.init === self)) output.semicolon();
};
}
DEFPRINT(AST_Const, print_definitinos("const"));
DEFPRINT(AST_Let, print_definitinos("let"));
DEFPRINT(AST_Var, print_definitinos("var"));
DEFPRINT(AST_Const, print_definitions("const"));
DEFPRINT(AST_Let, print_definitions("let"));
DEFPRINT(AST_Var, print_definitions("var"));

function parenthesize_for_noin(node, output, noin) {
function parenthesize_for_no_in(node, output, no_in) {
var parens = false;
// need to take some precautions here:
// https://github.com/mishoo/UglifyJS/issues/60
if (noin) node.walk(new TreeWalker(function(node) {
if (no_in) node.walk(new TreeWalker(function(node) {
if (parens) return true;
if (node instanceof AST_Binary && node.operator == "in") return parens = true;
if (node instanceof AST_Scope && !(is_arrow(node) && node.value)) return true;
Expand All @@ -1450,8 +1450,8 @@ function OutputStream(options) {
output.print("=");
output.space();
var p = output.parent(1);
var noin = p instanceof AST_For || p instanceof AST_ForEnumeration;
parenthesize_for_noin(self.value, output, noin);
var no_in = p instanceof AST_For || p instanceof AST_ForEnumeration;
parenthesize_for_no_in(self.value, output, no_in);
}
});

Expand Down
48 changes: 22 additions & 26 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ function parse($TEXT, options) {
else if (!optional && !can_insert_semicolon()) expect(";");
}

function parenthesised() {
function parenthesized() {
expect("(");
var exp = expression();
expect(")");
Expand Down Expand Up @@ -920,18 +920,18 @@ function parse($TEXT, options) {
next();
var body = in_loop(statement);
expect_token("keyword", "while");
var condition = parenthesised();
var condition = parenthesized();
semicolon(true);
return new AST_Do({
body : body,
condition : condition
condition : condition,
});

case "while":
next();
return new AST_While({
condition : parenthesised(),
body : in_loop(statement)
condition : parenthesized(),
body : in_loop(statement),
});

case "for":
Expand Down Expand Up @@ -959,15 +959,13 @@ function parse($TEXT, options) {
value = expression();
semicolon();
}
return new AST_Return({
value: value
});
return new AST_Return({ value: value });

case "switch":
next();
return new AST_Switch({
expression : parenthesised(),
body : in_loop(switch_body_)
expression : parenthesized(),
body : in_loop(switch_body_),
});

case "throw":
Expand All @@ -976,9 +974,7 @@ function parse($TEXT, options) {
croak("Illegal newline after 'throw'");
var value = expression();
semicolon();
return new AST_Throw({
value: value
});
return new AST_Throw({ value: value });

case "try":
next();
Expand All @@ -996,8 +992,8 @@ function parse($TEXT, options) {
}
next();
return new AST_With({
expression : parenthesised(),
body : statement()
expression : parenthesized(),
body : statement(),
});
}
}
Expand Down Expand Up @@ -1421,15 +1417,15 @@ function parse($TEXT, options) {
};

function if_() {
var cond = parenthesised(), body = statement(), belse = null;
var cond = parenthesized(), body = statement(), alt = null;
if (is("keyword", "else")) {
next();
belse = statement();
alt = statement();
}
return new AST_If({
condition : cond,
body : body,
alternative : belse
alternative : alt,
});
}

Expand Down Expand Up @@ -2171,9 +2167,9 @@ function parse($TEXT, options) {
token_error(sym.start, "Unexpected " + sym.name + " in strict mode");
}

function as_symbol(type, noerror) {
function as_symbol(type, no_error) {
if (!is("name")) {
if (!noerror) croak("Name expected");
if (!no_error) croak("Name expected");
return null;
}
var sym = _make_symbol(type, S.token);
Expand Down Expand Up @@ -2409,20 +2405,20 @@ function parse($TEXT, options) {
return new ctor({ operator: op, expression: expr });
}

var expr_op = function(left, min_prec, no_in) {
var expr_op = function(left, min_precision, no_in) {
var op = is("operator") ? S.token.value : null;
if (op == "in" && no_in) op = null;
var prec = op != null ? PRECEDENCE[op] : null;
if (prec != null && prec > min_prec) {
var precision = op != null ? PRECEDENCE[op] : null;
if (precision != null && precision > min_precision) {
next();
var right = expr_op(maybe_unary(no_in), op == "**" ? prec - 1 : prec, no_in);
var right = expr_op(maybe_unary(no_in), op == "**" ? precision - 1 : precision, no_in);
return expr_op(new AST_Binary({
start : left.start,
left : left,
operator : op,
right : right,
end : right.end
}), min_prec, no_in);
end : right.end,
}), min_precision, no_in);
}
return left;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function mangle_properties(ast, options) {
var regex = options.regex;

// note debug is either false (disabled), or a string of the debug suffix to use (enabled).
// note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
// note debug may be enabled as an empty string, which is falsy. Also treat passing 'true'
// the same as passing an empty string.
var debug = options.debug !== false;
var debug_suffix;
Expand Down
2 changes: 1 addition & 1 deletion test/compress/concat-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concat_1: {

var e = 1 + x() + 2 + "X" + 3 + "boo";

// be careful with concatentation with "\0" with octal-looking strings.
// be careful with concatenation with "\0" with octal-looking strings.
var f = "\0" + 360 + "\0" + 8 + "\0";
}
expect: {
Expand Down
2 changes: 1 addition & 1 deletion test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ if_var_return_2: {
}
}

if_var_retrn_3: {
if_var_return_3: {
options = {
conditionals: true,
if_return: true,
Expand Down
8 changes: 4 additions & 4 deletions test/compress/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ mangle: {
}
input: {
import foo, { bar } from "baz";
consoe.log(moo);
console.log(moo);
import * as moo from "moz";
}
expect: {
import o, { bar as m } from "baz";
consoe.log(r);
console.log(r);
import * as r from "moz";
}
}
Expand All @@ -157,12 +157,12 @@ rename_mangle: {
}
input: {
import foo, { bar } from "baz";
consoe.log(moo);
console.log(moo);
import * as moo from "moz";
}
expect: {
import o, { bar as m } from "baz";
consoe.log(r);
console.log(r);
import * as r from "moz";
}
}
Expand Down
53 changes: 35 additions & 18 deletions test/compress/issue-143.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* There was an incorrect sort behaviour documented in issue #143:
* There was an incorrect sort behavior documented in issue #143:
* (x = f(…)) <= x → x >= (x = f(…))
*
* For example, let the equation be:
Expand All @@ -12,37 +12,54 @@
* a >= (a = parseInt('100')) → 99 >= 100 → false
*/

tranformation_sort_order_equal: {
transformation_sort_order_equal: {
options = {
comparisons: true,
}

input: { (a = parseInt('100')) == a }
expect: { (a = parseInt('100')) == a }
input: {
console.log((a = parseInt("100")) == a);
}
expect: {
console.log((a = parseInt("100")) == a);
}
expect_stdout: "true"
}

tranformation_sort_order_unequal: {
transformation_sort_order_unequal: {
options = {
comparisons: true,
}

input: { (a = parseInt('100')) != a }
expect: { (a = parseInt('100')) != a }
input: {
console.log((a = parseInt("100")) != a);
}
expect: {
console.log((a = parseInt("100")) != a);
}
expect_stdout: "false"
}

tranformation_sort_order_lesser_or_equal: {
transformation_sort_order_lesser_or_equal: {
options = {
comparisons: true,
}

input: { (a = parseInt('100')) <= a }
expect: { (a = parseInt('100')) <= a }
input: {
console.log((a = parseInt("100")) <= a);
}
expect: {
console.log((a = parseInt("100")) <= a);
}
expect_stdout: "true"
}
tranformation_sort_order_greater_or_equal: {

transformation_sort_order_greater_or_equal: {
options = {
comparisons: true,
}

input: { (a = parseInt('100')) >= a }
expect: { (a = parseInt('100')) >= a }
}
input: {
console.log((a = parseInt("100")) >= a);
}
expect: {
console.log((a = parseInt("100")) >= a);
}
expect_stdout: "true"
}
Loading

0 comments on commit 94a954c

Please sign in to comment.