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

No cond assign eslint implementation #116

Merged
merged 5 commits into from
Mar 3, 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

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions packages/@romejs/js-compiler/__rtests__/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,44 @@ test('disallows comparing negative zero', async t => {
t.looksLike(res2.diagnostics, []);
});

test('no cond assign', async t => {
const forLoop = await testLint(
`for (let i = 1; i = 10; i++) {
console.log('foo')
}`,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);

t.snapshot(forLoop);

const ifCondition = await testLint(
`if(foo = 'bar') {
console.log('foo')
}`,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);

t.snapshot(ifCondition);

const whileLoop = await testLint(
`while (foo = 'bar' {
console.log('foo')
}`,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);

t.snapshot(whileLoop);

const DoWhileLoop = await testLint(
`do {
console.log('foo)
} while (foo = 'bar')`,
LINT_ENABLED_FORMAT_DISABLED_CONFIG,
);

t.snapshot(DoWhileLoop);
});

test('no label var', async t => {
const badLabel = await testLint(
`
Expand Down
20 changes: 14 additions & 6 deletions packages/@romejs/js-compiler/transforms/lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
*/

import defaultExportSameBasename from './defaultExportSameBasename';
import undeclaredVariables from './undeclaredVariables';
import unusedVariables from './unusedVariables';
import disallowVar from './disallowVar';
import emptyBlocks from './emptyBlocks';
import sparseArray from './sparseArray';
import noCompareNegZero from './noCompareNegZero';
import unsafeNegation from './unsafeNegation';
import noAsyncPromiseExecutor from './noAsyncPromiseExecutor';
import noLabelVar from './noLabelVar';
import noCompareNegZero from './noCompareNegZero';
import noCondAssign from './noCondAssign';
import noDuplicateKeys from './noDuplicateKeys';
import noLabelVar from './noLabelVar';
import undeclaredVariables from './undeclaredVariables';
import unsafeNegation from './unsafeNegation';
import unusedVariables from './unusedVariables';
import disallowVar from './disallowVar';
import noUnsafeFinally from './noUnsafeFinally';
import noDeleteVars from './noDeleteVars';
import noTemplateCurlyInString from './noTemplateCurlyInString';

export const lintTransforms = [
undeclaredVariables,
defaultExportSameBasename,
unusedVariables,
disallowVar,
emptyBlocks,
sparseArray,
noCompareNegZero,
unsafeNegation,
noAsyncPromiseExecutor,
noLabelVar,
noCompareNegZero,
noCondAssign,
noDuplicateKeys,
noLabelVar,
undeclaredVariables,
unsafeNegation,
unusedVariables,
disallowVar,
noUnsafeFinally,
noDeleteVars,
Expand Down
31 changes: 31 additions & 0 deletions packages/@romejs/js-compiler/transforms/lint/noCondAssign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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';

export default {
name: 'noCondAssign',
enter(path: Path): AnyNode {
const {node} = path;

if (
(node.type === 'IfStatement' ||
node.type === 'ForStatement' ||
node.type === 'WhileStatement' ||
node.type === 'DoWhileStatement') &&
node.test &&
node.test.type === 'AssignmentExpression'
) {
path.context.addNodeDiagnostic(node, {
category: 'lint/noCondAssign',
message: 'Cannot assign variable in loop condition',
});
}
return node;
},
};
10 changes: 7 additions & 3 deletions packages/@romejs/js-generator/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@ export default class Buffer {
}

flush(): void {
let item;
while ((item = this._queue.pop())) {
this._append(...item);
while (true) {
const item = this._queue.pop();
if (item === undefined) {
break;
} else {
this._append(...item);
}
}
}

Expand Down