npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rocambole-indent

v2.0.4

Published

helpers for rocambole AST indentation

Downloads

78,781

Readme

rocambole-indent

Helpers to manipulate rocambole Indent tokens.

Used mainly by esformatter and its plugins.

API

var indent = require('rocambole-indent');

setOptions(opts)

setOptions used to set the indent value.

setOptions({
  // sets "value" used by `Indent` tokens (defaults to two spaces)
  value: '  ',
  // amount of indents added on `alignComments` if comment is inside an empty
  // block (surrounded by `{}`, `[]` or `()`) - defaults to `1`
  CommentInsideEmptyBlock: 1
});

inBetween(startToken, endToken, level)

Increase/Decrease the indent level in between the startToken and endToken.

It will not include the start and end tokens on the indentation range, only the tokens in between them.

// increase the indent level by 1
inBetween(node.startToken, node.endToken, 1);
// decrease the indent level by 1
inBetween(node.startToken, node.endToken, -1);
// zero does nothing
inBetween(node.endToken, 0);

Important: negative values only work if original Indent token contains a level property since there is no reliable way to infer this value (probably will only work if indent was added by this lib).

addLevel(token, level)

Increases/decreases the indent level at the beginning of the line that includes the given token.

// adds 2 indents
addLevel(node.startToken, 2);
// decrease indent level in 1 step
addLevel(node.endToken, -1);
// zero does nothing
addLevel(node.endToken, 0);

Important: negative values only work if original Indent token contains a level property since there is no reliable way to infer this value (probably will only work if indent was added by this lib).

sanitize(astOrNode)

Removes any Indent tokens that don't have a level property (this is usually the original indentation of the program parsed by rocambole) or that are not at the beginning of the line. Also removing WhiteSpace tokens that are at the beginning of the line to avoid mistakes.

// sanitize a single node
sanitize(node);
// sanitize whole AST
sanitize(ast);

updateBlockComment(token)

Updates BlockComment raw value to make sure all the lines have the same Indent level.

This is called internally by the addLevel and indentInBetween methods (if first token of line is a BlockComment), so as long as you only use those methods to edit the indent level you shouldn't need to call this.

alignComments(astOrNode)

Align all the comments based on the next/previous lines inside a given ast or node.

It will align the comments with the next line unless the comment block is followed by an empty line, in that case it will use the previous non-empty line as a reference.

Example output:

// aligned with next line
switch (foo) {
  // aligned with next non-empty line

  case bar:
    // aligned with next line
    baz();
    // this should be aligned with previous line since comment block is
    // followed by an empty line

  // aligned with next line
  case biz:
    // aligned with next line
    what();
// aligned with next line
}

function noop() {
  // indented since it's inside an empty block
}

// aligned with previous line since it's at the end of program

whiteSpaceToIndent(token, [indentValue])

Convert WhiteSpace token into Indent if it's the first token of the line.

You can pass a custom indentValue or it will use the value set by setOptions() to calculate the indent level (basically count how many times this string repeats inside the token.value).

var token = {
  type: 'WhiteSpace',
  value: '\t\t\t',
  prev: { type: 'LineBreak', value: '\n' }
};
whiteSpaceToIndent(token, '\t');
// edits properties in place
console.log(token.type);  // > "Indent"
console.log(token.level); // > 3

This is useful in case you want to make sure sanitize won't remove the original indents.

Debug

This module uses debug internally. To make it easier to identify what is wrong we sometimes run the esformatter tests with a DEBUG flag, like:

DEBUG=rocambole:indent npm test

License

Released under the MIT License