Skip to content
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

[New]sort-comp: add static-variables option #2408

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/sort-comp.md
Expand Up @@ -90,7 +90,7 @@ The default configuration is:
}
}
```

* `static-variables` This group is not specified by default, but can be used to enforce class static variable positioning.
* `static-methods` is a special keyword that refers to static class methods.
* `lifecycle` refers to the `lifecycle` group defined in `groups`.
* `everything-else` is a special group that matches all of the methods that do not match any of the other groups.
Expand Down
14 changes: 12 additions & 2 deletions lib/rules/sort-comp.js
Expand Up @@ -148,8 +148,12 @@ module.exports = {
if (method.typeAnnotation) {
methodGroupIndexes.push(groupIndex);
}
} else if (currentGroup === 'static-variables') {
if (method.staticVariable) {
methodGroupIndexes.push(groupIndex);
}
} else if (currentGroup === 'static-methods') {
if (method.static) {
if (method.staticMethod) {
methodGroupIndexes.push(groupIndex);
}
} else if (currentGroup === 'instance-variables') {
Expand Down Expand Up @@ -380,7 +384,13 @@ module.exports = {
name: getPropertyName(node),
getter: node.kind === 'get',
setter: node.kind === 'set',
static: node.static,
staticVariable: node.static &&
node.type === 'ClassProperty' &&
(!node.value || !astUtil.isFunctionLikeExpression(node.value)),
staticMethod: node.static &&
(node.type === 'ClassProperty' || node.type === 'MethodDefinition') &&
node.value &&
(astUtil.isFunctionLikeExpression(node.value)),
instanceVariable: !node.static &&
node.type === 'ClassProperty' &&
(!node.value || !astUtil.isFunctionLikeExpression(node.value)),
Expand Down
184 changes: 184 additions & 0 deletions tests/lib/rules/sort-comp.js
Expand Up @@ -506,6 +506,129 @@ ruleTester.run('sort-comp', rule, {
'everything-else'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static foo;
static getDerivedStateFromProps() {}

render() {
return null;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-variables',
'static-methods'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static getDerivedStateFromProps() {}
static foo = 'some-str';

render() {
return null;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-methods',
'static-variables'
]
}]
}, {
code: `
class MyComponent extends React.Component {
foo = {};
static bar = 0;
static getDerivedStateFromProps() {}

render() {
return null;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'instance-variables',
'static-variables',
'static-methods'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static bar = 1;
foo = {};
static getDerivedStateFromProps() {}

render() {
return null;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-variables',
'instance-variables',
'static-methods'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static getDerivedStateFromProps() {}
render() {
return null;
}
static bar;
foo = {};
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-methods',
'render',
'static-variables',
'instance-variables'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static foo = 1;
bar;

constructor() {
super(props);

this.state = {};
}

render() {
return null;
}
}
`,
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-variables',
'instance-variables',
'constructor',
'everything-else',
'render'
]
}]
}],

invalid: [{
Expand Down Expand Up @@ -790,5 +913,66 @@ ruleTester.run('sort-comp', rule, {
'render'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static getDerivedStateFromProps() {}
static foo;

render() {
return null;
}
}
`,
errors: [{message: 'getDerivedStateFromProps should be placed after foo'}],
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'static-variables',
'static-methods'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static foo;
bar = 'some-str'
static getDerivedStateFromProps() {}

render() {
return null;
}
}
`,
errors: [{message: 'foo should be placed after bar'}],
parser: parsers.BABEL_ESLINT,
options: [{
order: [
'instance-variables',
'static-variables',
'static-methods'
]
}]
}, {
code: `
class MyComponent extends React.Component {
static getDerivedStateFromProps() {}
static bar;
render() {
return null;
}
foo = {};
}
`,
parser: parsers.BABEL_ESLINT,
errors: [{message: 'bar should be placed after render'}],
options: [{
order: [
'static-methods',
'render',
'static-variables',
'instance-variables'
]
}]
}]
});