Skip to content

Commit

Permalink
(fix) new line before the JSDoc of the first import (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Sep 5, 2022
1 parent 7e496d3 commit 599ddfb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
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>

0 comments on commit 599ddfb

Please sign in to comment.