2.0.12 • Published 10 months ago

fatlint v2.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

FatLint License NPM version Build Status Coverage Status

FAT-based JavaScript linter. The main idea is using FAT-filesystem for traversing AST, so each node written to a file.

FatLint

Install

npm i fatlint

API

traverse willing to support similar API as @babel/traverse.

remove()

Removing a node.

import {
    traverse,
    parse,
    print,
} from 'fatlint';
import {types} from 'putout';

const {isIdentifier} = types;
const source = `const a = 'hello'; const b = 'world'`;
const filesystem = parse(source, disk);

traverse(filesystem, {
    VariableDeclarator(path) {
        if (isIdentifier(path.node.id, {name: 'world'}))
            path.remove(path);
    },
});

print(filesystem);
// returns
`const a = 'hello'\n`;

replaceWith()

Replace node.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
} from 'fatlint';

const {numericLiteral} = types;

const source = `const a = 'hello';`;
const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path.replaceWith(numericLiteral(5));
    },
});

print(filesystem);
// returns
`const a = 5;\n`;

replaceWithMultiple()

Replace with multiple nodes.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
} from 'fatlint';

const {numericLiteral} = types;

const source = `const a = ['hello']`;
const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path.replaceWithMultipleNodes([
            numericLiteral(5),
            numericLiteral(3),
        ]);
    },
});

print(filesystem);
// returns
`const a = [5, 3];\n`;

insertBefore()

Insert sibling node.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
} from 'fatlint';

const {numericLiteral} = types;

const source = `const a = ['hello']`;
const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path.insertBefore(numericLiteral(5));
    },
});

print(filesystem);
// returns
`const a = [5, 'hello'];\n`;

insertAfter()

Insert sibling node.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
} from 'fatlint';

const {numericLiteral} = types;

const source = `const a = ['hello']`;
const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path.insertAfter(numericLiteral(5));
    },
});

print(filesystem);
// returns
`const a = ['hello', 5];\n`;

getNextSibling()

Get next sibling.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
    createDisk,
} from 'fatlint';

const disk = await createDisk();
const source = `const a = 'hello'; const b = 'world'`;

const filesystem = parse(source, disk);

traverse(filesystem, {
    VariableDeclaration(path) {
        path
            .getNextSibling()
            .remove();
    },
});

print(filesystem);
// returns
`const a = 'hello';\n`;

getPrevSibling()

Get prev sibling.

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
    createDisk,
} from 'fatlint';

const disk = await createDisk();
const source = `const a = 'hello'; const b = 'world'`;

const filesystem = parse(source, disk);

traverse(filesystem, {
    VariableDeclaration(path) {
        path
            .getPrevSibling()
            .remove();
    },
});

print(filesystem);
// returns
`const b = 'world';\n`;

path.find()

Find path:

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
    createDisk,
} from 'fatlint';

const {isVariableDeclaration} = types;

const disk = await createDisk();
const source = `function x() {const a = 'hello'; const b = 'world';}`;

const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path
            .find(isVariableDeclaration)
            .remove();
    },
});

print(filesystem);
// returns
`function x() {}\n`;

path.parentPath

Access to parentPath:

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
    createDisk,
} from 'fatlint';

const {isVariableDeclaration} = types;

const disk = await createDisk();
const source = `function x() {const a = 'hello'; const b = 'world';}`;

const filesystem = parse(source, disk);

traverse(filesystem, {
    StringLiteral(path) {
        path.parentPath.remove();
    },
});

print(filesystem);
// returns
`function x() {}\n`;

path.stop()

Access to parentPath:

path.parentPath

Stopping Traversal:

import {types} from 'putout';
import {
    traverse,
    parse,
    print,
    createDisk,
} from 'fatlint';

const {isVariableDeclaration} = types;

const disk = await createDisk();
const source = `function x() {const a = 'hello'; const b = 'world';}`;

const filesystem = parse(source, disk);
let counter = 0;

traverse(filesystem, {
    StringLiteral(path) {
        ++counter;
        path.parentPath.stop();
    },
});

console.log(counter);
// outputs
1;

License

MIT

1.14.1

10 months ago

1.14.0

10 months ago

1.12.0

10 months ago

1.18.1

10 months ago

1.18.0

10 months ago

1.16.1

10 months ago

1.16.0

10 months ago

1.18.3

10 months ago

1.18.2

10 months ago

2.0.3

10 months ago

2.0.2

10 months ago

2.0.5

10 months ago

2.0.4

10 months ago

2.0.7

10 months ago

2.0.6

10 months ago

2.0.9

10 months ago

2.0.8

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.15.0

10 months ago

1.13.1

10 months ago

1.13.0

10 months ago

1.17.0

10 months ago

2.0.11

10 months ago

2.0.12

10 months ago

2.0.10

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago

1.9.1

10 months ago

1.9.0

10 months ago

1.8.1

10 months ago

1.7.2

10 months ago

1.8.0

10 months ago

1.7.1

10 months ago

1.4.4

10 months ago

1.7.0

10 months ago

1.6.1

10 months ago

1.4.3

10 months ago

1.6.0

10 months ago

1.4.2

10 months ago

1.5.0

10 months ago

1.4.1

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.1

10 months ago

1.11.0

10 months ago

1.10.0

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.0

11 months ago