Skip to content

Commit

Permalink
Move normalizeWhitespace to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Dec 3, 2023
1 parent f25ef65 commit 1c408f1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
53 changes: 1 addition & 52 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Options } from '../options.js';
import type { GlimmerTemplate, RawGlimmerTemplate } from '../types/glimmer';
import { isDefaultTemplate } from '../types/glimmer';
import { assert } from '../utils';
import { normalizeWhitespace } from './whitespace';

const typescript = babelParsers['babel-ts'] as Parser<Node | undefined>;
const p = new Preprocessor();
Expand Down Expand Up @@ -118,58 +119,6 @@ function preprocess(code: string): Preprocessed {
return { results, code: output };
}

function replaceRange(
original: string,
range: { start: number; end: number },
substitute: string,
): string {
return (
original.slice(0, range.start) + substitute + original.slice(range.end)
);
}

const STATIC_OPEN = 'static{`';
const STATIC_CLOSE = '`}';
const NEWLINE = '\n';

function normalizeWhitespace(
templateNode: RawGlimmerTemplate,
originalCode: string,
currentCode: string,
): string {
let prefix: string;
let suffix: string;

if (templateNode.type === 'class-member') {
prefix = STATIC_OPEN;
suffix = STATIC_CLOSE;
} else {
const nextWord = originalCode.slice(templateNode.range.end).match(/\S+/);
prefix = '{';
suffix = '}';
if (nextWord && nextWord[0] === 'as') {
prefix = '(' + prefix;
suffix = suffix + ')';
} else if (!nextWord || ![',', ')'].includes(nextWord[0][0] || '')) {
suffix += ';';
}
}

const lineBreakCount = [...templateNode.contents].reduce(
(sum, currentContents) => sum + (currentContents === NEWLINE ? 1 : 0),
0,
);
const totalLength = templateNode.range.end - templateNode.range.start;
const spaces = totalLength - prefix.length - suffix.length - lineBreakCount;
const content = ' '.repeat(spaces) + NEWLINE.repeat(lineBreakCount);

return replaceRange(
currentCode,
templateNode.range,
`${prefix}${content}${suffix}`,
);
}

export const parser: Parser<Node | undefined> = {
...typescript,
astFormat: PRINTER_NAME,
Expand Down
53 changes: 53 additions & 0 deletions src/parse/whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { RawGlimmerTemplate } from '../types/glimmer';

const STATIC_OPEN = 'static{`';
const STATIC_CLOSE = '`}';
const NEWLINE = '\n';

function replaceRange(
original: string,
range: { start: number; end: number },
substitute: string,
): string {
return (
original.slice(0, range.start) + substitute + original.slice(range.end)
);
}

export function normalizeWhitespace(
templateNode: RawGlimmerTemplate,
originalCode: string,
currentCode: string,
): string {
let prefix: string;
let suffix: string;

if (templateNode.type === 'class-member') {
prefix = STATIC_OPEN;
suffix = STATIC_CLOSE;
} else {
const nextWord = originalCode.slice(templateNode.range.end).match(/\S+/);
prefix = '{';
suffix = '}';
if (nextWord && nextWord[0] === 'as') {
prefix = '(' + prefix;
suffix = suffix + ')';
} else if (!nextWord || ![',', ')'].includes(nextWord[0][0] || '')) {
suffix += ';';
}
}

const lineBreakCount = [...templateNode.contents].reduce(
(sum, currentContents) => sum + (currentContents === NEWLINE ? 1 : 0),
0,
);
const totalLength = templateNode.range.end - templateNode.range.start;
const spaces = totalLength - prefix.length - suffix.length - lineBreakCount;
const content = ' '.repeat(spaces) + NEWLINE.repeat(lineBreakCount);

return replaceRange(
currentCode,
templateNode.range,
`${prefix}${content}${suffix}`,
);
}

0 comments on commit 1c408f1

Please sign in to comment.