Skip to content

Commit a95768f

Browse files
authored
Merge pull request #344 from marko-js/ignore-non-marko-missing-types
fix: ignore tag types for non-marko templates
2 parents 5fca802 + ed43a8d commit a95768f

File tree

9 files changed

+91
-16
lines changed

9 files changed

+91
-16
lines changed

.changeset/happy-glasses-trade.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@marko/language-server": patch
3+
"@marko/language-tools": patch
4+
"@marko/type-check": patch
5+
"marko-vscode": patch
6+
---
7+
8+
Ignore types for non-Marko templates to remove errors for missing types

packages/language-server/src/__tests__/fixtures/script/import-without-types/__snapshots__/import-without-types.expected/index.html

Whitespace-only changes.

packages/language-server/src/__tests__/fixtures/script/import-without-types/__snapshots__/import-without-types.expected/index.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
export interface Input {}
2+
abstract class Component extends Marko.Component<Input> {}
3+
export { type Component };
4+
(function (this: void) {
5+
const input = Marko._.any as Input;
6+
const component = Marko._.any as Component;
7+
const state = Marko._.state(component);
8+
const out = Marko._.out;
9+
const $signal = Marko._.any as AbortSignal;
10+
const $global = Marko._.getGlobal(
11+
// @ts-expect-error We expect the compiler to error because we are checking if the MarkoRun.Context is defined.
12+
(Marko._.error, Marko._.any as MarkoRun.Context),
13+
);
14+
Marko._.noop({ component, state, out, input, $global, $signal });
15+
const Child = null;
16+
const __marko_internal_tag_1 = Marko._.fallbackTemplate(Child, Marko._.any);
17+
Marko._.renderDynamicTag(__marko_internal_tag_1)()()({
18+
a: 1,
19+
});
20+
Marko._.renderDynamicTag(Marko._.missingTag)()()({
21+
a: 1,
22+
});
23+
return;
24+
})();
25+
export default new (class Template extends Marko._.Template<{
26+
render(
27+
input: Marko.TemplateInput<Input>,
28+
stream?: {
29+
write: (chunk: string) => void;
30+
end: (chunk?: string) => void;
31+
},
32+
): Marko.Out<Component>;
33+
34+
render(
35+
input: Marko.TemplateInput<Input>,
36+
cb?: (err: Error | null, result: Marko.RenderResult<Component>) => void,
37+
): Marko.Out<Component>;
38+
39+
renderSync(input: Marko.TemplateInput<Input>): Marko.RenderResult<Component>;
40+
41+
renderToString(input: Marko.TemplateInput<Input>): string;
42+
43+
stream(
44+
input: Marko.TemplateInput<Input>,
45+
): ReadableStream<string> & NodeJS.ReadableStream;
46+
47+
api: "class";
48+
_(): () => <__marko_internal_input extends unknown>(
49+
input: Marko.Directives &
50+
Input &
51+
Marko._.Relate<__marko_internal_input, Marko.Directives & Input>,
52+
) => Marko._.ReturnWithScope<__marko_internal_input, void>;
53+
}> {})();

packages/language-server/src/__tests__/fixtures/script/import-without-types/components/Child/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"renderer": "./index.js"
3+
}

packages/language-server/src/__tests__/fixtures/script/import-without-types/components/child-other/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"renderer": "./index.js"
3+
}

packages/language-tools/src/extractors/script/index.ts

+24-16
Original file line numberDiff line numberDiff line change
@@ -718,34 +718,42 @@ constructor(_?: Return) {}
718718
const renderId = this.#getRenderId(tag);
719719
const def = tagName ? this.#lookup.getTag(tagName) : undefined;
720720
const isHTML = def?.html;
721-
const importPath = !isHTML && resolveTagImport(this.#filename, def);
721+
const importPath = !isHTML
722+
? resolveTagImport(this.#filename, def)
723+
: undefined;
722724
let tagIdentifier: undefined | string;
723725
let isTemplate = false;
724726

725727
if (!isHTML && (!def || importPath)) {
726-
const tagId = this.#ensureTagId(tag);
727-
tagIdentifier = varLocal("tag_" + tagId);
728-
this.#extractor.write(`const ${tagIdentifier} = (\n`);
728+
const isIdentifier = tagName && REG_TAG_NAME_IDENTIFIER.test(tagName);
729+
const isMarkoFile = importPath?.endsWith(".marko");
729730

730-
if (tagName && REG_TAG_NAME_IDENTIFIER.test(tagName)) {
731-
if (importPath) {
731+
if (isIdentifier || isMarkoFile || !importPath) {
732+
tagIdentifier = varLocal("tag_" + this.#ensureTagId(tag));
733+
this.#extractor.write(`const ${tagIdentifier} = (\n`);
734+
735+
if (isIdentifier) {
736+
if (importPath) {
737+
this.#extractor.write(
738+
`${varShared("fallbackTemplate")}(${tagName},${isMarkoFile ? `import("${importPath}")` : varShared("any")})`,
739+
);
740+
} else {
741+
this.#extractor.copy(tag.name);
742+
}
743+
} else if (isMarkoFile) {
744+
isTemplate = true;
732745
this.#extractor.write(
733-
`${varShared("fallbackTemplate")}(${tagName},import("${importPath}"))`,
746+
`${varShared("resolveTemplate")}(import("${importPath}"))`,
734747
);
735748
} else {
736-
this.#extractor.copy(tag.name);
749+
this.#writeDynamicTagName(tag);
737750
}
738-
} else if (importPath) {
739-
isTemplate = importPath.endsWith(".marko");
740-
this.#extractor.write(
741-
`${varShared("resolveTemplate")}(import("${importPath}"))`,
742-
);
751+
752+
this.#extractor.write("\n);\n");
743753
} else {
744-
this.#writeDynamicTagName(tag);
754+
tagIdentifier = varShared("missingTag");
745755
}
746756

747-
this.#extractor.write("\n);\n");
748-
749757
const attrTagTree = this.#getAttrTagTree(tag);
750758
if (attrTagTree) {
751759
this.#writeAttrTagTree(attrTagTree, tagIdentifier);

0 commit comments

Comments
 (0)