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

add sparse array check to lint #91

Merged
merged 8 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"empty file": "Object {\n diagnostics: Array []\n src: ''\n ast: Program {\n body: Array []\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 0\n index: 0\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n }\n}",
"undeclared variable": "Object {\n src: 'foobar;'\n diagnostics: Array [\n Object {\n category: 'lint/undeclaredVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Undeclared variable <emphasis>foobar</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 7\n index: 7\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 7\n index: 7\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: ReferenceIdentifier {\n name: 'foobar'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n }\n }\n ]\n }\n}",
"sparse array": "Object {\n src: '[1,,2]'\n diagnostics: Array [\n Object {\n category: 'lint/sparseArray'\n filename: 'unknown'\n language: 'js'\n message: 'Your array contains an empty slot'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: ArrayExpression {\n loc: undefined\n elements: Array [\n NumericLiteral {\n value: 1\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 2\n index: 2\n line: 1\n }\n start: Object {\n column: 1\n index: 1\n line: 1\n }\n }\n }\n ReferenceIdentifier {\n name: 'undefined'\n loc: undefined\n }\n NumericLiteral {\n value: 2\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 5\n index: 5\n line: 1\n }\n start: Object {\n column: 4\n index: 4\n line: 1\n }\n }\n }\n ]\n }\n }\n ]\n }\n}",
"unsafe negation": "Object {\n src: '!1 in [1,2]'\n diagnostics: Array [\n Object {\n category: 'lint/unsafeNegation'\n filename: 'unknown'\n language: 'js'\n message: 'Unsafe usage of negation operator in left side of binary expression'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: BinaryExpression {\n operator: 'in'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n left: UnaryExpression {\n operator: '!'\n prefix: true\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 2\n index: 2\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n argument: NumericLiteral {\n value: 1\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 2\n index: 2\n line: 1\n }\n start: Object {\n column: 1\n index: 1\n line: 1\n }\n }\n }\n }\n right: ArrayExpression {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 6\n index: 6\n line: 1\n }\n }\n elements: Array [\n NumericLiteral {\n value: 1\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 8\n index: 8\n line: 1\n }\n start: Object {\n column: 7\n index: 7\n line: 1\n }\n }\n }\n NumericLiteral {\n value: 2\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 10\n index: 10\n line: 1\n }\n start: Object {\n column: 9\n index: 9\n line: 1\n }\n }\n }\n ]\n }\n }\n }\n ]\n }\n}",
"unsafe negation: 1": "Object {\n diagnostics: Array []\n src: '!(1 in [1,2])'\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 13\n index: 13\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 13\n index: 13\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: UnaryExpression {\n operator: '!'\n prefix: true\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 13\n index: 13\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n argument: BinaryExpression {\n operator: 'in'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 12\n index: 12\n line: 1\n }\n start: Object {\n column: 2\n index: 2\n line: 1\n }\n }\n left: NumericLiteral {\n value: 1\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 3\n index: 3\n line: 1\n }\n start: Object {\n column: 2\n index: 2\n line: 1\n }\n }\n }\n right: ArrayExpression {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 12\n index: 12\n line: 1\n }\n start: Object {\n column: 7\n index: 7\n line: 1\n }\n }\n elements: Array [\n NumericLiteral {\n value: 1\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 9\n index: 9\n line: 1\n }\n start: Object {\n column: 8\n index: 8\n line: 1\n }\n }\n }\n NumericLiteral {\n value: 2\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 10\n index: 10\n line: 1\n }\n }\n }\n ]\n }\n }\n }\n }\n ]\n }\n}",
"no async promise executor": "Object {\n src: 'new Promise(async function foo() {})'\n diagnostics: Array [\n Object {\n category: 'lint/noAsyncPromiseExecutor'\n filename: 'unknown'\n language: 'js'\n message: 'Promise executor functions should not be async.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 35\n index: 35\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n ]\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 36\n index: 36\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 36\n index: 36\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: NewExpression {\n optional: undefined\n typeArguments: undefined\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 36\n index: 36\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n callee: ReferenceIdentifier {\n name: 'Promise'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 4\n index: 4\n line: 1\n }\n }\n }\n arguments: Array [\n FunctionExpression {\n id: BindingIdentifier {\n name: 'foo'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 30\n index: 30\n line: 1\n }\n start: Object {\n column: 27\n index: 27\n line: 1\n }\n }\n }\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 35\n index: 35\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n body: BlockStatement {\n body: Array []\n directives: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 35\n index: 35\n line: 1\n }\n start: Object {\n column: 33\n index: 33\n line: 1\n }\n }\n }\n head: FunctionHead {\n async: true\n generator: false\n hasHoistedVars: false\n params: Array []\n predicate: undefined\n rest: undefined\n returnType: undefined\n thisType: undefined\n typeParameters: undefined\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 33\n index: 33\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n }\n }\n ]\n }\n }\n ]\n }\n}",
Expand Down
6 changes: 6 additions & 0 deletions packages/@romejs/js-compiler/__rtests__/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ test('undeclared variable', async t => {
]);
});

test('sparse array', async t => {
const res = await testLint(`[1,,2]`, LINT_ENABLED_FORMAT_DISABLED_CONFIG);

t.snapshot(res);
});

test('unsafe negation', async t => {
const res = await testLint(
`!1 in [1,2]`,
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 @@ -9,6 +9,7 @@ import defaultExportSameBasename from './defaultExportSameBasename';
import undeclaredVariables from './undeclaredVariables';
import unusedVariables from './unusedVariables';
import emptyBlocks from './emptyBlocks';
import sparseArray from './sparseArray';
import noCompareNegZero from './noCompareNegZero';
import unsafeNegation from './unsafeNegation';
import noAsyncPromiseExecutor from './noAsyncPromiseExecutor';
Expand All @@ -20,6 +21,7 @@ export const lintTransforms = [
defaultExportSameBasename,
unusedVariables,
emptyBlocks,
sparseArray,
noCompareNegZero,
unsafeNegation,
noAsyncPromiseExecutor,
Expand Down
33 changes: 33 additions & 0 deletions packages/@romejs/js-compiler/transforms/lint/sparseArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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';
macovedj marked this conversation as resolved.
Show resolved Hide resolved
import {AnyNode} from '@romejs/js-ast';
import {referenceIdentifier, arrayExpression} from '@romejs/js-ast';

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

if (node.type === 'ArrayExpression' && node.elements.includes(undefined)) {
path.context.addNodeDiagnostic(node, {
category: 'lint/sparseArray',
message: 'Your array contains an empty slot',
});
return arrayExpression.quick(
node.elements.map(elem =>
elem === undefined
? referenceIdentifier.create({name: 'undefined'})
: elem,
),
);
}

return node;
},
};