Skip to content

Commit

Permalink
refactor: replace magic numbers with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ony3000 committed Feb 13, 2024
1 parent 5981edc commit 9f3d573
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/packages/core-parts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ function replaceClassName({
.split(EOL)
.slice(0, startLineIndex)
.reduce((textLength, line) => textLength + line.length + EOL.length, 0);
const firstLineIndentLength = indentUnit.length * baseIndentLevel;
const firstLinePadLength =
rangeStart +
1 -
totalTextLengthUptoPrevLine +
(indentUnit === '\t' ? 3 : 0) * baseIndentLevel;
totalTextLengthUptoPrevLine -
firstLineIndentLength +
(indentUnit === TAB ? TAB_SIZE : indentUnit.length) * baseIndentLevel;

// preprocess (first+1)
const classNameWithFirstLinePadding = `${PH.repeat(
Expand Down Expand Up @@ -169,7 +171,7 @@ function replaceClassName({
}

const multiLinePadLength =
(indentUnit === '\t' ? 4 : indentUnit.length) * multiLineIndentLevel;
(indentUnit === TAB ? TAB_SIZE : indentUnit.length) * multiLineIndentLevel;

const formattedRest = format(rest.join(EOL), {
...options,
Expand Down Expand Up @@ -236,7 +238,7 @@ export function parseLineByLineAndReplace({
return formattedText;
}

const indentUnit = options.useTabs ? '\t' : ' '.repeat(options.tabWidth);
const indentUnit = options.useTabs ? TAB : SPACE.repeat(options.tabWidth);

let targetClassNameNodes: ClassNameNode[] = [];
switch (options.parser) {
Expand Down Expand Up @@ -312,11 +314,13 @@ async function replaceClassNameAsync({
.split(EOL)
.slice(0, startLineIndex)
.reduce((textLength, line) => textLength + line.length + EOL.length, 0);
const firstLineIndentLength = indentUnit.length * baseIndentLevel;
const firstLinePadLength =
rangeStart +
1 -
totalTextLengthUptoPrevLine +
(indentUnit === '\t' ? 3 : 0) * baseIndentLevel;
totalTextLengthUptoPrevLine -
firstLineIndentLength +
(indentUnit === TAB ? TAB_SIZE : indentUnit.length) * baseIndentLevel;

// preprocess (first+1)
const classNameWithFirstLinePadding = `${PH.repeat(
Expand Down Expand Up @@ -346,7 +350,7 @@ async function replaceClassNameAsync({
}

const multiLinePadLength =
(indentUnit === '\t' ? 4 : indentUnit.length) * multiLineIndentLevel;
(indentUnit === TAB ? TAB_SIZE : indentUnit.length) * multiLineIndentLevel;

const formattedRest = (
await format(rest.join(EOL), {
Expand Down Expand Up @@ -415,7 +419,7 @@ export async function parseLineByLineAndReplaceAsync({
return formattedText;
}

const indentUnit = options.useTabs ? '\t' : ' '.repeat(options.tabWidth);
const indentUnit = options.useTabs ? TAB : SPACE.repeat(options.tabWidth);

let targetClassNameNodes: ClassNameNode[] = [];
switch (options.parser) {
Expand Down

0 comments on commit 9f3d573

Please sign in to comment.