From ec106126546f267d874c575911de66405501f2a4 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:48:09 -0700 Subject: [PATCH 1/5] chore: remove strip-indent dependency --- package.json | 3 +-- pnpm-lock.yaml | 3 --- src/modules/prepareContent.ts | 28 +++++++++++++++++++++++++--- src/types/modules.d.ts | 1 - 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index fdd234cc..531cb610 100644 --- a/package.json +++ b/package.json @@ -85,8 +85,7 @@ "vitest": "^1.6.0" }, "dependencies": { - "detect-indent": "^6.1.0", - "strip-indent": "^3.0.0" + "detect-indent": "^6.1.0" }, "peerDependencies": { "@babel/core": "^7.10.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5c1446b..950f5fed 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,9 +11,6 @@ importers: detect-indent: specifier: ^6.1.0 version: 6.1.0 - strip-indent: - specifier: ^3.0.0 - version: 3.0.0 devDependencies: '@babel/core': specifier: ^7.23.6 diff --git a/src/modules/prepareContent.ts b/src/modules/prepareContent.ts index b296edb5..1007f035 100644 --- a/src/modules/prepareContent.ts +++ b/src/modules/prepareContent.ts @@ -1,6 +1,4 @@ -import stripIndent from 'strip-indent'; - -// todo: could use magig-string and generate some sourcemaps 🗺 +// todo: could use magic-string and generate some sourcemaps 🗺 export function prepareContent({ options, content, @@ -22,3 +20,27 @@ export function prepareContent({ return content; } + +/** Get the shortest leading whitespace from lines in a string */ +const minIndent = (s: string) => { + const match = s.match(/^[ \t]*(?=\S)/gm); + + if (!match) { + return 0; + } + + return match.reduce((r, a) => Math.min(r, a.length), Infinity); +}; + +/** Strip leading whitespace from each line in a string */ +function stripIndent(s: string) { + const indent = minIndent(s); + + if (indent === 0) { + return s; + } + + const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm'); + + return s.replace(regex, ''); +} diff --git a/src/types/modules.d.ts b/src/types/modules.d.ts index e7626816..6b524a71 100644 --- a/src/types/modules.d.ts +++ b/src/types/modules.d.ts @@ -1,6 +1,5 @@ declare module 'svelte/package.json'; declare module 'coffeescript'; -declare module 'strip-indent'; declare module 'postcss-load-config'; declare module 'less'; declare module 'sorcery'; From 394b7bd5f7e0b5f39e60cbb6613fb8b72d48d515 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:54:14 -0700 Subject: [PATCH 2/5] remove detect-indent --- package.json | 3 --- pnpm-lock.yaml | 10 ---------- src/transformers/pug.ts | 42 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 531cb610..4e67d3a5 100644 --- a/package.json +++ b/package.json @@ -84,9 +84,6 @@ "typescript": "^5.0.2", "vitest": "^1.6.0" }, - "dependencies": { - "detect-indent": "^6.1.0" - }, "peerDependencies": { "@babel/core": "^7.10.2", "coffeescript": "^2.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 950f5fed..aab9c168 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,10 +7,6 @@ settings: importers: .: - dependencies: - detect-indent: - specifier: ^6.1.0 - version: 6.1.0 devDependencies: '@babel/core': specifier: ^7.23.6 @@ -1971,10 +1967,6 @@ packages: resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==} engines: {node: '>=6'} - detect-indent@6.1.0: - resolution: {integrity: sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA==} - engines: {node: '>=8'} - detect-newline@3.1.0: resolution: {integrity: sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==} engines: {node: '>=8'} @@ -6561,8 +6553,6 @@ snapshots: dequal@2.0.3: {} - detect-indent@6.1.0: {} - detect-newline@3.1.0: optional: true diff --git a/src/transformers/pug.ts b/src/transformers/pug.ts index b02192bf..dfad0609 100644 --- a/src/transformers/pug.ts +++ b/src/transformers/pug.ts @@ -1,4 +1,3 @@ -import detectIndent from 'detect-indent'; import pug from 'pug'; import type { Transformer, Options } from '../types'; @@ -67,8 +66,8 @@ const transformer: Transformer = async ({ ...options, }; - const { type: indentationType } = detectIndent(content); - const input = `${GET_MIXINS(indentationType ?? 'space')}\n${content}`; + const spaces = guessIndentString(content); + const input = `${GET_MIXINS(spaces ? 'space' : 'tab')}\n${content}`; const compiled = pug.compile( input, pugOptions, @@ -94,4 +93,41 @@ const transformer: Transformer = async ({ }; }; +// Sourced from `golden-fleece` +// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302 +function guessIndentString(str: string): number | undefined { + const lines = str.split('\n'); + + let tabs = 0; + let spaces = 0; + let minSpaces = 8; + + lines.forEach((line) => { + const match = /^(?: +|\t+)/.exec(line); + + if (!match) return; + + const [whitespace] = match; + + if (whitespace.length === line.length) return; + + if (whitespace[0] === '\t') { + tabs += 1; + } else { + spaces += 1; + if (whitespace.length > 1 && whitespace.length < minSpaces) { + minSpaces = whitespace.length; + } + } + }); + + if (spaces > tabs) { + let result = ''; + + while (minSpaces--) result += ' '; + + return result.length; + } +} + export { transformer }; From 0a51eac96374272d8e999ef579d9ccd95171e571 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:44:58 +0200 Subject: [PATCH 3/5] code style --- src/modules/prepareContent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/prepareContent.ts b/src/modules/prepareContent.ts index 1007f035..bdd8d83c 100644 --- a/src/modules/prepareContent.ts +++ b/src/modules/prepareContent.ts @@ -22,7 +22,7 @@ export function prepareContent({ } /** Get the shortest leading whitespace from lines in a string */ -const minIndent = (s: string) => { +function minIndent (s: string) { const match = s.match(/^[ \t]*(?=\S)/gm); if (!match) { From 76e3315bc902fe55104275bdcfb30df0487b8312 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:47:50 +0200 Subject: [PATCH 4/5] lint --- src/modules/prepareContent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/prepareContent.ts b/src/modules/prepareContent.ts index bdd8d83c..d5e60ebd 100644 --- a/src/modules/prepareContent.ts +++ b/src/modules/prepareContent.ts @@ -30,7 +30,7 @@ function minIndent (s: string) { } return match.reduce((r, a) => Math.min(r, a.length), Infinity); -}; +} /** Strip leading whitespace from each line in a string */ function stripIndent(s: string) { From a95911096508affa8b3f075c3cc3a9a26db139fd Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:53:19 +0200 Subject: [PATCH 5/5] lint --- src/modules/prepareContent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/prepareContent.ts b/src/modules/prepareContent.ts index d5e60ebd..5dadf5d9 100644 --- a/src/modules/prepareContent.ts +++ b/src/modules/prepareContent.ts @@ -22,7 +22,7 @@ export function prepareContent({ } /** Get the shortest leading whitespace from lines in a string */ -function minIndent (s: string) { +function minIndent(s: string) { const match = s.match(/^[ \t]*(?=\S)/gm); if (!match) {