-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Related to Svelte AST node {@link ConstTag}. | ||
* @see {@link https://svelte.dev/docs/special-tags#const} | ||
* @module | ||
*/ | ||
|
||
import { print } from "esrap"; | ||
|
||
import { define_printer } from "#printer"; | ||
import type { ConstTag } from "#types"; | ||
import { insert } from "#util"; | ||
|
||
/** | ||
* Print Svelte AST node {@link ConstTag} as string. | ||
* @see {@link https://svelte.dev/docs/special-tags#const} | ||
* | ||
* @example pattern | ||
* ```svelte | ||
* {@const assignment} | ||
* ``` | ||
*/ | ||
export const print_const_tag = define_printer((node: ConstTag, _options) => { | ||
const { declaration } = node; | ||
|
||
return insert( | ||
"{@", | ||
// NOTE: It removes the semicolon | ||
print(declaration).code.slice(0, -1), | ||
"}", | ||
); | ||
}); | ||
|
||
if (import.meta.vitest) { | ||
const { describe, it } = import.meta.vitest; | ||
const [{ parse_and_extract_svelte_node }, { DEFAULT_OPTIONS }] = await Promise.all([ | ||
import("#test/mod"), | ||
import("#options"), | ||
]); | ||
|
||
describe("ConstTag", () => { | ||
it("prints correctly when used as direct child of allowed tags ", ({ expect }) => { | ||
const code = ` | ||
{#each boxes as box} | ||
{@const area = box.width * box.height} | ||
{box.width} * {box.height} = {area} | ||
{/each} | ||
`; | ||
const node = parse_and_extract_svelte_node<ConstTag>(code, "ConstTag"); | ||
expect(print_const_tag(node, DEFAULT_OPTIONS)).toMatchInlineSnapshot( | ||
`"{@const area = box.width * box.height}"`, | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters