Socket
Book a DemoInstallSign in
Socket

snapdragon-position

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snapdragon-position

Snapdragon util and plugin for patching the position on an AST node.

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
3
-40%
Maintainers
1
Weekly downloads
 
Created
Source

snapdragon-position NPM version NPM monthly downloads NPM total downloads Linux Build Status

Snapdragon util and plugin for patching the position on an AST node.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save snapdragon-position

What does this do?

When used as a plugin, this adds a .position() method to a snapdragon-lexer instance, for adding position information to tokens.

If you prefer .loc over .position, see [snapdragon-location][].

Example

const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.use(position());

lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);

var token = lexer.advance();
console.log(token);

Adds a .position object to the token, like this:

Token {
  type: 'text',
  val: 'foo',
  match: [ 'foo', index: 0, input: 'foo/*' ],
  position: {
    start: { index: 0, column: 1, line: 1 },
    end: { index: 3, column: 4, line: 1 },
    range: [0, 3] // range is a getter
  } 
}

Usage

The main export is a function that can either be called directly to add a .position to a single token, or used as a plugin function with lexer.use().

position

Sets the start location and returns a function for setting the end location.

Params

  • lexer {Object}: Lexer instance
  • returns {Function}: Returns a function that takes a token as its only argument

Example

const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');

lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);

var pos = position(lexer);
var token = pos(lexer.advance());
console.log(token);

.plugin

Use as a plugin to add a .position method to your snapdragon-lexer instance, which automatically adds a position object to tokens when the .handle() method is used.

Example

var Lexer = require('snapdragon-lexer');
var position = require('snapdragon-position');
var lexer = new Lexer();
lexer.use(position());

.location

Get the current cursor location, with index, line and column. This is used in the .position() method to add the "start" and "end" locations to the position object, you can also call it directly when needed.

  • returns {Object}: Returns an object with the current target location, with cursor index, line, and column numbers.

Example

const Lexer = require('snapdragon-target');
const target = new Lexer();
console.log(target.location());
//=> Location { index: 0, line: 1, column: 1 };

.position

Returns a function for getting the current position.

  • returns {Function}: Returns a function that takes a token as its only argument

Example

const Lexer = require('snapdragon-target');
const target = new Lexer('foo/bar');
target.use(position.plugin());

target.set('text', function(tok) {
  // get start position before advancing target
  const pos = this.position();
  const match = this.match(/^\w+/);
  if (match) {
    // get end position after advancing target (with .match)
    return pos(this.token(match));
  }
});

Position information

Position {
  start: Location { index: 0, column: 1, line: 1 },
  end: Location { index: 3, column: 4, line: 1 },
  range: [getter] // [start.index, end.index]
} 

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.

This file was generated by verb-generate-readme, v0.6.0, on January 08, 2018.

Keywords

compile

FAQs

Package last updated on 08 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts