@putout/plugin-printer
v8.17.0
Published
πPutout plugin adds support of transformations for @putout/printer
Downloads
705
Maintainers
Readme
@putout/plugin-printer 
πPutout adds support of transformations for @putout/printer.
Install
npm i @putout/plugin-printer -DRules
- β add-args;
- β add-missing-spaces-to-type-checker;
- β add-missing-colon-to-type-checker;
- β add-missing-tuples-to-type-checker;
- β apply-breakline;
- β apply-computed-print;
- β apply-create-test-url;
- β apply-linebreak;
- β apply-types;
- β check-type-passed-to-type-checker;
- β check-if-success-possible-in-type-checker;
- β merge-tuple-of-type-checker;
- β declare;
- β remove-args;
- β remove-useless-maybe;
- β remove-legacy-test-declaration;
- β remove-trailing-spaces-from-type-checker;
- β remove-useless-spaces-from-type-checker;
- β remove-useless-arrow-from-type-checker;
- β remove-useless-colon-from-type-checker;
- β remove-useless-tuples-from-type-checker;
- β remove-useless-not-from-type-checker;
- β remove-useless-path-from-type-checker;
- β reverse-comparison-in-type-checker;
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-arrow-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-arrow-from-type-checker
Checkout in πPutout Editor.
β Example of incorrect code
const isTwoLongStringsInsideArray = createTypeChecker([
['-: -> !', isTwoLongStrings],
['+: parentPath -> -> ArrayExpression'],
]);β Example of correct code
const isTwoLongStringsInsideArray = createTypeChecker([
['-: -> !', isTwoLongStrings],
['+: parentPath -> ArrayExpression'],
]);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
