Skip to content

Tidy up analysis #9435

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

Closed
wants to merge 19 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/eighty-bikes-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: added missing context of `svelte.js` for validation
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"program": "start.js",
"cwd": "${workspaceFolder}/playgrounds/demo",
"cascadeTerminateToConfigurations": ["Playground: Browser"]
},
{
"type": "node",
"request": "launch",
"name": "Run sandbox",
"program": "${workspaceFolder}/playgrounds/sandbox/run.js"
}
],
"compounds": [
Expand Down
178 changes: 89 additions & 89 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,93 @@ import { getLocator } from 'locate-character';
import { walk } from 'zimmerframe';
import { validate_component_options, validate_module_options } from './validate-options.js';
import { convert } from './legacy.js';
import { transform_warnings } from './utils/warnings.js';
export { default as preprocess } from './preprocess/index.js';

/**
* @param {string} source
* @param {string | undefined} filename
* @param {Function} fn
*/
function handle_error(source, filename, fn) {
try {
return fn();
} catch (e) {
if (/** @type {any} */ (e).name === 'CompileError') {
const error = /** @type {import('#compiler').CompileError} */ (e);

error.filename = filename;

if (error.position) {
// TODO this is reused with warnings — DRY out
const locator = getLocator(source, { offsetLine: 1 });
const start = locator(error.position[0]);
const end = locator(error.position[1]);

error.start = start;
error.end = end;
}
}

throw e;
}
}

/**
* The parse function parses a component, returning only its abstract syntax tree.
*
* The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
* `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
*
* https://svelte.dev/docs/svelte-compiler#svelte-parse
* @param {string} source
* @param {{ filename?: string; modern?: boolean }} [options]
* @returns {import('#compiler').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode}
*/
export function parse(source, options = {}) {
return handle_error(source, undefined, () => {
/** @type {import('#compiler').Root} */
const ast = _parse(source);

if (options.modern) {
// remove things that we don't want to treat as public API
return walk(/** @type {import('#compiler').SvelteNode} */ (ast), null, {
_(node, { next }) {
// @ts-ignore
delete node.parent;
// @ts-ignore
delete node.metadata;
next();
}
});
}

return convert(source, ast);
});
}

/**
* @param {string} source
* @param {TODO} options
*/
export function analyze(source, options = {}) {
return handle_error(source, options.filename, () => {
const validated = validate_component_options(options, '');
const parsed = _parse(source);

const combined_options = /** @type {import('#compiler').ValidatedCompileOptions} */ ({
...validated,
...parsed.options
});

const analysis = analyze_component(parsed, combined_options);

return {
warnings: transform_warnings(source, options.filename, analysis.warnings)
};
});
}

/**
* `compile` converts your `.svelte` source code into a JavaScript module that exports a component
*
Expand All @@ -17,7 +102,7 @@ export { default as preprocess } from './preprocess/index.js';
* @returns {import('#compiler').CompileResult}
*/
export function compile(source, options) {
try {
return handle_error(source, options.filename, () => {
const validated = validate_component_options(options, '');
const parsed = _parse(source);

Expand All @@ -29,17 +114,7 @@ export function compile(source, options) {
const analysis = analyze_component(parsed, combined_options);
const result = transform_component(analysis, source, combined_options);
return result;
} catch (e) {
if (/** @type {any} */ (e).name === 'CompileError') {
handle_compile_error(
/** @type {import('#compiler').CompileError} */ (e),
options.filename,
source
);
}

throw e;
}
});
}

/**
Expand All @@ -51,86 +126,11 @@ export function compile(source, options) {
* @returns {import('#compiler').CompileResult}
*/
export function compileModule(source, options) {
try {
return handle_error(source, options.filename, () => {
const validated = validate_module_options(options, '');
const analysis = analyze_module(parse_acorn(source), validated);
return transform_module(analysis, source, validated);
} catch (e) {
if (/** @type {any} */ (e).name === 'CompileError') {
handle_compile_error(
/** @type {import('#compiler').CompileError} */ (e),
options.filename,
source
);
}

throw e;
}
}

/**
* @param {import('#compiler').CompileError} error
* @param {string | undefined} filename
* @param {string} source
*/
function handle_compile_error(error, filename, source) {
error.filename = filename;

if (error.position) {
// TODO this is reused with warnings — DRY out
const locator = getLocator(source, { offsetLine: 1 });
const start = locator(error.position[0]);
const end = locator(error.position[1]);

error.start = start;
error.end = end;
}

throw error;
}

/**
* The parse function parses a component, returning only its abstract syntax tree.
*
* The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
* `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
*
* https://svelte.dev/docs/svelte-compiler#svelte-parse
* @param {string} source
* @param {{ filename?: string; modern?: boolean }} [options]
* @returns {import('#compiler').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode}
*/
export function parse(source, options = {}) {
/** @type {import('#compiler').Root} */
let ast;
try {
ast = _parse(source);
} catch (e) {
if (/** @type {any} */ (e).name === 'CompileError') {
handle_compile_error(
/** @type {import('#compiler').CompileError} */ (e),
options.filename,
source
);
}

throw e;
}

if (options.modern) {
// remove things that we don't want to treat as public API
return walk(/** @type {import('#compiler').SvelteNode} */ (ast), null, {
_(node, { next }) {
// @ts-ignore
delete node.parent;
// @ts-ignore
delete node.metadata;
next();
}
});
}

return convert(source, ast);
});
}

/**
Expand Down
Loading