@fullstackio/cq
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -49,2 +49,16 @@ 'use strict'; | ||
var whitespace = new Set([' ', '\n', '\t', '\r']); | ||
function nextNewlinePos(code, start) { | ||
var pos = start; | ||
while (pos < code.length && code[pos] !== '\n') { | ||
pos++; | ||
} | ||
return pos; | ||
} | ||
function isNumeric(n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
} | ||
function movePositionByLines(code, numLines, position) { | ||
@@ -95,9 +109,33 @@ var opts = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
var whitespace = new Set([' ', '\n', '\t', '\r']); | ||
function adjustRangeForComments(ast, code, leading, trailing, engine, _ref2) { | ||
function adjustRangeWithWindow(code, startingLine, endingLine, _ref2) { | ||
var start = _ref2.start; | ||
var end = _ref2.end; | ||
var nodes = _ref2.nodes; | ||
// start, end are the range for the whole node | ||
var originalStart = start; | ||
if (isNumeric(startingLine)) { | ||
var trimNewline = startingLine > 0 ? false : true; | ||
start = movePositionByLines(code, startingLine, start, { trimNewline: trimNewline }); | ||
} | ||
if (endingLine === 0) { | ||
end = nextNewlinePos(code, start); | ||
return { start: start, end: end }; | ||
} | ||
if (isNumeric(endingLine)) { | ||
var _trimNewline2 = endingLine > 0 ? true : false; | ||
var eol = nextNewlinePos(code, originalStart); | ||
end = movePositionByLines(code, endingLine, eol /* <- notice */, { trimNewline: _trimNewline2 }); | ||
} | ||
return { start: start, end: end }; | ||
} | ||
function adjustRangeForComments(ast, code, leading, trailing, engine, _ref3) { | ||
var start = _ref3.start; | ||
var end = _ref3.end; | ||
var nodes = _ref3.nodes; | ||
// this is going to be part of the engine | ||
@@ -114,6 +152,6 @@ | ||
function modifyAnswerWithCall(ast, code, callee, args, engine, _ref3) { | ||
var start = _ref3.start; | ||
var end = _ref3.end; | ||
var nodes = _ref3.nodes; | ||
function modifyAnswerWithCall(ast, code, callee, args, engine, _ref4) { | ||
var start = _ref4.start; | ||
var end = _ref4.end; | ||
var nodes = _ref4.nodes; | ||
@@ -138,2 +176,10 @@ switch (callee) { | ||
break; | ||
case 'window': | ||
var _args2 = _slicedToArray(args, 2); | ||
var startingLine = _args2[0]; | ||
var endingLine = _args2[1]; | ||
return adjustRangeWithWindow(code, startingLine.value, endingLine.value, { start: start, end: end }); | ||
break; | ||
case 'comments': | ||
@@ -140,0 +186,0 @@ var leading = true, |
{ | ||
"name": "@fullstackio/cq", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "query code with selectors", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -403,3 +403,3 @@ <p align="center"> | ||
* Create a [remark](https://github.com/wooorm/remark) plugin to pull code into Markdown using queries | ||
* Get trailing and leading comments - [see here in ASTExplorer](https://github.com/fkling/astexplorer/tree/master/src/parsers/js/typescript.js#L68) | ||
* Support extracting lines of HTML (using regular CSS selectors) | ||
@@ -406,0 +406,0 @@ ## Limitations |
@@ -22,2 +22,16 @@ /** | ||
const whitespace = new Set([' ', '\n', '\t', '\r']); | ||
function nextNewlinePos(code, start) { | ||
let pos = start; | ||
while(pos < code.length && code[pos] !== '\n') { | ||
pos++; | ||
} | ||
return pos; | ||
} | ||
function isNumeric(n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
} | ||
function movePositionByLines(code, numLines, position, opts={}) { | ||
@@ -63,4 +77,26 @@ if(numLines < 0) { | ||
const whitespace = new Set([' ', '\n', '\t', '\r']); | ||
function adjustRangeWithWindow(code, startingLine, endingLine, {start, end}) { | ||
// start, end are the range for the whole node | ||
let originalStart = start; | ||
if(isNumeric(startingLine)) { | ||
let trimNewline = startingLine > 0 ? false : true; | ||
start = movePositionByLines(code, startingLine, start, {trimNewline}); | ||
} | ||
if(endingLine === 0) { | ||
end = nextNewlinePos(code, start); | ||
return {start, end}; | ||
} | ||
if(isNumeric(endingLine)) { | ||
let trimNewline = endingLine > 0 ? true : false; | ||
let eol = nextNewlinePos(code, originalStart); | ||
end = movePositionByLines(code, endingLine, eol /* <- notice */, {trimNewline}); | ||
} | ||
return {start, end}; | ||
} | ||
function adjustRangeForComments(ast, code, leading, trailing, engine, {start, end, nodes}) { | ||
@@ -93,2 +129,6 @@ // this is going to be part of the engine | ||
break; | ||
case 'window': | ||
let [startingLine, endingLine] = args; | ||
return adjustRangeWithWindow(code, startingLine.value, endingLine.value, {start, end}) | ||
break; | ||
case 'comments': | ||
@@ -95,0 +135,0 @@ let leading = true, trailing = false; |
@@ -227,3 +227,63 @@ import 'babel-polyfill' | ||
describe('from operator', () => { | ||
const src = ` | ||
import { bootstrap } from 'frobular'; | ||
let config = { | ||
template: \` | ||
<h1>Search</h1> | ||
// hi | ||
<p> | ||
<input type="text" #newquery | ||
[value]="query" | ||
(keydown.enter)="submit(newquery.value)"> | ||
<button (click)="submit(newquery.value)">Search</button> | ||
</p> | ||
<div *ngIf="results"> | ||
<div *ngIf="!results.length"> | ||
No tracks were found with the term '{{ query }}' | ||
</div> | ||
\` | ||
</div> | ||
} | ||
bootstrap(RoutesDemoApp, [ | ||
provideRouter(routes), | ||
provide(LocationStrategy, {useClass: HashLocationStrategy}) | ||
]); | ||
`; | ||
it('should get lines that are close below', () => { | ||
{ | ||
let { code } = cq(src, "window(.template, 0, 0)", {engine: 'typescript'}); | ||
const wanted = lines(src, 4, 4); | ||
assert.equal(code, wanted); | ||
} | ||
{ | ||
let { code } = cq(src, "window(.template, 0, 2)", {engine: 'typescript'}); | ||
const wanted = lines(src, 4, 6); | ||
assert.equal(code, wanted); | ||
} | ||
{ | ||
let { code } = cq(src, "window(.template, 1, 2)", {engine: 'typescript'}); | ||
const wanted = lines(src, 5, 6); | ||
assert.equal(code, wanted); | ||
} | ||
{ | ||
let { code } = cq(src, "window(.template, 3, 8)", {engine: 'typescript'}); | ||
const wanted = lines(src, 7, 12); | ||
assert.equal(code, wanted); | ||
} | ||
}) | ||
it('should get lines that are close around', () => { | ||
let { code } = cq(src, "window(.template, -1, 1)", {engine: 'typescript'}); | ||
const wanted = lines(src, 3, 5); | ||
assert.equal(code, wanted); | ||
}) | ||
}); | ||
}); |
604302
4288