
Product
Rust Support in Socket Is Now Generally Available
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.
snapdragon-position
Advanced tools
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 with npm:
$ npm install --save snapdragon-position
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
}
}
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().
Sets the start location and returns a function for setting the end location.
Params
lexer {Object}: Lexer instancereturns {Function}: Returns a function that takes a token as its only argumentExample
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);
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());
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 };
Returns a function for getting the current position.
returns {Function}: Returns a function that takes a token as its only argumentExample
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 {
start: Location { index: 0, column: 1, line: 1 },
end: Location { index: 3, column: 4, line: 1 },
range: [getter] // [start.index, end.index]
}
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 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
(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:
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 08, 2018.
FAQs
Snapdragon util and plugin for patching the position on an AST node.
The npm package snapdragon-position receives a total of 3 weekly downloads. As such, snapdragon-position popularity was classified as not popular.
We found that snapdragon-position demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.

Research
Five coordinated Chrome extensions enable session hijacking and block security controls across enterprise HR and ERP platforms.