Add lint/noSetterReturn rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -44,6 +44,7 @@ export type DiagnosticCategory =
| 'lint/noImportAssign'
| 'lint/noLabelVar'
| 'lint/noReferenceToNonExistingGroup'
+ | 'lint/noSetterReturn'
| 'lint/noShadowRestrictedNames'
| 'lint/noShorthandArrayType'
| 'lint/noTemplateCurlyInString'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -411,6 +411,11 @@ export const descriptions = createMessages({
message: `Expected a 'return' at end of a getter method but got ${got}`,
}),
+ NO_SETTER_RETURN: {
+ category: 'lint/noSetterReturn',
+ message: `Setter cannot return a value`,
+ },
+
EMPTY_BLOCKS: {
category: 'lint/emptyBlocks',
message: 'Empty block',

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -26,19 +26,20 @@ import noFunctionAssign from './noFunctionAssign';
import noImportAssign from './noImportAssign';
import noLabelVar from './noLabelVar';
import noMultipleSpacesInRegularExpressionLiterals from './noMultipleSpacesInRegularExpressionLiterals';
+import noReferenceToNonExistingGroup from './noReferenceToNonExistingGroup';
+import noSetterReturn from './noSetterReturn';
import noShadowRestrictedNames from './noShadowRestrictedNames';
+import noShorthandArrayType from './noShorthandArrayType';
import noTemplateCurlyInString from './noTemplateCurlyInString';
import noUnsafeFinally from './noUnsafeFinally';
import noVar from './noVar';
import preferFunctionDeclarations from './preferFunctionDeclarations';
import preferTemplate from './preferTemplate';
+import singleVarDeclarator from './singleVarDeclarator';
import sparseArray from './sparseArray';
import undeclaredVariables from './undeclaredVariables';
import unsafeNegation from './unsafeNegation';
import unusedVariables from './unusedVariables';
-import singleVarDeclarator from './singleVarDeclarator';
-import noReferenceToNonExistingGroup from './noReferenceToNonExistingGroup';
-import noShorthandArrayType from './noShorthandArrayType';
// Add transforms in alphabetical order.
export const lintTransforms = [
@@ -61,8 +62,9 @@ export const lintTransforms = [
noFunctionAssign,
noImportAssign,
noLabelVar,
- noReferenceToNonExistingGroup,
noMultipleSpacesInRegularExpressionLiterals,
+ noReferenceToNonExistingGroup,
+ noSetterReturn,
noShadowRestrictedNames,
noShorthandArrayType,
noTemplateCurlyInString,
@@ -70,10 +72,9 @@ export const lintTransforms = [
noVar,
preferFunctionDeclarations,
preferTemplate,
+ singleVarDeclarator,
sparseArray,
undeclaredVariables,
unsafeNegation,
unusedVariables,
- noDuplicateGroupNamesInRegularExpressions,
- singleVarDeclarator,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noSetterReturn.test.md
+# `noSetterReturn.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/transforms/lint/noSetterReturn.test.ts --update-snapshots` to update.
+
+## `no setter return`
+
+### `0`
+
+```javascript
+Object {
+ src: '\n class p {\n set name(value) {\n if (!value) {\n return \'wrong\';\n }\n }\n }\n console.log(new p());\n '
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: 'lint'}]
+ description: Object {
+ category: 'lint/noSetterReturn'
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: 'Setter cannot return a value'}
+ }
+ location: Object {
+ filename: 'unknown'
+ language: 'js'
+ mtime: undefined
+ sourceType: 'module'
+ end: Object {
+ column: 25
+ index: 86
+ line: 5
+ }
+ start: Object {
+ column: 10
+ index: 71
+ line: 5
+ }
+ }
+ }
+ ]
+}
+```
+
+### `1`
+
+```javascript
+Object {
+ src: '\n class p {\n static set name(value) {\n if (!value) {\n return \'wrong\';\n }\n }\n }\n console.log(p);\n '
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: 'lint'}]
+ description: Object {
+ category: 'lint/noSetterReturn'
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: 'Setter cannot return a value'}
+ }
+ location: Object {
+ filename: 'unknown'
+ language: 'js'
+ mtime: undefined
+ sourceType: 'module'
+ end: Object {
+ column: 25
+ index: 93
+ line: 5
+ }
+ start: Object {
+ column: 10
+ index: 78
+ line: 5
+ }
+ }
+ }
+ ]
+}
+```
+
+### `2`
+
+```javascript
+Object {
+ src: '\n let p = {\n set name(value) {\n if (!value) {\n return \'wrong\';\n }\n }\n };\n console.log(p);\n '
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: 'lint'}]
+ description: Object {
+ category: 'lint/noSetterReturn'
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: 'Setter cannot return a value'}
+ }
+ location: Object {
+ filename: 'unknown'
+ language: 'js'
+ mtime: undefined
+ sourceType: 'module'
+ end: Object {
+ column: 25
+ index: 86
+ line: 5
+ }
+ start: Object {
+ column: 10
+ index: 71
+ line: 5
+ }
+ }
+ }
+ ]
+}
+```
+
+### `3`
+
+```javascript
+Object {
+ diagnostics: Array []
+ src: '\n class p {\n set name(value) {\n if (!value) {\n return;\n }\n }\n };\n console.log(p);\n '
+ suppressions: Array []
+}
+```

ADD
packages/@romejs/js-compiler/transforms/lint/noSetterReturn.test.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import test from '@romejs/test';
+import {testLint} from '../../api/lint.test';
+
+test('no setter return', async (t) => {
+ const badClass = await testLint(`
+ class p {
+ set name(value) {
+ if (!value) {
+ return 'wrong';
+ }
+ }
+ }
+ console.log(new p());
+ `);
+
+ t.snapshot(badClass);
+
+ const badStaticClass = await testLint(`
+ class p {
+ static set name(value) {
+ if (!value) {
+ return 'wrong';
+ }
+ }
+ }
+ console.log(p);
+ `);
+
+ t.snapshot(badStaticClass);
+
+ const badObject = await testLint(`
+ let p = {
+ set name(value) {
+ if (!value) {
+ return 'wrong';
+ }
+ }
+ };
+ console.log(p);
+ `);
+
+ t.snapshot(badObject);
+
+ const blankReturn = await testLint(`
+ class p {
+ set name(value) {
+ if (!value) {
+ return;
+ }
+ }
+ };
+ console.log(p);
+ `);
+
+ t.snapshot(blankReturn);
+});

ADD
packages/@romejs/js-compiler/transforms/lint/noSetterReturn.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {Path} from '@romejs/js-compiler';
+import {AnyNode} from '@romejs/js-ast';
+import {getCompletionRecords} from '@romejs/js-ast-utils';
+import {descriptions} from '@romejs/diagnostics';
+
+export default {
+ name: 'noSetterReturn',
+ enter(path: Path): AnyNode {
+ const {node} = path;
+
+ if ((node.type === 'ClassMethod' || node.type === 'ObjectMethod') &&
+ node.kind === 'set') {
+ for (const record of getCompletionRecords(node.body)) {
+ if (record.type === 'COMPLETION' && record.node.type ===
+ 'ReturnStatement' && record.node.argument !== undefined) {
+ path.context.addNodeDiagnostic(
+ record.node,
+ descriptions.LINT.NO_SETTER_RETURN,
+ );
+ }
+ }
+ }
+
+ return node;
+ },
+};

Add lint/noSetterReturn rule