From 25cacbd7f3f7310a71f3f6e3131f4cc8fb866442 Mon Sep 17 00:00:00 2001 From: Michael Rawlings Date: Thu, 19 Dec 2024 18:09:01 -0500 Subject: [PATCH] refactor: better filename check for tags/components? --- .../src/feature-detection.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/translator-interop/src/feature-detection.ts b/packages/translator-interop/src/feature-detection.ts index 499332b78d..054329dd95 100644 --- a/packages/translator-interop/src/feature-detection.ts +++ b/packages/translator-interop/src/feature-detection.ts @@ -2,7 +2,6 @@ import type { types as t } from "@marko/compiler"; import { getTagDef } from "@marko/compiler/babel-utils"; import { taglibs as taglibs6 } from "@marko/runtime-tags/translator"; import { taglibs as taglibs5 } from "marko/translator"; -import { sep } from "path"; import { buildAggregateError } from "./build-aggregate-error"; @@ -27,16 +26,7 @@ export function isTagsAPI(path: t.NodePath) { let featureType = file.path.node.extra?.featureType; if (!featureType) { - let forceTags = false; - - if (file.opts.filename) { - const filename = file.opts.filename; - const tagsIndex = filename.lastIndexOf(sep + "tags" + sep); - const componentsIndex = filename.lastIndexOf(sep + "components" + sep); - if (tagsIndex > componentsIndex) { - forceTags = true; - } - } + const forceTags = isTagsAPIFromFileName(file.opts.filename); if ((file as any).___compileStage === "parse") { featureType = forceTags ? FeatureType.Tags : DEFAULT_FEATURE_TYPE; @@ -65,6 +55,24 @@ export function isTagsAPI(path: t.NodePath) { return featureType === FeatureType.Tags; } +function isTagsAPIFromFileName(filename: string) { + for (let end = filename.length, i = end; --i; ) { + switch (filename[i]) { + case "/": + case "\\": + if (filename.startsWith("tags" + filename[i], i + 1)) { + return true; + } else if (filename.startsWith("components" + filename[i], i + 1)) { + return false; + } + + end = i; + break; + } + } + return false; +} + const featureDetectionVisitor = { MarkoComment(comment, state) { if (/^\s*use tags\s*$/.test(comment.node.value)) {