Skip to content

putout v23.3.0

Choose a tag to compare

@coderaiser coderaiser released this 26 Dec 10:52
· 12431 commits to master since this release

A couple words about variable declarations

image

I seize him with a terrific struggle.
His great will and power
are inexhaustible.
He charges to the high plateau
far above the cloud-mists,
Or in an impenetrable ravine he stands.
(c) Zen Ten Bulls

Hi folks! It's a new day, and new release 😄!

Today I want to tell you about some useful feature that was improved drastically is:

operator.getBindingPath and variable declarations

Let's start from the code example:

const x  = {
   y: 'hello',
};

typeof hello === 'string';
typeof x.y === 'number';

How do you determine whether hello variable is declared or not? Let's try to solve it.

When you have a path like this one you get using match in Replacer:

const {operator} = require('putout');
const {getBindingPath} = operator;

module.exports.match = () => ({
    'typeof __a === "__b"': ({__a}, path) => {
        // when __a declared proceed to replace
        return getBindingPath(path, __a))
    }
});

And want to know is node __a is declared you can use getBindingPath, and it will solve it for you:

getBindingPath(path, 'hello');
// returns
null;

getBindingPath(path, 'x');
// returns
Path;

But what if you don't know what is the type of node you going to receive, but you know definitely that you need a name of this node to proceed? Just pass the node and getBindingPath will parse the name for you :)!

getBindingPath(path, node); // node can be Identifier or MemberExpression
// parse the name first, and then find it's declaration
Path;

New abilities of convert-typeof-to-is-type.

Now convert-typeof-to-is-type supports MemberExpressions is well:

const hello = {
    world: '',
};

-typeof hello.world === 'string'
+isString(hello.world);

And with help of declare-undefined-variables you will get:

+const isString = (a) => typeof a === 'string';
const hello = {
    world: '',
};

isString(hello.world);

So use both of them to get the most benefit 🎈!
Of course, both of them enabled by default 😏

Autofixes for Includer

When you using Includer and for some reason forget the it requires a function which returns an array
putout/includer will get you covered!

And such code:

module.exports.include = [
    'const __a = __b',
];

And even such:

module.exports.include = 'const __a = __b';

Will be converted to the only one correct:

module.exports.include = () => [
    'const __a = __b',
];

☝️ To get things working enable putout rule in .putout.json:

{
    "rules": {
        "putout": "on"
    }
}

Always use the simplest possible plugin type for your needs 🍬.

That's all for today! Happy holidays 🎄!

🔥 feature

  • (package) @putout/operate v6.13.0
  • (package) @putout/plugin-convert-typeof-to-is-type v2.0.0
  • (@putout/plugin-convert-typeof-to-is-type) drop support of putout < 23
  • (@putout/plugin-convert-typeof-to-is-type) add support of MemberExpression
  • (@putout/operate) getBinding/getBindingPath: name: string -> name: string | Node'
  • (@putout/plugin-putout) add includer