Skip to content

Commit 49619c0

Browse files
committed
Implement framework detection
1 parent 5ce339c commit 49619c0

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/printer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import type { PugFramework } from './options/pug-framework';
6969
import type { PugIdNotation } from './options/pug-id-notation';
7070
import { isAngularAction, isAngularBinding, isAngularDirective, isAngularInterpolation } from './utils/angular';
7171
import {
72+
detectFramework,
7273
handleBracketSpacing,
7374
isMultilineInterpolation,
7475
isQuoted,
@@ -155,6 +156,8 @@ export class PugPrinter {
155156
private readonly indentString: string;
156157
private indentLevel: number = 0;
157158

159+
private readonly framework: PugFramework = 'none';
160+
158161
private readonly quotes: "'" | '"';
159162
private readonly otherQuotes: "'" | '"';
160163

@@ -199,6 +202,7 @@ export class PugPrinter {
199202
if (options.pugSingleFileComponentIndentation) {
200203
this.indentLevel++;
201204
}
205+
this.framework = options.pugFramework !== 'none' ? options.pugFramework : detectFramework();
202206

203207
this.quotes = this.options.pugSingleQuote ? "'" : '"';
204208
this.otherQuotes = this.options.pugSingleQuote ? '"' : "'";
@@ -371,7 +375,7 @@ export class PugPrinter {
371375

372376
private frameworkFormat(code: string): string {
373377
const options: Options = { ...this.codeInterpolationOptions };
374-
switch (this.options.pugFramework) {
378+
switch (this.framework) {
375379
case 'angular':
376380
options.parser = '__ng_interpolation';
377381
break;
@@ -421,7 +425,6 @@ export class PugPrinter {
421425
code = this.frameworkFormat(code);
422426
}
423427
} catch (error: unknown) {
424-
const { pugFramework } = this.options;
425428
if (typeof error === 'string') {
426429
if (error.includes('Unexpected token Lexer Error')) {
427430
if (!error.includes('Unexpected character [`]')) {
@@ -433,21 +436,21 @@ export class PugPrinter {
433436
`code: \`${code.trim()}\``
434437
);
435438
} else if (error.includes("Unexpected token '('")) {
436-
if (pugFramework !== 'vue') {
439+
if (this.framework !== 'vue') {
437440
logger.warn(
438441
'[PugPrinter:formatText]: Found unexpected token `(`.',
439442
`code: \`${code.trim()}\``
440443
);
441444
}
442445
} else if (error.includes('Missing expected `)`')) {
443-
if (pugFramework !== 'vue') {
446+
if (this.framework !== 'vue') {
444447
logger.warn(
445448
'[PugPrinter:formatText]: Missing expected `)`.',
446449
`code: \`${code.trim()}\``
447450
);
448451
}
449452
} else if (error.includes('Missing expected `:`')) {
450-
if (pugFramework !== 'vue') {
453+
if (this.framework !== 'vue') {
451454
logger.warn(
452455
'[PugPrinter:formatText]: Missing expected `:`.',
453456
`code: \`${code.trim()}\``

src/utils/common.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AttributeToken, TagToken, Token } from 'pug-lexer';
2+
import type { PugFramework } from '../options/pug-framework';
23

34
/**
45
* Returns the previous tag token if there was one.
@@ -191,3 +192,23 @@ export function makeString(
191192
);
192193
return enclosingQuote + newContent + enclosingQuote;
193194
}
195+
196+
/**
197+
* Try to detect used framework within the project by reading `process.env.npm_package_dependencies_*`.
198+
*
199+
* @returns PugFramework.
200+
*/
201+
export function detectFramework(): PugFramework {
202+
try {
203+
if (process.env.npm_package_dependencies_vue) {
204+
return 'vue';
205+
} else if (process.env.npm_package_dependencies_svelte) {
206+
return 'svelte';
207+
} else if (process.env.npm_package_dependencies_angular) {
208+
return 'angular';
209+
}
210+
} catch {
211+
return 'none';
212+
}
213+
return 'none';
214+
}

0 commit comments

Comments
 (0)