Skip to content

Commit

Permalink
fix(es/minifier): Remove wrong rule (#6201)
Browse files Browse the repository at this point in the history
**Related issue:**

 - vercel/next.js#41527.
  • Loading branch information
kdy1 committed Oct 19, 2022
1 parent a049ef0 commit 842abd4
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 27 deletions.
12 changes: 5 additions & 7 deletions crates/swc_ecma_minifier/src/compress/optimize/ops.rs
Expand Up @@ -279,7 +279,7 @@ where
/// # Examples
///
/// - `x() && true` => `!!x()`
pub(super) fn compress_logical_exprs_as_bang_bang(&mut self, e: &mut Expr, in_bool_ctx: bool) {
pub(super) fn compress_logical_exprs_as_bang_bang(&mut self, e: &mut Expr, _in_bool_ctx: bool) {
if !self.options.conditionals && !self.options.reduce_vars {
return;
}
Expand All @@ -299,12 +299,10 @@ where
}

let lt = bin.left.get_type();
if !in_bool_ctx {
match lt {
// Don't change type
Known(Type::Bool) => {}
_ => return,
}
match lt {
// Don't change type
Known(Type::Bool) => {}
_ => return,
}

let rt = bin.right.get_type();
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Expand Up @@ -82,6 +82,7 @@ conditionals/ifs_5/input.js
conditionals/ifs_6/input.js
conditionals/ifs_same_consequent/input.js
conditionals/issue_1154/input.js
conditionals/issue_2535_2/input.js
dead_code/dead_code_const_annotation/input.js
dead_code/dead_code_const_annotation_complex_scope/input.js
dead_code/dead_code_constant_boolean_should_warn_more/input.js
Expand Down Expand Up @@ -210,7 +211,6 @@ harmony/issue_1898/input.js
harmony/issue_2349b/input.js
harmony/issue_2794_1/input.js
harmony/issue_2794_2/input.js
harmony/issue_2794_3/input.js
harmony/issue_2874_1/input.js
harmony/issue_2874_2/input.js
harmony/module_enabled/input.js
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/echarts.js
Expand Up @@ -10053,7 +10053,7 @@
return dataIndices;
}, SeriesModel.prototype.isSelected = function(dataIndex, dataType) {
var selectedMap = this.option.selectedMap;
return !!selectedMap && !!selectedMap[getSelectionKey(this.getData(dataType), dataIndex)];
return !!selectedMap && (selectedMap[getSelectionKey(this.getData(dataType), dataIndex)] || !1);
}, SeriesModel.prototype._innerSelect = function(data, innerDataIndices) {
var _a, _b, selectedMode = this.option.selectedMode, len = innerDataIndices.length;
if (selectedMode && len) {
Expand Down
Expand Up @@ -6024,7 +6024,7 @@
$({
target: "Number",
proto: !0,
forced: !!nativeToFixed || !fails(function() {
forced: nativeToFixed && !0 || !fails(function() {
nativeToFixed.call({});
})
}, {
Expand Down
238 changes: 238 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/next/41527/1/input.js
@@ -0,0 +1,238 @@
import Delta from 'quill-delta';
import { EmbedBlot, Scope } from 'parchment';
import Quill from '../core/quill';
import logger from '../core/logger';
import Module from '../core/module';
const debug = logger('quill:toolbar');
class Toolbar extends Module {
constructor(quill, options) {
super(quill, options);
if (Array.isArray(this.options.container)) {
const container = document.createElement('div');
addControls(container, this.options.container);
quill.container.parentNode.insertBefore(container, quill.container);
this.container = container;
} else if (typeof this.options.container === 'string') {
this.container = document.querySelector(this.options.container);
} else {
this.container = this.options.container;
}
if (!(this.container instanceof HTMLElement)) {
debug.error('Container required for toolbar', this.options);
return;
}
this.container.classList.add('ql-toolbar');
this.controls = [];
this.handlers = {};
Object.keys(this.options.handlers).forEach((format) => {
this.addHandler(format, this.options.handlers[format]);
});
Array.from(this.container.querySelectorAll('button, select')).forEach((input) => {
this.attach(input);
});
this.quill.on(Quill.events.EDITOR_CHANGE, (type, range) => {
if (type === Quill.events.SELECTION_CHANGE) {
this.update(range);
}
});
this.quill.on(Quill.events.SCROLL_OPTIMIZE, () => {
const [range] = this.quill.selection.getRange();
this.update(range);
});
}
addHandler(format, handler) {
this.handlers[format] = handler;
}
attach(input) {
let format = Array.from(input.classList).find((className) => {
return className.indexOf('ql-') === 0;
});
if (!format) return;
format = format.slice('ql-'.length);
if (input.tagName === 'BUTTON') {
input.setAttribute('type', 'button');
}
if (this.handlers[format] == null && this.quill.scroll.query(format) == null) {
debug.warn('ignoring attaching to nonexistent format', format, input);
return;
}
const eventName = input.tagName === 'SELECT' ? 'change' : 'click';
input.addEventListener(eventName, (e) => {
let value;
if (input.tagName === 'SELECT') {
if (input.selectedIndex < 0) return;
const selected = input.options[input.selectedIndex];
if (selected.hasAttribute('selected')) {
value = false;
} else {
value = selected.value || false;
}
} else {
if (input.classList.contains('ql-active')) {
value = false;
} else {
value = input.value || !input.hasAttribute('value');
}
e.preventDefault();
}
this.quill.focus();
const [range] = this.quill.selection.getRange();
if (this.handlers[format] != null) {
this.handlers[format].call(this, value);
} else if (this.quill.scroll.query(format).prototype instanceof EmbedBlot) {
value = prompt(`Enter ${format}`);
if (!value) return;
this.quill.updateContents(new Delta().retain(range.index).delete(range.length).insert({
[format]: value
}), Quill.sources.USER);
} else {
this.quill.format(format, value, Quill.sources.USER);
}
this.update(range);
});
this.controls.push([
format,
input
]);
}
update(range) {
const formats = range == null ? {} : this.quill.getFormat(range);
this.controls.forEach((pair) => {
const [format, input] = pair;
if (input.tagName === 'SELECT') {
let option;
if (range == null) {
option = null;
} else if (formats[format] == null) {
option = input.querySelector('option[selected]');
} else if (!Array.isArray(formats[format])) {
let value = formats[format];
if (typeof value === 'string') {
value = value.replace(/"/g, '\\"');
}
option = input.querySelector(`option[value="${value}"]`);
}
if (option == null) {
input.value = '';
input.selectedIndex = -1;
} else {
option.selected = true;
}
} else if (range == null) {
input.classList.remove('ql-active');
} else if (input.hasAttribute('value')) {
const isActive = formats[format] === input.getAttribute('value') || formats[format] != null && formats[format].toString() === input.getAttribute('value') || formats[format] == null && !input.getAttribute('value');
input.classList.toggle('ql-active', isActive);
} else {
input.classList.toggle('ql-active', formats[format] != null);
}
});
}
}
Toolbar.DEFAULTS = {};
function addButton(container, format, value) {
const input = document.createElement('button');
input.setAttribute('type', 'button');
input.classList.add(`ql-${format}`);
if (value != null) {
input.value = value;
}
container.appendChild(input);
}
function addControls(container, groups) {
if (!Array.isArray(groups[0])) {
groups = [
groups
];
}
groups.forEach((controls) => {
const group = document.createElement('span');
group.classList.add('ql-formats');
controls.forEach((control) => {
if (typeof control === 'string') {
addButton(group, control);
} else {
const format = Object.keys(control)[0];
const value = control[format];
if (Array.isArray(value)) {
addSelect(group, format, value);
} else {
addButton(group, format, value);
}
}
});
container.appendChild(group);
});
}
function addSelect(container, format, values) {
const input = document.createElement('select');
input.classList.add(`ql-${format}`);
values.forEach((value) => {
const option = document.createElement('option');
if (value !== false) {
option.setAttribute('value', value);
} else {
option.setAttribute('selected', 'selected');
}
input.appendChild(option);
});
container.appendChild(input);
}
Toolbar.DEFAULTS = {
container: null,
handlers: {
clean() {
const range = this.quill.getSelection();
if (range == null) return;
if (range.length === 0) {
const formats = this.quill.getFormat();
Object.keys(formats).forEach((name) => {
if (this.quill.scroll.query(name, Scope.INLINE) != null) {
this.quill.format(name, false, Quill.sources.USER);
}
});
} else {
this.quill.removeFormat(range, Quill.sources.USER);
}
},
direction(value) {
const { align } = this.quill.getFormat();
if (value === 'rtl' && align == null) {
this.quill.format('align', 'right', Quill.sources.USER);
} else if (!value && align === 'right') {
this.quill.format('align', false, Quill.sources.USER);
}
this.quill.format('direction', value, Quill.sources.USER);
},
indent(value) {
const range = this.quill.getSelection();
const formats = this.quill.getFormat(range);
const indent = parseInt(formats.indent || 0, 10);
if (value === '+1' || value === '-1') {
let modifier = value === '+1' ? 1 : -1;
if (formats.direction === 'rtl') modifier *= -1;
this.quill.format('indent', indent + modifier, Quill.sources.USER);
}
},
link(value) {
if (value === true) {
value = prompt('Enter link URL:');
}
this.quill.format('link', value, Quill.sources.USER);
},
list(value) {
const range = this.quill.getSelection();
const formats = this.quill.getFormat(range);
if (value === 'check') {
if (formats.list === 'checked' || formats.list === 'unchecked') {
this.quill.format('list', false, Quill.sources.USER);
} else {
this.quill.format('list', 'unchecked', Quill.sources.USER);
}
} else {
this.quill.format('list', value, Quill.sources.USER);
}
}
}
};
export { Toolbar as default, addControls };

1 comment on commit 842abd4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 842abd4 Previous: 0ba464d Ratio
es/full/minify/libraries/antd 1987920106 ns/iter (± 38350667) 1980275902 ns/iter (± 46306263) 1.00
es/full/minify/libraries/d3 454701610 ns/iter (± 22105722) 433243434 ns/iter (± 33146554) 1.05
es/full/minify/libraries/echarts 1822721566 ns/iter (± 430307712) 1658424070 ns/iter (± 69878668) 1.10
es/full/minify/libraries/jquery 116499814 ns/iter (± 11769607) 115088142 ns/iter (± 9354814) 1.01
es/full/minify/libraries/lodash 132333266 ns/iter (± 10850176) 130959579 ns/iter (± 10430462) 1.01
es/full/minify/libraries/moment 79246110 ns/iter (± 13373743) 65224644 ns/iter (± 6799692) 1.21
es/full/minify/libraries/react 22049141 ns/iter (± 1228722) 22352799 ns/iter (± 1524468) 0.99
es/full/minify/libraries/terser 351748932 ns/iter (± 37835653) 342102294 ns/iter (± 14456376) 1.03
es/full/minify/libraries/three 614463318 ns/iter (± 38044971) 628007422 ns/iter (± 240501706) 0.98
es/full/minify/libraries/typescript 3731332042 ns/iter (± 109327183) 3731844430 ns/iter (± 222150843) 1.00
es/full/minify/libraries/victory 1000605691 ns/iter (± 68287295) 863141606 ns/iter (± 16115762) 1.16
es/full/minify/libraries/vue 195728434 ns/iter (± 19696210) 185526411 ns/iter (± 16100830) 1.05
es/full/codegen/es3 38207 ns/iter (± 7380) 33978 ns/iter (± 3938) 1.12
es/full/codegen/es5 36399 ns/iter (± 5843) 33924 ns/iter (± 1607) 1.07
es/full/codegen/es2015 40705 ns/iter (± 6214) 33711 ns/iter (± 2385) 1.21
es/full/codegen/es2016 38705 ns/iter (± 6792) 33571 ns/iter (± 6121) 1.15
es/full/codegen/es2017 40114 ns/iter (± 6343) 34277 ns/iter (± 7727) 1.17
es/full/codegen/es2018 42386 ns/iter (± 7881) 33742 ns/iter (± 2113) 1.26
es/full/codegen/es2019 39960 ns/iter (± 6800) 33684 ns/iter (± 767) 1.19
es/full/codegen/es2020 37409 ns/iter (± 5336) 33476 ns/iter (± 830) 1.12
es/full/all/es3 247828681 ns/iter (± 26836941) 205031486 ns/iter (± 32267580) 1.21
es/full/all/es5 228299889 ns/iter (± 32952433) 213044840 ns/iter (± 21278091) 1.07
es/full/all/es2015 188536936 ns/iter (± 20727926) 172217080 ns/iter (± 19316954) 1.09
es/full/all/es2016 193588097 ns/iter (± 19695210) 158515768 ns/iter (± 11354755) 1.22
es/full/all/es2017 191247476 ns/iter (± 19413051) 166700104 ns/iter (± 14003228) 1.15
es/full/all/es2018 187477093 ns/iter (± 20962967) 151470894 ns/iter (± 10763565) 1.24
es/full/all/es2019 189771349 ns/iter (± 18380408) 145611300 ns/iter (± 10422728) 1.30
es/full/all/es2020 181164204 ns/iter (± 22428478) 146629383 ns/iter (± 20690210) 1.24
es/full/parser 923359 ns/iter (± 141678) 729613 ns/iter (± 43289) 1.27
es/full/base/fixer 31768 ns/iter (± 3808) 29888 ns/iter (± 2688) 1.06
es/full/base/resolver_and_hygiene 105770 ns/iter (± 15684) 100167 ns/iter (± 15700) 1.06
serialization of ast node 272 ns/iter (± 48) 224 ns/iter (± 14) 1.21
serialization of serde 255 ns/iter (± 48) 216 ns/iter (± 14) 1.18

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.