From 71c373d0a59e2191432fb01f1977057e3cf8038f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 30 Jul 2024 18:21:54 -0400 Subject: [PATCH] chore: refactor analysis (#12651) * start moving visitors into separate modules * remove unused code * more * more * tidy up * more * more * more * more * more * more * more * more * more * more * more * alphabetize * more * fix * more * more * consolidate * more * more * more * more * more * more * more * tweak * more * more * more * more * more * more * more * more * more * more * jfc what are we doing here * more * bizarre * more * more * more * more * more * more * tidy * one down * dont merge * hmm * DRY * more * more * tidy up * tidy up * add changeset, as this should have its own release * tidy up * oh i should probably hit save --- .changeset/lucky-panthers-chew.md | 5 + .../src/compiler/phases/2-analyze/index.js | 1330 ++------------- .../src/compiler/phases/2-analyze/types.d.ts | 15 +- .../compiler/phases/2-analyze/validation.js | 1443 ----------------- .../visitors/ArrowFunctionExpression.js | 11 + .../visitors/AssignmentExpression.js | 28 + .../phases/2-analyze/visitors/Attribute.js | 213 +++ .../phases/2-analyze/visitors/AwaitBlock.js | 42 + .../2-analyze/visitors/BindDirective.js | 234 +++ .../2-analyze/visitors/CallExpression.js | 209 +++ .../phases/2-analyze/visitors/ClassBody.js | 27 + .../2-analyze/visitors/ClassDeclaration.js | 20 + .../2-analyze/visitors/ClassDirective.js | 10 + .../phases/2-analyze/visitors/Component.js | 20 + .../phases/2-analyze/visitors/ConstTag.js | 34 + .../phases/2-analyze/visitors/DebugTag.js | 15 + .../phases/2-analyze/visitors/EachBlock.js | 29 + .../visitors/ExportDefaultDeclaration.js | 15 + .../visitors/ExportNamedDeclaration.js | 92 ++ .../2-analyze/visitors/ExportSpecifier.js | 66 + .../2-analyze/visitors/ExpressionStatement.js | 38 + .../2-analyze/visitors/ExpressionTag.js | 18 + .../2-analyze/visitors/FunctionDeclaration.js | 11 + .../2-analyze/visitors/FunctionExpression.js | 11 + .../phases/2-analyze/visitors/HtmlTag.js | 15 + .../phases/2-analyze/visitors/Identifier.js | 154 ++ .../phases/2-analyze/visitors/IfBlock.js | 26 + .../2-analyze/visitors/ImportDeclaration.js | 30 + .../phases/2-analyze/visitors/KeyBlock.js | 17 + .../2-analyze/visitors/LabeledStatement.js | 104 ++ .../phases/2-analyze/visitors/LetDirective.js | 24 + .../2-analyze/visitors/MemberExpression.js | 27 + .../2-analyze/visitors/NewExpression.js | 15 + .../phases/2-analyze/visitors/OnDirective.js | 25 + .../2-analyze/visitors/RegularElement.js | 164 ++ .../phases/2-analyze/visitors/RenderTag.js | 37 + .../phases/2-analyze/visitors/SlotElement.js | 39 + .../phases/2-analyze/visitors/SnippetBlock.js | 55 + .../2-analyze/visitors/SpreadAttribute.js | 10 + .../2-analyze/visitors/StyleDirective.js | 38 + .../2-analyze/visitors/SvelteComponent.js | 11 + .../2-analyze/visitors/SvelteElement.js | 62 + .../2-analyze/visitors/SvelteFragment.js | 27 + .../phases/2-analyze/visitors/SvelteHead.js | 15 + .../phases/2-analyze/visitors/SvelteSelf.js | 11 + .../phases/2-analyze/visitors/Text.js | 17 + .../phases/2-analyze/visitors/TitleElement.js | 21 + .../2-analyze/visitors/UpdateExpression.js | 25 + .../2-analyze/visitors/VariableDeclarator.js | 115 ++ .../2-analyze/{ => visitors/shared}/a11y.js | 28 +- .../2-analyze/visitors/shared/attribute.js | 123 ++ .../2-analyze/visitors/shared/component.js | 68 + .../2-analyze/visitors/shared/element.js | 149 ++ .../2-analyze/visitors/shared/function.js | 21 + .../phases/2-analyze/visitors/shared/utils.js | 167 ++ .../svelte/src/compiler/phases/types.d.ts | 2 +- .../svelte/src/compiler/phases/visitors.js | 1 + packages/svelte/src/compiler/utils/ast.js | 1 + .../_config.js | 0 .../main.svelte | 0 .../_config.js | 9 + .../main.svelte.js | 0 62 files changed, 2962 insertions(+), 2627 deletions(-) create mode 100644 .changeset/lucky-panthers-chew.md delete mode 100644 packages/svelte/src/compiler/phases/2-analyze/validation.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ArrowFunctionExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/AssignmentExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/AwaitBlock.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/BindDirective.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDirective.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/Component.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ConstTag.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/DebugTag.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ExportDefaultDeclaration.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ExpressionStatement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ExpressionTag.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/FunctionDeclaration.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/FunctionExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/HtmlTag.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/IfBlock.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/ImportDeclaration.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/KeyBlock.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/LabeledStatement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/LetDirective.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/MemberExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/NewExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/OnDirective.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/RenderTag.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SlotElement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SnippetBlock.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SpreadAttribute.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/StyleDirective.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteComponent.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteElement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteFragment.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteHead.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteSelf.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/Text.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/TitleElement.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/UpdateExpression.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/VariableDeclarator.js rename packages/svelte/src/compiler/phases/2-analyze/{ => visitors/shared}/a11y.js (98%) create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/shared/attribute.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/shared/element.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/shared/function.js create mode 100644 packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js rename packages/svelte/tests/compiler-errors/samples/{runes-wrong-props-placement => runes-wrong-props-placement-instance}/_config.js (100%) rename packages/svelte/tests/compiler-errors/samples/{runes-wrong-props-placement => runes-wrong-props-placement-instance}/main.svelte (100%) create mode 100644 packages/svelte/tests/compiler-errors/samples/runes-wrong-props-placement-module/_config.js rename packages/svelte/tests/compiler-errors/samples/{runes-wrong-props-placement => runes-wrong-props-placement-module}/main.svelte.js (100%) diff --git a/.changeset/lucky-panthers-chew.md b/.changeset/lucky-panthers-chew.md new file mode 100644 index 0000000000..9bd08df3e6 --- /dev/null +++ b/.changeset/lucky-panthers-chew.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +chore: internal compiler refactoring diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index d797703e92..2a4241a338 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -1,44 +1,171 @@ -/** @import { ArrowFunctionExpression, CallExpression, Expression, FunctionDeclaration, FunctionExpression, Identifier, LabeledStatement, Literal, Node, Program, Super } from 'estree' */ -/** @import { Attribute, BindDirective, Binding, DelegatedEvent, RegularElement, Root, Script, SvelteNode, Text, ValidatedCompileOptions, ValidatedModuleCompileOptions } from '#compiler' */ -/** @import { AnalysisState, Context, LegacyAnalysisState, Visitors } from './types' */ +/** @import { Node, Program } from 'estree' */ +/** @import { Root, Script, SvelteNode, ValidatedCompileOptions, ValidatedModuleCompileOptions } from '#compiler' */ +/** @import { AnalysisState, Visitors } from './types' */ /** @import { Analysis, ComponentAnalysis, Js, ReactiveStatement, Template } from '../types' */ -import is_reference from 'is-reference'; import { walk } from 'zimmerframe'; import * as e from '../../errors.js'; import * as w from '../../warnings.js'; -import { - extract_identifiers, - extract_all_identifiers_from_expression, - extract_paths, - is_event_attribute, - is_text_attribute, - object, - unwrap_optional, - get_attribute_expression, - get_attribute_chunks -} from '../../utils/ast.js'; +import { is_text_attribute } from '../../utils/ast.js'; import * as b from '../../utils/builders.js'; -import { MathMLElements, ReservedKeywords, Runes, SVGElements } from '../constants.js'; -import { Scope, ScopeRoot, create_scopes, get_rune, set_scope } from '../scope.js'; -import { merge } from '../visitors.js'; -import { validation_legacy, validation_runes, validation_runes_js } from './validation.js'; +import { ReservedKeywords, Runes } from '../constants.js'; +import { Scope, ScopeRoot, create_scopes, get_rune } from '../scope.js'; import check_graph_for_cycles from './utils/check_graph_for_cycles.js'; -import { regex_starts_with_newline } from '../patterns.js'; -import { create_attribute, is_element_node } from '../nodes.js'; -import { - DelegatedEvents, - is_capture_event, - namespace_mathml, - namespace_svg -} from '../../../constants.js'; -import { should_proxy_or_freeze } from '../3-transform/client/utils.js'; +import { create_attribute } from '../nodes.js'; import { analyze_css } from './css/css-analyze.js'; import { prune } from './css/css-prune.js'; import { hash } from '../../../utils.js'; import { warn_unused } from './css/css-warn.js'; import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js'; import { ignore_map, ignore_stack, pop_ignore, push_ignore } from '../../state.js'; -import { equal } from '../../utils/assert.js'; +import { ArrowFunctionExpression } from './visitors/ArrowFunctionExpression.js'; +import { AssignmentExpression } from './visitors/AssignmentExpression.js'; +import { Attribute } from './visitors/Attribute.js'; +import { AwaitBlock } from './visitors/AwaitBlock.js'; +import { BindDirective } from './visitors/BindDirective.js'; +import { CallExpression } from './visitors/CallExpression.js'; +import { ClassBody } from './visitors/ClassBody.js'; +import { ClassDeclaration } from './visitors/ClassDeclaration.js'; +import { ClassDirective } from './visitors/ClassDirective.js'; +import { Component } from './visitors/Component.js'; +import { ConstTag } from './visitors/ConstTag.js'; +import { DebugTag } from './visitors/DebugTag.js'; +import { EachBlock } from './visitors/EachBlock.js'; +import { ExportDefaultDeclaration } from './visitors/ExportDefaultDeclaration.js'; +import { ExportNamedDeclaration } from './visitors/ExportNamedDeclaration.js'; +import { ExportSpecifier } from './visitors/ExportSpecifier.js'; +import { ExpressionStatement } from './visitors/ExpressionStatement.js'; +import { ExpressionTag } from './visitors/ExpressionTag.js'; +import { FunctionDeclaration } from './visitors/FunctionDeclaration.js'; +import { FunctionExpression } from './visitors/FunctionExpression.js'; +import { HtmlTag } from './visitors/HtmlTag.js'; +import { Identifier } from './visitors/Identifier.js'; +import { IfBlock } from './visitors/IfBlock.js'; +import { ImportDeclaration } from './visitors/ImportDeclaration.js'; +import { KeyBlock } from './visitors/KeyBlock.js'; +import { LabeledStatement } from './visitors/LabeledStatement.js'; +import { LetDirective } from './visitors/LetDirective.js'; +import { MemberExpression } from './visitors/MemberExpression.js'; +import { NewExpression } from './visitors/NewExpression.js'; +import { OnDirective } from './visitors/OnDirective.js'; +import { RegularElement } from './visitors/RegularElement.js'; +import { RenderTag } from './visitors/RenderTag.js'; +import { SlotElement } from './visitors/SlotElement.js'; +import { SnippetBlock } from './visitors/SnippetBlock.js'; +import { SpreadAttribute } from './visitors/SpreadAttribute.js'; +import { StyleDirective } from './visitors/StyleDirective.js'; +import { SvelteComponent } from './visitors/SvelteComponent.js'; +import { SvelteElement } from './visitors/SvelteElement.js'; +import { SvelteFragment } from './visitors/SvelteFragment.js'; +import { SvelteHead } from './visitors/SvelteHead.js'; +import { SvelteSelf } from './visitors/SvelteSelf.js'; +import { Text } from './visitors/Text.js'; +import { TitleElement } from './visitors/TitleElement.js'; +import { UpdateExpression } from './visitors/UpdateExpression.js'; +import { VariableDeclarator } from './visitors/VariableDeclarator.js'; + +/** + * @type {Visitors} + */ +const visitors = { + _(node, { state, next, path }) { + const parent = path.at(-1); + + /** @type {string[]} */ + const ignores = []; + + if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') { + const idx = parent.nodes.indexOf(/** @type {any} */ (node)); + + for (let i = idx - 1; i >= 0; i--) { + const prev = parent.nodes[i]; + + if (prev.type === 'Comment') { + ignores.push( + ...extract_svelte_ignore( + prev.start + 4 /* '