Skip to content

Add compilation variables report option to allow getting all variables (even undeclared or internal) #6192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2021
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
4 changes: 3 additions & 1 deletion site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ The following options can be passed to the compiler. None are required:
| `filename` | string | `null`
| `name` | string | `"Component"`
| `format` | `"esm"` or `"cjs"` | `"esm"`
| `generate` | `"dom"` or `"ssr"` | `"dom"`
| `generate` | `"dom"` or `"ssr" or false` | `"dom"`
| `varsReport` | `"strict"` or `"full" or false` | `"strict"`
| `dev` | boolean | `false`
| `immutable` | boolean | `false`
| `hydratable` | boolean | `false`
Expand All @@ -66,6 +67,7 @@ The following options can be passed to the compiler. None are required:
| `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`.
| `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.
| `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.
| `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.
| `dev` | `false` | If `true`, causes extra code to be added to components that will perform runtime checks and provide debugging information during development.
| `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.
| `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.
Expand Down
47 changes: 32 additions & 15 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ export default class Component {
this.stylesheet.warn_on_unused_selectors(this);
}

add_var(variable: Var) {
add_var(variable: Var, add_to_lookup = true) {
this.vars.push(variable);
this.var_lookup.set(variable.name, variable);

if (add_to_lookup) {
this.var_lookup.set(variable.name, variable);
}
}

add_reference(name: string) {
Expand Down Expand Up @@ -216,6 +219,10 @@ export default class Component {
variable.subscribable = true;
}
} else {
if (this.compile_options.varsReport === 'full') {
this.add_var({ name, referenced: true }, false);
}

this.used_names.add(name);
}
}
Expand Down Expand Up @@ -340,19 +347,7 @@ export default class Component {
css,
ast: this.original_ast,
warnings: this.warnings,
vars: this.vars
.filter(v => !v.global && !v.internal)
.map(v => ({
name: v.name,
export_name: v.export_name || null,
injected: v.injected || false,
module: v.module || false,
mutated: v.mutated || false,
reassigned: v.reassigned || false,
referenced: v.referenced || false,
writable: v.writable || false,
referenced_from_script: v.referenced_from_script || false
})),
vars: this.get_vars_report(),
stats: this.stats.render()
};
}
Expand Down Expand Up @@ -402,6 +397,28 @@ export default class Component {
};
}

get_vars_report(): Var[] {
const { compile_options, vars } = this;

const vars_report = compile_options.varsReport === false
? []
: compile_options.varsReport === 'full'
? vars
: vars.filter(v => !v.global && !v.internal);

return vars_report.map(v => ({
name: v.name,
export_name: v.export_name || null,
injected: v.injected || false,
module: v.module || false,
mutated: v.mutated || false,
reassigned: v.reassigned || false,
referenced: v.referenced || false,
writable: v.writable || false,
referenced_from_script: v.referenced_from_script || false
}));
}

error(
pos: {
start: number;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const valid_options = [
'filename',
'sourcemap',
'generate',
'varsReport',
'outputFilename',
'cssOutputFilename',
'sveltePath',
Expand Down
1 change: 1 addition & 0 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface CompileOptions {
name?: string;
filename?: string;
generate?: 'dom' | 'ssr' | false;
varsReport?: 'full' | 'strict' | false;

sourcemap?: object | string;
outputFilename?: string;
Expand Down
9 changes: 9 additions & 0 deletions test/vars/samples/vars-report-false/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
options: {
varsReport: false
},

test(assert, vars) {
assert.deepEqual(vars, []);
}
};
5 changes: 5 additions & 0 deletions test/vars/samples/vars-report-false/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let foo = "bar";
</script>

{foo}
19 changes: 19 additions & 0 deletions test/vars/samples/vars-report-full-noscript/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
options: {
varsReport: 'full'
},

test(assert, vars) {
assert.deepEqual(vars, [{
name: 'foo',
export_name: null,
injected: false,
module: false,
mutated: false,
reassigned: false,
referenced: true,
referenced_from_script: false,
writable: false
}]);
}
};
1 change: 1 addition & 0 deletions test/vars/samples/vars-report-full-noscript/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{foo}
31 changes: 31 additions & 0 deletions test/vars/samples/vars-report-full-script/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default {
options: {
varsReport: 'full'
},

test(assert, vars) {
assert.deepEqual(vars, [
{
name: 'foo',
export_name: 'foo',
injected: false,
module: false,
mutated: false,
reassigned: false,
referenced: true,
referenced_from_script: false,
writable: true
}, {
name: 'bar',
export_name: null,
injected: false,
module: false,
mutated: false,
reassigned: false,
referenced: true,
referenced_from_script: false,
writable: false
}
]);
}
};
5 changes: 5 additions & 0 deletions test/vars/samples/vars-report-full-script/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let foo = "bar";
</script>

{foo} {bar}
19 changes: 19 additions & 0 deletions test/vars/samples/vars-report-full/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
options: {
varsReport: 'full'
},

test(assert, vars) {
assert.deepEqual(vars, [{
name: 'foo',
export_name: null,
injected: false,
module: false,
mutated: false,
reassigned: false,
referenced: true,
referenced_from_script: false,
writable: false
}]);
}
};
3 changes: 3 additions & 0 deletions test/vars/samples/vars-report-full/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script></script>

{foo}