Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Add linter rule: no function reassignment #130

Merged
merged 6 commits into from
Mar 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/@romejs/js-compiler/__rtests__/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import lint from '../api/lint';
import {parseJS} from '@romejs/js-parser';
import {createUnknownFilePath} from '@romejs/path';
import {DEFAULT_PROJECT_CONFIG, ProjectConfig} from '@romejs/project';
import {PartialDiagnostic} from '@romejs/diagnostics/types';
import {ConstSourceType} from '@romejs/js-ast';

const LINT_ENABLED_FORMAT_DISABLED_CONFIG: ProjectConfig = {
Expand Down Expand Up @@ -315,6 +316,48 @@ test('no duplicate keys', async t => {
]);
});

test('no function reassignment', async t => {
function checkCategory(diagnostic: PartialDiagnostic): Boolean {
return diagnostic.category === 'lint/noFunctionAssign';
}

const validTestCases = [
'function foo() { var foo = bar; }',
'function foo(foo) { foo = bar; }',
'function foo() { var foo; foo = bar; }',
'var foo = () => {}; foo = bar;',
'var foo = function() {}; foo = bar;',
'var foo = function() { foo = bar; };',
`import bar from 'bar'; function foo() { var foo = bar; }`,
];

const invalidTestCases = [
'function foo() {}; foo = bar;',
'function foo() { foo = bar; }',
'foo = bar; function foo() { };',
'[foo] = bar; function foo() { };',
'({x: foo = 0} = bar); function foo() { };',
'function foo() { [foo] = bar; }',
'(function() { ({x: foo = 0} = bar); function foo() { }; })();',
];

for (const testCase of validTestCases) {
const {diagnostics} = await testLint(
testCase,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);
t.falsy(diagnostics.find(checkCategory));
}

for (const testCase of invalidTestCases) {
const {diagnostics} = await testLint(
testCase,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);
t.truthy(diagnostics.find(checkCategory));
}
});

test('no duplicated args allowed', async t => {
const duplicatedArgs = await testLint(
`
Expand Down
2 changes: 2 additions & 0 deletions packages/@romejs/js-compiler/transforms/lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import noDeleteVars from './noDeleteVars';
import noDebugger from './noDebugger';
import noDupeArgs from './noDupeArgs';
import noDuplicateKeys from './noDuplicateKeys';
import noFunctionAssign from './noFunctionAssign';
import noImportAssign from './noImportAssign';
import noLabelVar from './noLabelVar';
import noTemplateCurlyInString from './noTemplateCurlyInString';
Expand All @@ -41,6 +42,7 @@ export const lintTransforms = [
noDeleteVars,
noDupeArgs,
noDuplicateKeys,
noFunctionAssign,
noImportAssign,
noLabelVar,
noTemplateCurlyInString,
Expand Down
27 changes: 27 additions & 0 deletions packages/@romejs/js-compiler/transforms/lint/noFunctionAssign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 {FunctionBinding} from '@romejs/js-compiler/scope/bindings';

export default {
name: 'noFunctionAssign',
enter(path: Path): AnyNode {
const {node, scope} = path;
if (
node.type === 'AssignmentIdentifier' &&
scope.getBinding(node.name) instanceof FunctionBinding
) {
path.context.addNodeDiagnostic(node, {
category: 'lint/noFunctionAssign',
message: 'Reassignment of function declaration',
});
}
return node;
},
};