get rid of the class

pull/15820/head
Rich Harris 4 months ago
parent 7341f403af
commit 75680a9a5e

@ -266,7 +266,7 @@ export function analyze_module(ast, options) {
scopes, scopes,
analysis: /** @type {ComponentAnalysis} */ (analysis), analysis: /** @type {ComponentAnalysis} */ (analysis),
classes: new Map(), classes: new Map(),
class: null, state_fields: null,
// TODO the following are not needed for modules, but we have to pass them in order to avoid type error, // TODO the following are not needed for modules, but we have to pass them in order to avoid type error,
// and reducing the type would result in a lot of tedious type casts elsewhere - find a good solution one day // and reducing the type would result in a lot of tedious type casts elsewhere - find a good solution one day
ast_type: /** @type {any} */ (null), ast_type: /** @type {any} */ (null),
@ -626,7 +626,7 @@ export function analyze_component(root, source, options) {
component_slots: new Set(), component_slots: new Set(),
expression: null, expression: null,
classes: new Map(), classes: new Map(),
class: null, state_fields: null,
function_depth: scope.function_depth, function_depth: scope.function_depth,
reactive_statement: null reactive_statement: null
}; };
@ -694,7 +694,7 @@ export function analyze_component(root, source, options) {
component_slots: new Set(), component_slots: new Set(),
expression: null, expression: null,
classes: new Map(), classes: new Map(),
class: null, state_fields: null,
function_depth: scope.function_depth function_depth: scope.function_depth
}; };

@ -1,7 +1,6 @@
import type { Scope } from '../scope.js'; import type { Scope } from '../scope.js';
import type { ComponentAnalysis, ReactiveStatement } from '../types.js'; import type { ComponentAnalysis, ReactiveStatement } from '../types.js';
import type { AST, ExpressionMetadata, ValidatedCompileOptions } from '#compiler'; import type { AST, ExpressionMetadata, StateFields, ValidatedCompileOptions } from '#compiler';
import type { ClassAnalysis } from './visitors/shared/class-analysis.js';
import type { ClassBody } from 'estree'; import type { ClassBody } from 'estree';
export interface AnalysisState { export interface AnalysisState {
@ -22,8 +21,8 @@ export interface AnalysisState {
expression: ExpressionMetadata | null; expression: ExpressionMetadata | null;
/** Used to analyze class state. */ /** Used to analyze class state. */
classes: Map<ClassBody, ClassAnalysis>; classes: Map<ClassBody, StateFields>;
class: ClassAnalysis | null; state_fields: StateFields | null;
function_depth: number; function_depth: number;

@ -1,6 +1,6 @@
/** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, PrivateIdentifier, MethodDefinition } from 'estree' */ /** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, PrivateIdentifier, MethodDefinition } from 'estree' */
/** @import { StateFields } from '#compiler' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
/** @import { StateCreationRuneName } from '../../../../utils.js' */
import { get_rune } from '../../scope.js'; import { get_rune } from '../../scope.js';
import * as e from '../../../errors.js'; import * as e from '../../../errors.js';
import { is_state_creation_rune } from '../../../../utils.js'; import { is_state_creation_rune } from '../../../../utils.js';
@ -15,7 +15,8 @@ export function ClassBody(node, context) {
return; return;
} }
const analyzed = new ClassAnalysis(); /** @type {StateFields} */
const state_fields = {};
/** @type {string[]} */ /** @type {string[]} */
const seen = []; const seen = [];
@ -39,7 +40,7 @@ export function ClassBody(node, context) {
e.constructor_state_reassignment(node); // TODO the same thing applies to duplicate fields, so the code/message needs to change e.constructor_state_reassignment(node); // TODO the same thing applies to duplicate fields, so the code/message needs to change
} }
analyzed.fields[name] = { state_fields[name] = {
node, node,
type: rune type: rune
}; };
@ -81,7 +82,7 @@ export function ClassBody(node, context) {
context.next({ context.next({
...context.state, ...context.state,
class: analyzed state_fields
}); });
} }
@ -93,11 +94,3 @@ function get_name(node) {
return null; return null;
} }
/** @typedef { StateCreationRuneName | 'regular'} PropertyAssignmentType */
/** @typedef {{ type: PropertyAssignmentType; node: AssignmentExpression | PropertyDefinition; }} PropertyAssignmentDetails */
class ClassAnalysis {
/** @type {Record<string, PropertyAssignmentDetails>} */
fields = {};
}

@ -1,6 +1,7 @@
import type { AST, Binding } from '#compiler'; import type { AST, Binding } from '#compiler';
import type { Identifier, LabeledStatement, Node, Program } from 'estree'; import type { AssignmentExpression, Identifier, LabeledStatement, Node, Program } from 'estree';
import type { Scope, ScopeRoot } from './scope.js'; import type { Scope, ScopeRoot } from './scope.js';
import type { StateCreationRuneName } from '../../utils.js';
export interface Js { export interface Js {
ast: Program; ast: Program;

@ -2,6 +2,8 @@ import type { SourceMap } from 'magic-string';
import type { Binding } from '../phases/scope.js'; import type { Binding } from '../phases/scope.js';
import type { AST, Namespace } from './template.js'; import type { AST, Namespace } from './template.js';
import type { ICompileDiagnostic } from '../utils/compile_diagnostic.js'; import type { ICompileDiagnostic } from '../utils/compile_diagnostic.js';
import type { StateCreationRuneName } from '../../utils.js';
import type { AssignmentExpression, PropertyDefinition } from 'estree';
/** The return value of `compile` from `svelte/compiler` */ /** The return value of `compile` from `svelte/compiler` */
export interface CompileResult { export interface CompileResult {
@ -269,6 +271,13 @@ export interface ExpressionMetadata {
has_call: boolean; has_call: boolean;
} }
export interface StateFields {
[name: string]: {
type: StateCreationRuneName;
node: PropertyDefinition | AssignmentExpression;
};
}
export * from './template.js'; export * from './template.js';
export { Binding, Scope } from '../phases/scope.js'; export { Binding, Scope } from '../phases/scope.js';

Loading…
Cancel
Save