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 4 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
1 change: 1 addition & 0 deletions hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,,2]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"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}"
"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}"
}
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('format disabled in project config should not regenerate the file', async t => {
// Intentionally weird formatting
const sourceText = 'foobar ( "yes" );';
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,10 +9,12 @@ import defaultExportSameBasename from './defaultExportSameBasename';
import undeclaredVariables from './undeclaredVariables';
import unusedVariables from './unusedVariables';
import emptyBlocks from './emptyBlocks';
import sparseArray from './sparseArray';

export const lintTransforms = [
undeclaredVariables,
defaultExportSameBasename,
unusedVariables,
emptyBlocks,
sparseArray,
];
39 changes: 39 additions & 0 deletions packages/@romejs/js-compiler/transforms/lint/sparseArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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,
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',
});
const fixedArray: ArrayExpression = {
type: 'ArrayExpression',
macovedj marked this conversation as resolved.
Show resolved Hide resolved
elements: node.elements.map(elem =>
elem === undefined
? referenceIdentifier.create({name: 'undefined'})
: elem,
),
};
return arrayExpression.create(fixedArray);
macovedj marked this conversation as resolved.
Show resolved Hide resolved
}

return node;
},
};