Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/js-multiline-to-singleline/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-eval */
import { expect } from 'chai';
import { makeMultilineJSIntoSingleLine as toSingleLine } from './';

Expand All @@ -12,12 +13,34 @@ describe('makeMultilineJSIntoSingleLine', () => {
expect(toSingleLine('() => { return\n42\n }')).to.equal('() => {return; 42; }');
});

it('treats comments propertly', () => {
it('treats comments properly', () => {
expect(toSingleLine('a // comment\n b')).to.equal('a; /* comment*/ b');
expect(toSingleLine('a /* comment*/\n b')).to.equal('a; /* comment*/ b');
});

it('keeps invalid code as-is', () => {
expect(toSingleLine('---\n---')).to.equal('--- ---');
});

it('treats multiline template strings properly', () => {
for (const original of [
'(`1\\t2`);',
'(`1\\t\n2`);',
'(`1\\t\r\n2`);',
'(`1\\t\r2`);',
'(`1\\t\\nn2`);',
'(`1${\nnull\n}\n2`);',
'(String.raw `1\\t2`);',
'(String.raw `1\\t\n2`);',
'(String.raw `1\\t\\n2`);',
'(String.raw `1\\t\\r\\n2`);',
'(String.raw `1\\t\\r2`);',
'(String.raw `1${\nnull\n}\n2`);',
'(String.raw `1${\nnull\n}\\n2`);',
]) {
const singleLine = toSingleLine(original);
expect(singleLine).to.not.match(/[\r\n]/); // singleline
expect(eval(singleLine)).to.equal(eval(original)); // same behavior
}
});
});
32 changes: 31 additions & 1 deletion packages/js-multiline-to-singleline/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as babel from '@babel/core';
import * as BabelTypes from '@babel/types';

// Babel plugin that turns all single-line // comments into /* ... */ block comments
function lineCommentToBlockComment(): babel.PluginObj {
Expand Down Expand Up @@ -40,6 +41,35 @@ function lineCommentToBlockComment(): babel.PluginObj {
};
}

// Babel plugin that turns all multi-line `...` template strings into single-line template strings
function multilineTemplateStringToSingleLine({ types: t }: { types: typeof BabelTypes }): babel.PluginObj {
return {
visitor: {
TemplateLiteral(path) {
if (!path.node.quasis.some(({ value }) => value.raw.match(/[\r\n]/))) {
return; // is already single line, nothing to do
}
if (path.parentPath.isTaggedTemplateExpression()) {
// Just wrap it in `eval()`. There isn't much we can do about e.g. String.raw `a<newline>b`
// that would remove the newline but reserve the template tag behavior.
path.parentPath.replaceWith(t.callExpression(
t.identifier('eval'),
[t.stringLiteral(this.file.code.slice(path.parent.start ?? undefined, path.parent.end ?? undefined))]
));
return;
}
// Escape newlines directly (note that \r and \r\n are being turned into \n here!)
path.replaceWith(t.templateLiteral(
path.node.quasis.map(el => t.templateElement({
raw: el.value.raw.replace(/\n|\r\n?/g, '\\n')
}, el.tail)),
path.node.expressions
));
}
}
};
}

export function makeMultilineJSIntoSingleLine(src: string): string {
// We use babel without any actual transformation steps, and only for ASI
// effects here, e.g. turning `return\n42` into `return;\n42;`
Expand All @@ -60,7 +90,7 @@ export function makeMultilineJSIntoSingleLine(src: string): string {
configFile: false,
babelrc: false,
browserslistConfigFile: false,
plugins: [lineCommentToBlockComment],
plugins: [lineCommentToBlockComment, multilineTemplateStringToSingleLine],
sourceType: 'script'
})?.code ?? src;
} catch {
Expand Down