Skip to content

Commit 638c467

Browse files
committed
get leading comment back
1 parent 9315b8c commit 638c467

File tree

13 files changed

+44
-6
lines changed

13 files changed

+44
-6
lines changed

crates/swc_ecma_minifier/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040

4141
use once_cell::sync::Lazy;
4242
use pass::mangle_names::mangle_names;
43-
use swc_common::{comments::Comments, pass::Repeated, sync::Lrc, SourceMap, SyntaxContext};
43+
use swc_common::{
44+
comments::Comments, pass::Repeated, sync::Lrc, SourceMap, Spanned, SyntaxContext,
45+
};
4446
use swc_ecma_ast::*;
4547
use swc_ecma_transforms_optimization::debug_assert_valid;
4648
use swc_ecma_usage_analyzer::marks::Marks;
@@ -157,6 +159,11 @@ pub fn optimize(
157159
{
158160
let _timer = timer!("compress ast");
159161

162+
let first_span = match &n {
163+
Program::Module(module) => module.body.first().map(|s| s.span()),
164+
Program::Script(script) => script.body.first().map(|s| s.span()),
165+
};
166+
160167
perform_dce(&mut n, c, marks);
161168

162169
n.mutate(&mut compressor(
@@ -167,6 +174,22 @@ pub fn optimize(
167174
));
168175

169176
perform_dce(&mut n, c, marks);
177+
178+
let new_first_span = match &n {
179+
Program::Module(module) => module.body.first().map(|s| s.span()),
180+
Program::Script(script) => script.body.first().map(|s| s.span()),
181+
};
182+
183+
if first_span.map(|s| s.lo) != new_first_span.map(|s| s.lo) {
184+
if let (Some(span), Some(comments)) = (first_span, comments) {
185+
let comment = comments.take_leading(span.lo);
186+
let new_pos = new_first_span.unwrap_or(n.span()).lo;
187+
188+
for cmt in comment.into_iter().flatten() {
189+
comments.add_leading(new_pos, cmt);
190+
}
191+
}
192+
}
170193
}
171194

172195
// Again, we don't need to validate ast

crates/swc_ecma_minifier/tests/benches-full/lodash.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
(function() {
1+
/**
2+
* @license
3+
* Lodash <https://lodash.com/>
4+
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
5+
* Released under MIT license <https://lodash.com/license>
6+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
7+
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
8+
*/ (function() {
29
/** Error message constants. */ var undefined, FUNC_ERROR_TEXT = 'Expected a function', HASH_UNDEFINED = '__lodash_hash_undefined__', PLACEHOLDER = '__lodash_placeholder__', INFINITY = 1 / 0, NAN = 0 / 0, wrapFlags = [
310
[
411
'ary',

crates/swc_ecma_minifier/tests/benches-full/moment.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! moment.js
2+
//! version : 2.29.1
3+
//! authors : Tim Wood, Iskren Chernev, Moment.js contributors
4+
//! license : MIT
5+
//! momentjs.com
16
!function(global, factory) {
27
'object' == typeof exports && 'undefined' != typeof module ? module.exports = factory() : 'function' == typeof define && define.amd ? define(factory) : global.moment = factory();
38
}(this, function() {

crates/swc_ecma_minifier/tests/fixture/issues/10054/for/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Input:
12
window.a = [
23
function() {
34
for(var l = 0; i < 10; l++);

crates/swc_ecma_minifier/tests/fixture/issues/10054/if/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Input:
12
window.a = [
23
function() {
34
if (void 0 !== navigator.userAgentData) throw Error();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//// [indexerWithTuple.ts]

crates/swc_ecma_minifier/tests/fixture/issues/10539/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// class def disappears, leaving only `new DoesntWorkClass()` as output
2+
// class def completely disappears
23
(class DoesntWorkClass {
34
static prop = new DoesntWorkClass();
45
});

crates/swc_ecma_minifier/tests/fixture/issues/3256/2/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// real life example taken from https://github.com/nodeca/pako/blob/master/lib/zlib/adler32.js#L26
12
export default ((adler, buf, len, pos)=>{
23
let s1 = 0xffff & adler, s2 = adler >>> 16 & 0xffff, n = 0;
34
for(; 0 !== len;){

crates/swc_ecma_minifier/tests/fixture/issues/emotion/react/2/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var fn, cache, cursor, hyphenateRegex = /[A-Z]|^ms/g, animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g, unitless_browser_esm = {
1+
/* harmony default export */ var fn, cache, cursor, hyphenateRegex = /[A-Z]|^ms/g, animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g, unitless_browser_esm = {
22
animationIterationCount: 1,
33
borderImageOutset: 1,
44
borderImageSlice: 1,

crates/swc_ecma_minifier/tests/fixture/member_expr/array/output.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)