Skip to content

Commit 394b7bd

Browse files
committed
remove detect-indent
1 parent ec10612 commit 394b7bd

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@
8484
"typescript": "^5.0.2",
8585
"vitest": "^1.6.0"
8686
},
87-
"dependencies": {
88-
"detect-indent": "^6.1.0"
89-
},
9087
"peerDependencies": {
9188
"@babel/core": "^7.10.2",
9289
"coffeescript": "^2.5.1",

pnpm-lock.yaml

-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/transformers/pug.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import detectIndent from 'detect-indent';
21
import pug from 'pug';
32

43
import type { Transformer, Options } from '../types';
@@ -67,8 +66,8 @@ const transformer: Transformer<Options.Pug> = async ({
6766
...options,
6867
};
6968

70-
const { type: indentationType } = detectIndent(content);
71-
const input = `${GET_MIXINS(indentationType ?? 'space')}\n${content}`;
69+
const spaces = guessIndentString(content);
70+
const input = `${GET_MIXINS(spaces ? 'space' : 'tab')}\n${content}`;
7271
const compiled = pug.compile(
7372
input,
7473
pugOptions,
@@ -94,4 +93,41 @@ const transformer: Transformer<Options.Pug> = async ({
9493
};
9594
};
9695

96+
// Sourced from `golden-fleece`
97+
// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
98+
function guessIndentString(str: string): number | undefined {
99+
const lines = str.split('\n');
100+
101+
let tabs = 0;
102+
let spaces = 0;
103+
let minSpaces = 8;
104+
105+
lines.forEach((line) => {
106+
const match = /^(?: +|\t+)/.exec(line);
107+
108+
if (!match) return;
109+
110+
const [whitespace] = match;
111+
112+
if (whitespace.length === line.length) return;
113+
114+
if (whitespace[0] === '\t') {
115+
tabs += 1;
116+
} else {
117+
spaces += 1;
118+
if (whitespace.length > 1 && whitespace.length < minSpaces) {
119+
minSpaces = whitespace.length;
120+
}
121+
}
122+
});
123+
124+
if (spaces > tabs) {
125+
let result = '';
126+
127+
while (minSpaces--) result += ' ';
128+
129+
return result.length;
130+
}
131+
}
132+
97133
export { transformer };

0 commit comments

Comments
 (0)