Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

@putout/plugin-printer NPM version

🐊Putout adds support of transformations for @putout/printer.

Install

npm i @putout/plugin-printer -D

Rules

Config

{
    "rules": {
        "printer/add-args": "on",
        "printer/add-missing-tuples-to-type-checker": "on",
        "printer/add-missing-colon-to-type-checker": "on",
        "printer/add-missing-spaces-to-type-checker": "on",
        "printer/apply-breakline": "on",
        "printer/apply-linebreak": "on",
        "printer/apply-computed-print": "on",
        "printer/apply-create-test-url": "on",
        "printer/apply-types": "on",
        "printer/check-type-passed-to-type-checker": "on",
        "printer/check-if-success-possible-in-type-checker": "on",
        "printer/declare": "on",
        "printer/merge-tuple-of-type-checker": "on",
        "printer/remove-args": "on",
        "printer/remove-useless-maybe": "on",
        "printer/remove-trailing-spaces-from-type-checker": "on",
        "printer/remove-useless-spaces-from-type-checker": "on",
        "printer/remove-useless-colon-from-type-checker": "on",
        "printer/remove-useless-not-from-type-checker": "on",
        "printer/remove-useless-path-from-type-checker": "on",
        "printer/remove-useless-tuples-from-type-checker": "on",
        "printer/reverse-comparison-in-type-checker": "on"
    }
}

apply-breakline

-print.newline();
-indent();
print.breakline();

apply-linebreak;

-indent();
-print.newline();
print.linebreak();

apply-types

Checkout in 🐊Putout Editor.

-const {isIdentifier} = require('@babel/types');
+const {types} = require('@babel/types');
+const {isIdentifier} = types;

add-args

❌ Example of incorrect code

module.exports = {
    TSPropertySignature(path) {
        const {optional} = path.node;
        print('__key');
        maybe.print(optional, '?');
    },
};

✅ Example of correct code

module.exports = {
    TSPropertySignature(path, {print, maybe}) {
        const {optional} = path.node;
        print('__key');
        maybe.print(optional, '?');
    },
};

add-missing-spaces-to-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['-:->!', isInsideArray],
    ['-:parentPath->', isCoupleLines],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['-: -> !', isInsideArray],
    ['-: parentPath ->', isCoupleLines],
]);

add-missing-colon-to-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['+ -> !', isInsideArray],
    ['- parentPath ->', isCoupleLines],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['+: -> !', isInsideArray],
    ['-: parentPath ->', isCoupleLines],
]);

add-missing-tuple-to-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    '- : -> !StringLiteral',
    '- : -> BlockStatement',
    '- : -> WrongType',
    ['- : ->', isBlockStatement],
    ['-', isBlockStatement],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['- : -> !StringLiteral'],
    ['- : -> BlockStatement'],
    ['- : -> WrongType'],
    ['- : ->', isBlockStatement],
    ['-', isBlockStatement],
]);

apply-computed-print

❌ Example of incorrect code

print(path.get('block'));

✅ Example of correct code

print('__block');

apply-create-test-url

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

import {createTest} from '#test';

const {test, fixture} = createTest(__dirname);

✅ Example of correct code

import {createTest} from '#test';

const {test, fixture} = createTest(import.meta.url);

remove-args

❌ Example of incorrect code

print.indent(is);

✅ Example of correct code

print.indent();

remove-useless-maybe

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

maybe.print.linebreak(wasNewline);
maybe.print.newline(!wasNewline);

✅ Example of correct code

maybe.indent(wasNewline);
print.newline();

remove-useless-spaces-from-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['- : -> !', isInsideArray],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['-: -> !', isInsideArray],
]);

remove-useless-colon-from-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['+:', isInsideArray],
    ['-:', isCoupleLines],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['+', isInsideArray],
    ['-', isCoupleLines],
]);

remove-useless-not-from-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['-: -> !+'],
    ['+: -> !-'],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['-: -> -'],
    ['+: -> +'],
]);

