Skip to content

Commit

Permalink
Keep leading comments on consecutive lines (#4975)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 5, 2023
1 parent fd64748 commit f85634c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/ast/nodes/Program.ts
Expand Up @@ -3,7 +3,11 @@ import type MagicString from 'magic-string';
import getCodeFrame from '../../utils/getCodeFrame';
import { getOriginalLocation } from '../../utils/getOriginalLocation';
import relativeId from '../../utils/relativeId';
import { type RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import {
findFirstLineBreakOutsideComment,
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { createHasEffectsContext } from '../ExecutionContext';
import type * as NodeType from './NodeType';
Expand Down Expand Up @@ -79,6 +83,16 @@ export default class Program extends NodeBase {
code.remove(0, start);
}
if (this.body.length > 0) {
// Keep all consecutive lines that start with a comment
while (code.original[start] === '/' && /[*/]/.test(code.original[start + 1])) {
const firstLineBreak = findFirstLineBreakOutsideComment(
code.original.slice(start, this.body[0].start)
);
if (firstLineBreak[0] === -1) {
break;
}
start += firstLineBreak[1];
}
renderStatementList(this.body, code, start, this.end, options);
} else {
super.render(code, options);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/renderHelpers.ts
Expand Up @@ -70,7 +70,7 @@ export function findNonWhiteSpace(code: string, index: number): number {

// This assumes "code" only contains white-space and comments
// Returns position of line-comment if applicable
function findFirstLineBreakOutsideComment(code: string): [number, number] {
export function findFirstLineBreakOutsideComment(code: string): [number, number] {
let lineBreakPos,
charCodeAfterSlash,
start = 0;
Expand Down
6 changes: 6 additions & 0 deletions test/form/samples/leading-comments/_config.js
@@ -0,0 +1,6 @@
module.exports = defineTest({
description: 'keeps leading comments on consecutive lines',
options: {
external: ['external']
}
});
13 changes: 13 additions & 0 deletions test/form/samples/leading-comments/_expected.js
@@ -0,0 +1,13 @@
import 'external';

// comment dep 1/3
/* comment
*/ // comment dep 2/3
// comment dep 3/3

console.log('dep');

// comment main 1/2
// comment main 2/2

console.log('main');
7 changes: 7 additions & 0 deletions test/form/samples/leading-comments/dep.js
@@ -0,0 +1,7 @@
// comment dep 1/3
/* comment
*/ // comment dep 2/3
// comment dep 3/3
/* import comment */ import 'external';

console.log('dep');
6 changes: 6 additions & 0 deletions test/form/samples/leading-comments/main.js
@@ -0,0 +1,6 @@
#! /usr/bin/env node
// comment main 1/2
// comment main 2/2
import './dep.js';

console.log('main');
@@ -1,5 +1,6 @@
// pure top-level IIFE will be dropped


// pure top-level IIFE assigned to unreferenced var will not be dropped
global.iife1 = /*@__PURE__*/(function() {
console.log("iife1");
Expand Down
1 change: 1 addition & 0 deletions test/form/samples/remove-treeshaken-banners/_expected.js
@@ -1,4 +1,5 @@
// dep included banner: included

console.log('dep included');

// dep included footer: included
1 change: 1 addition & 0 deletions test/form/samples/render-removed-statements/_expected.js
@@ -1,4 +1,5 @@
/* header retained */

/* lead
retained */ console.log(2); // trail retained
console.log(2); // trail retained
Expand Down

0 comments on commit f85634c

Please sign in to comment.