2.0.12 • Published 3 months ago

fatlint v2.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
3 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

3 months ago

1.14.0

3 months ago

1.12.0

4 months ago

1.18.1

3 months ago

1.18.0

3 months ago

1.16.1

3 months ago

1.16.0

3 months ago

1.18.3

3 months ago

1.18.2

3 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.5

3 months ago

2.0.4

3 months ago

2.0.7

3 months ago

2.0.6

3 months ago

2.0.9

3 months ago

2.0.8

3 months ago

2.0.1

3 months ago

2.0.0

3 months ago

1.15.0

3 months ago

1.13.1

4 months ago

1.13.0

4 months ago

1.17.0

3 months ago

2.0.11

3 months ago

2.0.12

3 months ago

2.0.10

3 months ago

1.2.0

4 months ago

1.1.0

4 months ago

1.9.1

4 months ago

1.9.0

4 months ago

1.8.1

4 months ago

1.7.2

4 months ago

1.8.0

4 months ago

1.7.1

4 months ago

1.4.4

4 months ago

1.7.0

4 months ago

1.6.1

4 months ago

1.4.3

4 months ago

1.6.0

4 months ago

1.4.2

4 months ago

1.5.0

4 months ago

1.4.1

4 months ago

1.4.0

4 months ago

1.3.0

4 months ago

1.2.1

4 months ago

1.11.0

4 months ago

1.10.0

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.0

4 months ago