Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(isTypeScriptComponent): check all script tags (#933) #934

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/core/src/parsers/svelte/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { SveltosisComponent } from '../types';
function handleExportNamedDeclaration(json: SveltosisComponent, node: ExportNamedDeclaration) {
const declarations = (node.declaration as VariableDeclaration)?.declarations;

// FIXME(milahu): loop declarations
//for (const declaration of node.declarations) {}
if (declarations?.length) {
const declaration = declarations[0];
const property = (declaration.id as Identifier).name;
Expand All @@ -27,13 +29,25 @@ function handleExportNamedDeclaration(json: SveltosisComponent, node: ExportName
}
}

function handleVariableDeclaration(_json: SveltosisComponent, _node: VariableDeclaration) {
throw new Error('not implemented: VariableDeclaration in svelte <script context="module">');
// TODO(milahu): implement, similar to handleExportNamedDeclaration.
// VariableDeclaration in <script context="module">
// has the meaning of a static variable.
// https://svelte.dev/docs#component-format-script-context-module
//for (const declaration of node.declarations) {}
}

export function parseModule(ast: Ast, json: SveltosisComponent) {
walk(ast.module as BaseNode, {
enter(node) {
switch (node.type) {
case 'ExportNamedDeclaration':
handleExportNamedDeclaration(json, node as ExportNamedDeclaration);
break;
case 'VariableDeclaration':
handleVariableDeclaration(json, node as VariableDeclaration);
break;
}
},
});
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/parsers/svelte/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import type { SveltosisComponent } from '../types';

export function isTypeScriptComponent(string_: string) {
const regex = createTagRegex('script', 'gi');
const match = regex.exec(string_);
const { lang } = parseAttributes((match?.length && match[1]) || '');
return lang === 'ts';
let isTypeScript = false;
// match all
string_.replace(regex, (...match) => {
const { lang } = parseAttributes((match?.length && match[1]) || '');
if (lang === 'ts') isTypeScript = true;
return '';
});
return isTypeScript;
}

/** Create a tag matching regexp. */
Expand Down