fix linting issues on changes related to noExAssign
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -50,6 +50,7 @@ export type DiagnosticCategory =
| 'lint/noShadowRestrictedNames'
| 'lint/noTemplateCurlyInString'
| 'lint/noUnsafeFinally'
+ | 'lint/noExAssign'
| 'lint/preferFunctionDeclarations'
| 'lint/preferTemplate'
| 'lint/sparseArray'

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -29,6 +29,7 @@ import noLabelVar from './noLabelVar';
import noShadowRestrictedNames from './noShadowRestrictedNames';
import noTemplateCurlyInString from './noTemplateCurlyInString';
import noUnsafeFinally from './noUnsafeFinally';
+import noExAssign from './noExAssign';
import preferFunctionDeclarations from './preferFunctionDeclarations';
import preferTemplate from './preferTemplate';
import sparseArray from './sparseArray';
@@ -62,6 +63,7 @@ export const lintTransforms = [
noShadowRestrictedNames,
noTemplateCurlyInString,
noUnsafeFinally,
+ noExAssign,
preferFunctionDeclarations,
preferTemplate,
sparseArray,

ADD
packages/@romejs/js-compiler/transforms/lint/noExAssign.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 exception assign', async (t) => {
+ const validTestCases = [
+ `try { } catch (e) { three = 2 + 1; }`,
+ 'try { } catch ({e}) { this.something = 2; }',
+ 'function foo() { try { } catch (e) { return false; } }',
+ ];
+
+ const invalidTestCases = [
+ 'try { } catch (e) { e; e = 10; }',
+ 'try { } catch (ex) { console.log(\'test\'); ex = 10; }',
+ 'try { } catch (ex) { [ex, test] = []; }',
+ 'try { } catch ({message, name}) { message = \'test\'; name = 10; }',
+ // "try { } catch (ex) { ({x: ex = 0} = {}); }",
+
+ // "try { } catch (ex) { let a; ({x: a = ex = 0} = {}); }"
+ ];
+
+ for (const validTestCase of validTestCases) {
+ const {diagnostics} = await testLint(validTestCase);
+
+ t.falsy(diagnostics.find((d) => d.category === 'lint/noExAssign'));
+ }
+
+ for (const invalidTestCase of invalidTestCases) {
+ let {diagnostics} = await testLint(invalidTestCase);
+
+ t.truthy(diagnostics.find((d) => d.category === 'lint/noExAssign'));
+ }
+});

ADD
packages/@romejs/js-compiler/transforms/lint/noExAssign.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 {getBindingIdentifiers, isVariableIdentifier} from '@romejs/js-ast-utils';
+import {Path} from '@romejs/js-compiler';
+import {AnyNode} from '@romejs/js-ast';
+import {Context} from '@romejs/js-compiler';
+function addDiagnostic(name: string, node: AnyNode, context: Context) {
+ const identifiers = getBindingIdentifiers(node).map((identifier) =>
+ identifier.name
+ );
+
+ if (!identifiers.includes(name)) {
+ return;
+ }
+
+ context.addNodeDiagnostic(node, {
+ category: 'lint/noExAssign',
+ message: 'Do not assign to the exception parameter.',
+ });
+}
+
+export default {
+ name: 'noExAssign',
+ enter(path: Path): AnyNode {
+ const {node, context} = path;
+
+ if (node.type === 'CatchClause' && node.body.type === 'BlockStatement') {
+ node.body.body.forEach((item) => {
+ if (item.type === 'ExpressionStatement' && item.expression.type ===
+ 'AssignmentExpression') {
+ if (isVariableIdentifier(item.expression.left)) {
+ const {name} = item.expression.left;
+
+ addDiagnostic(name, node, context);
+ }
+
+ if (item.expression.left.type === 'AssignmentArrayPattern') {
+ const {elements} = item.expression.left;
+
+ elements.forEach((element) => {
+ if (!element || element && !isVariableIdentifier(element)) {
+ return;
+ }
+
+ const {name} = element;
+
+ addDiagnostic(name, node, context);
+ });
+ }
+ }
+ });
+ }
+
+ return node;
+ },
+};

fix linting issues on changes related to noExAssign