remove-useless-path-from-type-checker

Selector always starts from path, so it can be parentPath., or node., but never path., since it is self reference.

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['-: path.parentPath -> ', isCoupleLines],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['-: parentPath -> ', isCoupleLines],
]);

remove-useless-tuples-from-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const allStrings = createTypeChecker([
    ['- : -> BlockStatement'],
    ['- : -> WrongType'],
]);

✅ Example of correct code

export const allStrings = createTypeChecker([
    '- : -> BlockStatement',
    '- : -> WrongType',
]);

remove-trailing-spaces-from-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['-: parentPath -> ', isCoupleLines],
]);

✅ Example of correct code

export const beforeIf = createTypeChecker([
    ['-: parentPath ->', isCoupleLines],
]);

check-if-success-possible-in-type-checker

Checkout in 🐊Putout Editor. When you have only negative branch defined in type checker it never never succeeds (+), and always returns false.

Consider adding success branch, or remove type checker.

☝️ This rule has no autofix, because most likely tests will fail after auto fix, and instead of observing code with a problem, you will first see failed tests, and then after some searches determine that there is a wrong auto fix.

❌ Example of incorrect code

const isSimple = createTypeChecker([
    '-: -> SpreadElement',
    '-: -> Identifier',
    '-: -> !CallExpression',
]);

const isSimpleAfterObject = createTypeChecker([
    ['-', isSimple],
    ['-', callWithNext(isObjectExpression)],
    ['-', callWithPrev(isObjectExpression)],
]);

✅ Example of correct code

Possible fix:

const isSimple = createTypeChecker([
    '-: -> SpreadElement',
    '-: -> Identifier',
    '+: -> !CallExpression',
]);

const isSimpleAfterObject = createTypeChecker([
    ['-', isSimple],
    ['-', callWithNext(isObjectExpression)],
    ['+', callWithPrev(isObjectExpression)],
]);

check-type-passed-to-type-checker

Checkout in 🐊Putout Editor.

☝️ This rule has no autofix, because most likely tests will fail after auto fix, and instead of observing code with a problem, you will first see failed tests, and then after some searches determine that there is a wrong auto fix.

❌ Example of incorrect code

export const beforeIf = createTypeChecker([
    ['- : -> WrongType'],
]);

✅ Example of correct code

Here is possible correct code:

export const beforeIf = createTypeChecker([
    ['- : -> Identifier'],
]);

declare

❌ Example of incorrect code

isIdentifier();

test('', (t) => {
    t.print(fixture.returnStatement);
});

✅ Example of correct code

const {types} = require('@putout/babel');
const {createTest} = require('#test');

const {test, fixture} = createTest(__dirname);
const {isIdentifier} = types;

isIdentifier();

test('', (t) => {
    t.print(fixture.returnStatement);
});

merge-tuple-of-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const isNewlineAfterComma = createTypeChecker([
    ['+: -> !', isObjectExpression],
    ['+: ->', isStringLiteral],
    ['+', isStringLiteral],
]);

✅ Example of correct code

export const isNewlineAfterComma = createTypeChecker([
    ['+: -> !ObjectExpression'],
    ['+: -> StringLiteral'],
    ['+: StringLiteral'],
]);

reverse-comparison-in-type-checker

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
    ['-: node.elements.length -> !', '<', 2],
    ['-: node.elements.length -> !', '>', 2],
    ['-: node.elements.length -> !', '>=', 2],
    ['-: node.elements.length -> !', '<=', 2],
]);

✅ Example of correct code

const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
    ['-: node.elements.length', '>=', 2],
    ['-: node.elements.length', '<=', 2],
    ['-: node.elements.length', '<', 2],
    ['-: node.elements.length', '>', 2],
]);

remove-legacy-test-declaration

-const {printExtension} = require('../../../test/printer');
-const {readFixtures} = require('../../../test/fixture');
-
-const fixture = readFixtures(__dirname);
-
-const test = extend({
-    print: printExtension,
-});

License

MIT