Add noImportAssign
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -251,3 +251,40 @@ test('disallow var', async t => {
},
]);
});
+
+test('no import assign', async t => {
+ let failingCases = [
+ 'import x from "y";\nx=1;',
+ 'import x from "y";\n[x]=1;',
+ 'import x from "y";\n({x}=1);',
+ 'import x from "y";\nx++',
+ 'import x from "y";\n[...x]=1;',
+ 'import x from "y";\n({...x}=1);',
+ 'import x from "y";\nfor (x in y);',
+ 'import x from "y";\nx+=1',
+ 'import * as x from "y";\nx=1;',
+ 'import {x} from "y";\nx=1;',
+ ];
+ for (let failingCase of failingCases) {
+ const res = await testLint(
+ failingCase,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+ if (!res.diagnostics.some(d => d.category === 'lint/noImportAssign')) {
+ t.fail(
+ `expected "\n${failingCase}\n" to report a lint/noImportAssign diagnostic but it didn't`,
+ [
+ {
+ type: 'inspect',
+ data: parseJS({
+ input: failingCase,
+ sourceType: 'module',
+ path: createUnknownFilePath('unknown'),
+ }),
+ },
+ {type: 'inspect', data: res.diagnostics},
+ ],
+ );
+ }
+ }
+});

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -15,6 +15,7 @@ import noAsyncPromiseExecutor from './noAsyncPromiseExecutor';
import noLabelVar from './noLabelVar';
import noDuplicateKeys from './noDuplicateKeys';
import disallowVar from './disallowVar';
+import noImportAssign from './noImportAssign';
export const lintTransforms = [
undeclaredVariables,
@@ -27,4 +28,5 @@ export const lintTransforms = [
noLabelVar,
noDuplicateKeys,
disallowVar,
+ noImportAssign,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noImportAssign.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 {markup} from '@romejs/string-markup';
+
+function isAssignment(path: Path) {
+ switch (path.parentPath.node.type) {
+ case 'AssignmentExpression':
+ case 'AssignmentArrayPattern':
+ case 'AssignmentObjectPatternProperty':
+ case 'UpdateExpression':
+ case 'AssignmentObjectPattern':
+ case 'ForInStatement':
+ return true;
+ }
+}
+
+export default {
+ name: 'noImportAssign',
+ enter(path: Path): AnyNode {
+ const {node, scope} = path;
+
+ if (
+ ((node.type === 'AssignmentIdentifier' && isAssignment(path)) ||
+ (node.type === 'ReferenceIdentifier' &&
+ path.parentPath.node.type === 'UpdateExpression')) &&
+ scope.getBindingAssert(node.name).kind === 'import'
+ ) {
+ path.context.addNodeDiagnostic(node, {
+ category: 'lint/noImportAssign',
+ message: markup`<emphasis>${node.name}</emphasis> is read-only`,
+ });
+ }
+
+ return node;
+ },
+};

Add noImportAssign