Skip to content

Commit

Permalink
refactor(transform): format 함수에서 String Interpolation을 사용할 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Danji-ya committed Nov 5, 2023
1 parent 61a01b9 commit 60640fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/transform/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { format, contentToTransformedContent } from './utils';
import { DEFAULT_IMPORT_KEYWORD } from '../constants';

describe('import declaration', () => {
it('can support Default import', () => {
Expand All @@ -11,7 +12,7 @@ describe('import declaration', () => {
"use strict";
const _imported = require("./square");
console.log("Area of square: ", _imported["default"](3, 5));
console.log("Area of square: ", _imported["${DEFAULT_IMPORT_KEYWORD}"](3, 5));
`;

expect(contentToTransformedContent(content)).toBe(
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('ExportDefaultDeclaration', () => {
const expectedTransformedContent = format`
"use strict";
module.exports.default = test;
module.exports.${DEFAULT_IMPORT_KEYWORD} = test;
function test(user) {
console.log(user);
}
Expand All @@ -71,7 +72,7 @@ describe('ExportDefaultDeclaration', () => {
"use strict";
const test = 'test';
module.exports.default = test;
module.exports.${DEFAULT_IMPORT_KEYWORD} = test;
`;

expect(contentToTransformedContent(content)).toBe(
Expand Down
39 changes: 33 additions & 6 deletions src/transform/test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,45 @@ import transform from '../../index.js';

const contentToAST = (content) => parseSync(content);

export const format = (strings) => {
const source = strings.join('').trim();
const lines = source.split('\n');
const getReplacementMap = (lines, values) => {
const replacements = new Map();
const usedValues = values;

lines.forEach((line) => {
const match = line.match(/\${(.*?)}/);
if (match) {
const placeholder = match[0];
const value = usedValues.shift();
replacements.set(placeholder, value);
}
});

return replacements;
};

const replacePlaceholderToValue = (source, replacements) => {
let replacedSource = source;

if (lines.length === 1) {
return source;
for (const [placeholder, value] of replacements) {
replacedSource = replacedSource.replace(placeholder, value);
}

return replacedSource;
};

export const format = (strings, ...values) => {
const source = String.raw(strings, ...values).trim();
const lines = source.split('\n');

if (lines.length === 1) return source;

const space = lines[lines.length - 1].match(/\s+/)[0];
const exp = new RegExp(`${space}`, 'g');

return source.replace(exp, '');
const replacements = getReplacementMap(lines, values);
const replacedSource = replacePlaceholderToValue(source, replacements);

return replacedSource.replace(exp, '');
};

export const contentToTransformedContent = (content) => {
Expand Down

0 comments on commit 60640fd

Please sign in to comment.