Skip to content

Commit 6ed1c4b

Browse files
authored
Merge pull request #12 from mistlog/feature/inline-watch
Feature/inline watch
2 parents e89ef20 + fd74581 commit 6ed1c4b

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"dist"
2020
],
2121
"bin": {
22-
"sd": "cli/cli.js"
22+
"sd": "dist/cli/cli.js"
2323
},
2424
"main": "src/index.js",
2525
"types": "src/index.d.ts",
@@ -32,7 +32,7 @@
3232
"pretest": "npm run clean:ts && npm run transcribe",
3333
"test": "jest --coverage",
3434
"test:watch": "jest --watch",
35-
"prebuild": "npm run clean:dist",
35+
"prebuild": "npm run clean:dist && npm run clean:ts",
3636
"build": "npm run transcribe && tsc",
3737
"postbuild": "npm run clean:ts",
3838
"dev": "npm run transcribe:watch",

src/dsl/draft-dsl-svelte-watch.tsx

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import { IDSL } from "typedraft";
2-
import { Statement, labeledStatement, blockStatement, identifier } from "@babel/types";
2+
import {
3+
Statement,
4+
labeledStatement,
5+
blockStatement,
6+
identifier,
7+
isExpressionStatement,
8+
isStringLiteral,
9+
} from "@babel/types";
310

411
export class SvelteWatch implements IDSL {
12+
m_Merge: boolean;
13+
constructor() {
14+
this.m_Merge = true;
15+
}
16+
517
Transcribe(block: Array<Statement>): Array<Statement> {
6-
return [labeledStatement(identifier("$"), blockStatement(block))];
18+
const [use_watch, ...rest] = block;
19+
let to_watch = block;
20+
if (isExpressionStatement(use_watch) && isStringLiteral(use_watch.expression)) {
21+
// we are in inline watch
22+
to_watch = rest;
23+
}
24+
return [labeledStatement(identifier("$"), blockStatement(to_watch))];
725
}
826
}

test/section/__snapshots__/script-section.test.ts.snap

+8
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ $: {
4242
doubled = count * 2;
4343
}"
4444
`;
45+
46+
exports[`translate script section track change: inline 1`] = `
47+
"let count = 0;
48+
let doubled = 0;
49+
$: {
50+
doubled = count * 2;
51+
}"
52+
`;

test/section/script-section.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ describe("translate script section", () => {
4141
SnapshotTest(code);
4242
});
4343

44+
test("track change: inline", () => {
45+
const code = `
46+
export default function App()
47+
{
48+
let count = 0;
49+
let doubled = 0;
50+
51+
{
52+
"use watch";
53+
doubled = count * 2;
54+
}
55+
}
56+
`;
57+
58+
SnapshotTest(code);
59+
});
60+
4461
test("ignore variable declaration starts with $", () => {
4562
const code = `
4663
export default function App()

0 commit comments

Comments
 (0)