hatch-surf/node_modules/eslint-plugin-react/lib/rules/state-in-constructor.js
Riley Zhang df378d33ef
Some checks failed
CI / go (push) Waiting to run
CI / docker (push) Waiting to run
CI / lint-go (push) Waiting to run
CI / lint-docs (push) Waiting to run
CI / check-paperclip (push) Waiting to run
Deploy static site / Deploy to GitHub Pages (push) Has been cancelled
Deploy static site / Deploy via Docker to hatch.surf (push) Has been cancelled
Merge GitHub main into Gitea repo (allow unrelated histories)
Resolved conflicts by taking GitHub versions for:
- .dockerignore, .gitignore, Dockerfile, README.md, docker-compose.yml

Kept deploy.sh updated to:
- Pull from GitHub (primary source)
- Push to Gitea (push-mirror)
- Build from site/ directory (GitHub structure)

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-06-25 05:20:46 +02:00

69 lines
1.9 KiB
JavaScript

/**
* @fileoverview Enforce the state initialization style to be either in a constructor or with a class property
* @author Kanitkorn Sujautra
*/
'use strict';
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
stateInitConstructor: 'State initialization should be in a constructor',
stateInitClassProp: 'State initialization should be in a class property',
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Enforce class component state initialization style',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('state-in-constructor'),
},
messages,
schema: [{
enum: ['always', 'never'],
}],
},
create(context) {
const option = context.options[0] || 'always';
return {
'ClassProperty, PropertyDefinition'(node) {
if (
option === 'always'
&& !node.static
&& node.key.name === 'state'
&& componentUtil.getParentES6Component(context, node)
) {
report(context, messages.stateInitConstructor, 'stateInitConstructor', {
node,
});
}
},
AssignmentExpression(node) {
if (
option === 'never'
&& componentUtil.isStateMemberExpression(node.left)
&& astUtil.inConstructor(context, node)
&& componentUtil.getParentES6Component(context, node)
) {
report(context, messages.stateInitClassProp, 'stateInitClassProp', {
node,
});
}
},
};
},
};