Skip to content

Commit aedf69c

Browse files
authored
Add compilation variables report option to allow getting all variables (even undeclared or internal) (#6192)
* feat: add compilation variables report option * test: add full var report test * docs: document varsReport compile option
1 parent 487190f commit aedf69c

File tree

12 files changed

+129
-16
lines changed

12 files changed

+129
-16
lines changed

site/content/docs/04-compile-time.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ The following options can be passed to the compiler. None are required:
4444
| `filename` | string | `null`
4545
| `name` | string | `"Component"`
4646
| `format` | `"esm"` or `"cjs"` | `"esm"`
47-
| `generate` | `"dom"` or `"ssr"` | `"dom"`
47+
| `generate` | `"dom"` or `"ssr" or false` | `"dom"`
48+
| `varsReport` | `"strict"` or `"full" or false` | `"strict"`
4849
| `dev` | boolean | `false`
4950
| `immutable` | boolean | `false`
5051
| `hydratable` | boolean | `false`
@@ -66,6 +67,7 @@ The following options can be passed to the compiler. None are required:
6667
| `name` | `"Component"` | `string` that sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). It will normally be inferred from `filename`.
6768
| `format` | `"esm"` | If `"esm"`, creates a JavaScript module (with `import` and `export`). If `"cjs"`, creates a CommonJS module (with `require` and `module.exports`), which is useful in some server-side rendering situations or for testing.
6869
| `generate` | `"dom"` | If `"dom"`, Svelte emits a JavaScript class for mounting to the DOM. If `"ssr"`, Svelte emits an object with a `render` method suitable for server-side rendering. If `false`, no JavaScript or CSS is returned; just metadata.
70+
| `varsReport` | `"strict"` | If `"strict"`, Svelte returns a variables report with only variables that are not globals nor internals. If `"full"`, Svelte returns a variables report with all detected variables. If `false`, no variables report is returned.
6971
| `dev` | `false` | If `true`, causes extra code to be added to components that will perform runtime checks and provide debugging information during development.
7072
| `immutable` | `false` | If `true`, tells the compiler that you promise not to mutate any objects. This allows it to be less conservative about checking whether values have changed.
7173
| `hydratable` | `false` | If `true` when generating DOM code, enables the `hydrate: true` runtime option, which allows a component to upgrade existing DOM rather than creating new DOM from scratch. When generating SSR code, this adds markers to `<head>` elements so that hydration knows which to replace.

src/compiler/compile/Component.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ export default class Component {
183183
this.stylesheet.warn_on_unused_selectors(this);
184184
}
185185

186-
add_var(variable: Var) {
186+
add_var(variable: Var, add_to_lookup = true) {
187187
this.vars.push(variable);
188-
this.var_lookup.set(variable.name, variable);
188+
189+
if (add_to_lookup) {
190+
this.var_lookup.set(variable.name, variable);
191+
}
189192
}
190193

191194
add_reference(name: string) {
@@ -216,6 +219,10 @@ export default class Component {
216219
variable.subscribable = true;
217220
}
218221
} else {
222+
if (this.compile_options.varsReport === 'full') {
223+
this.add_var({ name, referenced: true }, false);
224+
}
225+
219226
this.used_names.add(name);
220227
}
221228
}
@@ -340,19 +347,7 @@ export default class Component {
340347
css,
341348
ast: this.original_ast,
342349
warnings: this.warnings,
343-
vars: this.vars
344-
.filter(v => !v.global && !v.internal)
345-
.map(v => ({
346-
name: v.name,
347-
export_name: v.export_name || null,
348-
injected: v.injected || false,
349-
module: v.module || false,
350-
mutated: v.mutated || false,
351-
reassigned: v.reassigned || false,
352-
referenced: v.referenced || false,
353-
writable: v.writable || false,
354-
referenced_from_script: v.referenced_from_script || false
355-
})),
350+
vars: this.get_vars_report(),
356351
stats: this.stats.render()
357352
};
358353
}
@@ -402,6 +397,28 @@ export default class Component {
402397
};
403398
}
404399

400+
get_vars_report(): Var[] {
401+
const { compile_options, vars } = this;
402+
403+
const vars_report = compile_options.varsReport === false
404+
? []
405+
: compile_options.varsReport === 'full'
406+
? vars
407+
: vars.filter(v => !v.global && !v.internal);
408+
409+
return vars_report.map(v => ({
410+
name: v.name,
411+
export_name: v.export_name || null,
412+
injected: v.injected || false,
413+
module: v.module || false,
414+
mutated: v.mutated || false,
415+
reassigned: v.reassigned || false,
416+
referenced: v.referenced || false,
417+
writable: v.writable || false,
418+
referenced_from_script: v.referenced_from_script || false
419+
}));
420+
}
421+
405422
error(
406423
pos: {
407424
start: number;

src/compiler/compile/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const valid_options = [
1414
'filename',
1515
'sourcemap',
1616
'generate',
17+
'varsReport',
1718
'outputFilename',
1819
'cssOutputFilename',
1920
'sveltePath',

src/compiler/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export interface CompileOptions {
116116
name?: string;
117117
filename?: string;
118118
generate?: 'dom' | 'ssr' | false;
119+
varsReport?: 'full' | 'strict' | false;
119120

120121
sourcemap?: object | string;
121122
outputFilename?: string;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
options: {
3+
varsReport: false
4+
},
5+
6+
test(assert, vars) {
7+
assert.deepEqual(vars, []);
8+
}
9+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let foo = "bar";
3+
</script>
4+
5+
{foo}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
options: {
3+
varsReport: 'full'
4+
},
5+
6+
test(assert, vars) {
7+
assert.deepEqual(vars, [{
8+
name: 'foo',
9+
export_name: null,
10+
injected: false,
11+
module: false,
12+
mutated: false,
13+
reassigned: false,
14+
referenced: true,
15+
referenced_from_script: false,
16+
writable: false
17+
}]);
18+
}
19+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{foo}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default {
2+
options: {
3+
varsReport: 'full'
4+
},
5+
6+
test(assert, vars) {
7+
assert.deepEqual(vars, [
8+
{
9+
name: 'foo',
10+
export_name: 'foo',
11+
injected: false,
12+
module: false,
13+
mutated: false,
14+
reassigned: false,
15+
referenced: true,
16+
referenced_from_script: false,
17+
writable: true
18+
}, {
19+
name: 'bar',
20+
export_name: null,
21+
injected: false,
22+
module: false,
23+
mutated: false,
24+
reassigned: false,
25+
referenced: true,
26+
referenced_from_script: false,
27+
writable: false
28+
}
29+
]);
30+
}
31+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let foo = "bar";
3+
</script>
4+
5+
{foo} {bar}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
options: {
3+
varsReport: 'full'
4+
},
5+
6+
test(assert, vars) {
7+
assert.deepEqual(vars, [{
8+
name: 'foo',
9+
export_name: null,
10+
injected: false,
11+
module: false,
12+
mutated: false,
13+
reassigned: false,
14+
referenced: true,
15+
referenced_from_script: false,
16+
writable: false
17+
}]);
18+
}
19+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script></script>
2+
3+
{foo}

0 commit comments

Comments
 (0)