Repository: sveltejs/svelte
File: packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js (JavaScript)

Code before:

	context.state.analysis.classes.set(node, state_fields);

	/** @type {MethodDefinition | null} */
	let constructor = null;

	/**
	 * @param {PropertyDefinition | AssignmentExpression} node
	 * @param {Expression | PrivateIdentifier} key
	 * @param {Expression | null | undefined} value
	 */
	function handle(node, key, value) {
		const name = get_name(key);
		if (name === null) return;

		const rune = get_rune(value, context.state.scope);

		if (rune && is_state_creation_rune(rune)) {
			if (state_fields.has(name)) {
				e.state_field_duplicate(node, name);
			}

			const _key = (key.type === 'PrivateIdentifier' ? '#' : '') + name;
			const field = fields.get(_key);

			if (field && (field.length > 1 || (field.length === 1 && field[0] !== 'prop'))) {
				e.duplicate_class_field(node, _key);
			}

			state_fields.set(name, {
				node,
				type: rune,
				// @ts-expect-error for public state this is filled out in a moment
				key: key.type === 'PrivateIdentifier' ? key : null,
				value: /** @type {CallExpression} */ (value)
			});
		}
	}

	for (const child of node.body) {
		if (child.type === 'PropertyDefinition' && !child.computed && !child.static) {
			handle(child, child.key, child.value);
			const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key);
			const field = fields.get(key);
			if (!field) {
				fields.set(key, ['prop']);
				continue;
			}
			e.duplicate_class_field(child, key);
		}


Review comment (refactor):
What do you think about
```diff
-if (field && (field.length > 1 || (field.length === 1 && field[0] !== 'prop'))) {
+if (field) {
```
with adjusting a corresponding test so in the following case, as for me, the error will be more meaningful. Though both errors are somewhat puzzling here. 
```js
export class Counter {
	count = -1; // before: state_field_invalid_assignment here

	constructor() {
		this.count = $state(0); // after: duplicate_class_field here
	}
}
```

Code after:

	context.state.analysis.classes.set(node, state_fields);

	/** @type {MethodDefinition | null} */
	let constructor = null;

	/**
	 * @param {PropertyDefinition | AssignmentExpression} node
	 * @param {Expression | PrivateIdentifier} key
	 * @param {Expression | null | undefined} value
	 */
	function handle(node, key, value) {
		const name = get_name(key);
		if (name === null) return;

		const rune = get_rune(value, context.state.scope);

		if (rune && is_state_creation_rune(rune)) {
			if (state_fields.has(name)) {
				e.state_field_duplicate(node, name);
			}

			const _key = (key.type === 'PrivateIdentifier' ? '#' : '') + name;
			const field = fields.get(_key);

			// if there's already a method or assigned field, error
			if (field && !(field.length === 1 && field[0] === 'prop')) {
				e.duplicate_class_field(node, _key);
			}

			state_fields.set(name, {
				node,
				type: rune,
				// @ts-expect-error for public state this is filled out in a moment
				key: key.type === 'PrivateIdentifier' ? key : null,
				value: /** @type {CallExpression} */ (value)
			});
		}
	}

	for (const child of node.body) {
		if (child.type === 'PropertyDefinition' && !child.computed && !child.static) {
			handle(child, child.key, child.value);
			const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key);
			const field = fields.get(key);
			if (!field) {
				fields.set(key, [child.value ? 'assigned_prop' : 'prop']);
				continue;
			}
			e.duplicate_class_field(child, key);
		}