prettier plus add autofix and snapshot test
ADD
hello.js
+[1,,2]
\ No newline at end of file

MODIFY
packages/@romejs/js-compiler/__rtests__/__rsnapshots__/lint.ts.rsnap
packages/@romejs/js-compiler/__rtests__/__rsnapshots__/lint.ts.rsnap
{
"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}"
}
\ No newline at end of file

MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -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" );';

MODIFY
packages/@romejs/js-compiler/transforms/lint/sparseArray.ts
packages/@romejs/js-compiler/transforms/lint/sparseArray.ts
import {Path} from '@romejs/js-compiler';
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)) {
+ if (node.type === 'ArrayExpression' && node.elements.includes(undefined)) {
path.context.addNodeDiagnostic(node, {
category: 'lint/sparseArray',
- message: 'Your array contains an empty slot'
- })
+ message: 'Your array contains an empty slot',
+ });
+ const fixedArray: ArrayExpression = {
+ type: 'ArrayExpression',
+ elements: node.elements.map(elem =>
+ elem === undefined
+ ? referenceIdentifier.create({name: 'undefined'})
+ : elem,
+ ),
+ };
+ return arrayExpression.create(fixedArray);
}
return node;
- }
-}
+ },
+};

prettier plus add autofix and snapshot test