Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,32 @@ function isNewGroup(

return false;
}

/**
* ensure it's in a newline.
* if file has module script ensure an empty line to separate imports
*/
export function handleFirstInstanceImport(
tsAst: ts.SourceFile,
astOffset: number,
hasModuleScript: boolean,
str: MagicString
) {
const firstImport = tsAst.statements
.filter(ts.isImportDeclaration)
.sort((a, b) => a.end - b.end)[0];
if (!firstImport) {
return;
}

const firstComment = Array.from(
ts.getLeadingCommentRanges(firstImport.getFullText(), 0) ?? []
).sort((a, b) => a.pos - b.pos)[0];

const start =
firstComment && firstComment.kind === ts.SyntaxKind.MultiLineCommentTrivia
? firstComment.pos + firstImport.getFullStart()
: firstImport.getStart();

str.appendRight(start + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { ImplicitStoreValues } from './nodes/ImplicitStoreValues';
import { Generics } from './nodes/Generics';
import { is$$SlotsDeclaration } from './nodes/slot';
import { preprendStr } from '../utils/magic-string';
import { handleImportDeclaration } from './nodes/handleImportDeclaration';
import {
handleFirstInstanceImport,
handleImportDeclaration
} from './nodes/handleImportDeclaration';

export interface InstanceScriptProcessResult {
exportedNames: ExportedNames;
Expand Down Expand Up @@ -259,14 +262,7 @@ export function processInstanceScriptContent(
implicitTopLevelNames.modifyCode(rootScope.declared);
implicitStoreValues.modifyCode(astOffset, str);

const firstImport = tsAst.statements
.filter(ts.isImportDeclaration)
.sort((a, b) => a.end - b.end)[0];
if (firstImport) {
// ensure it's in a newline.
// if file has module script ensure an empty line to separate imports
str.appendRight(firstImport.getStart() + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
}
handleFirstInstanceImport(tsAst, astOffset, hasModuleScript, str);

if (mode === 'dts') {
// Transform interface declarations to type declarations because indirectly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
///<reference types="svelte" />
<></>;// non-leading comment
/**@typedef {{ a: string }} Foo */

import ''
function render() {





;
() => (<></>);
return { props: {}, slots: {}, getters: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
///<reference types="svelte" />
;// non-leading comment
/**@typedef {{ a: string }} Foo */

import ''
function render() {





;
async () => {};
return { props: {}, slots: {}, getters: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
// non-leading comment

/**@typedef {{ a: string }} Foo */
import ''
</script>