Skip to content

Commit

Permalink
refactor: better filename check for tags/components?
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Dec 19, 2024
1 parent 3183224 commit 25cacbd
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/translator-interop/src/feature-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 25cacbd

Please sign in to comment.