New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fullstackio/cq

Package Overview
Dependencies
Maintainers
4
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fullstackio/cq - npm Package Compare versions

Comparing version 5.1.2 to 5.2.0

dist/cq.browser.js

20

dist/engines/babylon.js

@@ -8,13 +8,17 @@ "use strict";

var _babylon = require("babylon");
var babylon = _interopRequireWildcard(_babylon);
var _util = require("./util");
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
/**
* cq Babylon Engine
*
* Parse files with Babylon
*
*/
var babylon = require("babylon");
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /**
* cq Babylon Engine
*
* Parse files with Babylon
*
*/
// let babylon = require("babylon");

@@ -21,0 +25,0 @@

11

dist/engines/typescript.js

@@ -10,8 +10,4 @@ "use strict";

var ts = _interopRequireWildcard(_typescript);
var _util = require("./util");
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /**

@@ -25,4 +21,11 @@ * cq TypeScript Engine

*/
// import * as ts from "typescript";
var ts = {
SyntaxKind: _typescript.SyntaxKind,
createSourceFile: _typescript.createSourceFile,
ScriptTarget: _typescript.ScriptTarget,
getLeadingCommentRanges: _typescript.getLeadingCommentRanges
};
var ignoredProperties = new Set(["constructor", "parent"]);

@@ -29,0 +32,0 @@

@@ -6,2 +6,3 @@ "use strict";

});
exports.rangeExtents = rangeExtents;
/**

@@ -29,3 +30,2 @@ * cq Engine Util

rangeExtents: rangeExtents
};
module.exports = exports["default"];
};

@@ -6,3 +6,2 @@ "use strict";

});
exports.NodeTypes = undefined;

@@ -18,6 +17,88 @@ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /**

var _babelTraverse = require("babel-traverse");
var cq = function () {
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(code, queries) {
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var engine, ast, root, results;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
engine = opts.engine || (0, _babylon2.default)();
var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
if (typeof queries === "string") {
// parse into an array
queries = _queryParser2.default.parse(queries);
}
if (!(typeof engine === "string")) {
_context.next = 18;
break;
}
_context.t0 = engine;
_context.next = _context.t0 === "typescript" ? 6 : _context.t0 === "babylon" ? 8 : 10;
break;
case 6:
engine = (0, _typescript2.default)();
return _context.abrupt("break", 18);
case 8:
engine = (0, _babylon2.default)();
return _context.abrupt("break", 18);
case 10:
_context.prev = 10;
engine = require("cq-" + engine + "-engine");
_context.next = 17;
break;
case 14:
_context.prev = 14;
_context.t1 = _context["catch"](10);
throw new Error("unknown engine: " + engine);
case 17:
return _context.abrupt("break", 18);
case 18:
if (typeof engine === "function") {
// then just use it
}
debug(code);
_context.next = 22;
return Promise.resolve(engine.parse(code, Object.assign({}, opts.parserOpts)));
case 22:
ast = _context.sent;
// debug(JSON.stringify(ast, null, 2));
root = engine.getInitialRoot(ast);
results = resolveListOfQueries(ast, root, code, queries, engine, opts);
if (opts.undent) {
results.code = undent(results.code);
}
return _context.abrupt("return", results);
case 27:
case "end":
return _context.stop();
}
}
}, _callee, this, [[10, 14]]);
}));
return function cq(_x2, _x3) {
return _ref6.apply(this, arguments);
};
}();
var _queryParser = require("./query-parser");

@@ -37,6 +118,2 @@

var _debug = require("debug");
var _debug2 = _interopRequireDefault(_debug);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -50,5 +127,15 @@

var debug = (0, _debug2.default)("cq");
var debug = void 0;
if (process.browser) {
debug = function debug() {
var _console;
var NodeTypes = exports.NodeTypes = {
return (_console = console).log.apply(_console, arguments);
};
} else {
var debugLib = require("debug");
debug = debugLib("cq");
}
var NodeTypes = {
IDENTIFIER: "IDENTIFIER",

@@ -60,2 +147,3 @@ RANGE: "RANGE",

};
cq.NodeTypes = NodeTypes;

@@ -200,3 +288,2 @@ var QueryResultTypes = {

switch (callee) {

@@ -281,3 +368,5 @@ case "upto":

}
start++; // don't include the newline
if (code[start] === "\n") {
start++; // don't include the newline
}

@@ -536,88 +625,3 @@ // we also want to read to the end of the line for the node we found

exports.default = function () {
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(code, queries) {
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var engine, ast, root, results;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
engine = opts.engine || (0, _babylon2.default)();
if (typeof queries === "string") {
// parse into an array
queries = _queryParser2.default.parse(queries);
}
if (!(typeof engine === "string")) {
_context.next = 18;
break;
}
_context.t0 = engine;
_context.next = _context.t0 === "typescript" ? 6 : _context.t0 === "babylon" ? 8 : 10;
break;
case 6:
engine = (0, _typescript2.default)();
return _context.abrupt("break", 18);
case 8:
engine = (0, _babylon2.default)();
return _context.abrupt("break", 18);
case 10:
_context.prev = 10;
engine = require("cq-" + engine + "-engine");
_context.next = 17;
break;
case 14:
_context.prev = 14;
_context.t1 = _context["catch"](10);
throw new Error("unknown engine: " + engine);
case 17:
return _context.abrupt("break", 18);
case 18:
if (typeof engine === "function") {
// then just use it
}
debug(code);
_context.next = 22;
return Promise.resolve(engine.parse(code, Object.assign({}, opts.parserOpts)));
case 22:
ast = _context.sent;
// debug(JSON.stringify(ast, null, 2));
root = engine.getInitialRoot(ast);
results = resolveListOfQueries(ast, root, code, queries, engine, opts);
if (opts.undent) {
results.code = undent(results.code);
}
return _context.abrupt("return", results);
case 27:
case "end":
return _context.stop();
}
}
}, _callee, this, [[10, 14]]);
}));
function cq(_x2, _x3) {
return _ref6.apply(this, arguments);
}
return cq;
}();
exports.default = cq;
module.exports = exports["default"];

@@ -10,3 +10,7 @@ <p align="center">

> Supports JavaScript ES5, ES6, JSX, and TypeScript
>
> `cq` supports sophisticated, production-ready selectors and is used for all of the [Fullstack.io Books](https://fullstack.io)
> If you're a developer and you're interested in writing a programming book, then [read more here](https://www.fullstack.io/write-a-book/)
## Install

@@ -13,0 +17,0 @@

{
"name": "@fullstackio/cq",
"version": "5.1.2",
"version": "5.2.0",
"description": "query code with selectors",

@@ -9,7 +9,8 @@ "main": "dist/index.js",

"compile": "./node_modules/.bin/babel -d dist/ src/",
"prepublish": "npm run generate-readme && git diff ../../README.md && npm run generate-parser && npm run compile",
"prepublish": "npm run generate-parser && npm run compile && npm run rollup",
"test": "./node_modules/.bin/mocha --compilers js:babel-core/register",
"watchtest": "./node_modules/.bin/mocha --compilers js:babel-core/register --watch --watch-extensions pegjs",
"generate-readme": "node ./node_modules/.bin/cqmd -o ../../README.md -p . doc/readme/README.cq.md",
"generate-parser": "./node_modules/.bin/pegjs src/query.pegjs src/query-parser.js"
"generate-parser": "./node_modules/.bin/pegjs src/query.pegjs src/query-parser.js",
"rollup": "BROWSER=true node --max-old-space-size=8192 ./node_modules/rollup/bin/rollup -c rollup.config.js"
},

@@ -30,7 +31,6 @@ "bin": {

"debug": "^2.4.4",
"typescript": "^1.8.10",
"typescript": "3.2.1",
"yargs": "^4.7.1"
},
"devDependencies": {
"@fullstackio/cqmd": "^5.1.2",
"babel-cli": "^6.10.1",

@@ -47,4 +47,11 @@ "babel-core": "^6.9.1",

"mocha": "^2.5.3",
"pegjs": "^0.9.0"
}
"pegjs": "^0.9.0",
"rollup": "0.56.0",
"rollup-plugin-commonjs": "9.2.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-replace": "1.1.1"
},
"gitHead": "b6fd8c64734ea30a4703fc2a676da3be0a5b246c"
}

@@ -7,3 +7,4 @@ /**

*/
let babylon = require("babylon");
// let babylon = require("babylon");
import * as babylon from "babylon";
import { rangeExtents } from "./util";

@@ -10,0 +11,0 @@

@@ -9,5 +9,17 @@ /**

*/
import * as ts from "typescript";
// import * as ts from "typescript";
import {
SyntaxKind,
createSourceFile,
ScriptTarget,
getLeadingCommentRanges
} from "typescript";
import { rangeExtents } from "./util";
const ts = {
SyntaxKind,
createSourceFile,
ScriptTarget,
getLeadingCommentRanges
};
const ignoredProperties = new Set(["constructor", "parent"]);

@@ -54,7 +66,9 @@

if (
typeof node.getStart === "function" && typeof node.getEnd === "function"
typeof node.getStart === "function" &&
typeof node.getEnd === "function"
) {
range = { start: node.getStart(), end: node.getEnd() };
} else if (
typeof node.pos !== "undefined" && typeof node.end !== "undefined"
typeof node.pos !== "undefined" &&
typeof node.end !== "undefined"
) {

@@ -61,0 +75,0 @@ range = { start: node.pos, end: node.end };

@@ -8,3 +8,3 @@ /**

function rangeExtents(ranges) {
export function rangeExtents(ranges) {
let start = Number.MAX_VALUE;

@@ -11,0 +11,0 @@ let end = Number.MIN_VALUE;

@@ -8,3 +8,2 @@ /**

*/
import traverse from "babel-traverse";
import parser from "./query-parser";

@@ -16,6 +15,11 @@

import debugLib from "debug";
const debug = debugLib("cq");
let debug;
if (process.browser) {
debug = (...args) => console.log(...args);
} else {
const debugLib = require("debug");
debug = debugLib("cq");
}
export const NodeTypes = {
const NodeTypes = {
IDENTIFIER: "IDENTIFIER",

@@ -27,2 +31,3 @@ RANGE: "RANGE",

};
cq.NodeTypes = NodeTypes;

@@ -49,3 +54,3 @@ const QueryResultTypes = {

if (numLines < 0) {
let numPreviousLines = numLines * (-1);
let numPreviousLines = numLines * -1;
position--;

@@ -77,3 +82,3 @@ while (position > 0 && numPreviousLines > 0) {

let trimNewline = linesBefore > 0 ? true : false;
start = movePositionByLines(code, (-1) * linesBefore, start, {
start = movePositionByLines(code, -1 * linesBefore, start, {
trimNewline

@@ -176,3 +181,2 @@ });

) {
switch (callee) {

@@ -218,3 +222,4 @@ case "upto":

case "comments":
let leading = true, trailing = false;
let leading = true,
trailing = false;
return adjustRangeForComments(ast, code, leading, trailing, engine, {

@@ -250,3 +255,5 @@ start,

}
start++; // don't include the newline
if (code[start] === "\n") {
start++; // don't include the newline
}

@@ -522,3 +529,3 @@ // we also want to read to the end of the line for the node we found

acc.code = acc.code + opts.gapFiller + resolved.code;
acc.disjoint = true
acc.disjoint = true;
} else if (

@@ -554,3 +561,3 @@ opts.gapFiller &&

export default (async function cq(code, queries, opts = {}) {
async function cq(code, queries, opts = {}) {
let engine = opts.engine || babylonEngine();

@@ -599,2 +606,4 @@

return results;
});
}
export default cq;

@@ -9,3 +9,6 @@ import "babel-polyfill";

function lines(str, startLine, endLine) {
return str.split("\n").slice(startLine, endLine + 1).join("\n");
return str
.split("\n")
.slice(startLine, endLine + 1)
.join("\n");
}

@@ -102,17 +105,14 @@

it(
"should return an anonymous function assigned to a variable",
async () => {
let query = [
{
type: NodeTypes.IDENTIFIER,
matcher: "bye"
}
];
it("should return an anonymous function assigned to a variable", async () => {
let query = [
{
type: NodeTypes.IDENTIFIER,
matcher: "bye"
}
];
let { code } = await cq(someFunctions, query);
const wanted = lines(someFunctions, 5, 7);
assert.equal(code, wanted);
}
);
let { code } = await cq(someFunctions, query);
const wanted = lines(someFunctions, 5, 7);
assert.equal(code, wanted);
});

@@ -253,5 +253,3 @@ it("should return an arrow function assigned to a variable", async () => {

it("should extract code with gaps", async () => {
let {
code
} = await cq(
let { code } = await cq(
reactCreateClass,

@@ -275,5 +273,3 @@ "window(.Switch, 0, 0), .renderOtherStuff, window(.Switch, 0, 0, true)",

it("should extract code with gaps and contiguous", async () => {
let {
code
} = await cq(
let { code } = await cq(
reactCreateClass,

@@ -413,10 +409,7 @@ "window(.Switch, 0, 0), .render, window(.Switch, 0, 0, true)",

it(
"should get a constructor as a child of the class in a range",
async () => {
let { code } = await cq(es6Class, ".Polygon-(.Polygon .constructor)");
const wanted = lines(es6Class, 1, 11);
assert.equal(code, wanted);
}
);
it("should get a constructor as a child of the class in a range", async () => {
let { code } = await cq(es6Class, ".Polygon-(.Polygon .constructor)");
const wanted = lines(es6Class, 1, 11);
assert.equal(code, wanted);
});

@@ -606,10 +599,7 @@ it("should undent indented code", async () => {

it(
"shouldnt fail if you try to get comments where there are none",
async () => {
let { code } = await cq(src, "comments(.noComments)");
const wanted = lines(src, 14, 16);
assert.equal(code, wanted);
}
);
it("shouldnt fail if you try to get comments where there are none", async () => {
let { code } = await cq(src, "comments(.noComments)");
const wanted = lines(src, 14, 16);
assert.equal(code, wanted);
});
});

@@ -638,27 +628,21 @@

it(
"should find ranges for identifiers only if they are beyond the start of the range",
async () => {
let { code } = await cq(src, ".routes-.bootstrap");
const wanted = lines(src, 8, 17);
it("should find ranges for identifiers only if they are beyond the start of the range", async () => {
let { code } = await cq(src, ".routes-.bootstrap");
const wanted = lines(src, 8, 17);
assert.equal(code, wanted);
});
it("should find ranges for strings only if they are beyond the start of the range", async () => {
{
let { code } = await cq(src, "'hi'");
const wanted = lines(src, 6, 6);
assert.equal(code, wanted);
}
);
it(
"should find ranges for strings only if they are beyond the start of the range",
async () => {
{
let { code } = await cq(src, "'hi'");
const wanted = lines(src, 6, 6);
assert.equal(code, wanted);
}
{
let { code } = await cq(src, ".routes-'hi'");
const wanted = lines(src, 8, 13);
assert.equal(code, wanted);
}
{
let { code } = await cq(src, ".routes-'hi'");
const wanted = lines(src, 8, 13);
assert.equal(code, wanted);
}
);
});

@@ -801,5 +785,9 @@ it("should have comment separators for discontiguous queries", async () => {

it("should grab firstLineOf", async () => {
let { code } = await cq(src, "firstLineOf(.ThreadList),.ThreadList .contextTypes,lastLineOf(.ThreadList)", {
gapFiller: "\n // ...\n"
});
let { code } = await cq(
src,
"firstLineOf(.ThreadList),.ThreadList .contextTypes,lastLineOf(.ThreadList)",
{
gapFiller: "\n // ...\n"
}
);
const wanted = `class ThreadList extends React.Component {

@@ -815,5 +803,9 @@ static contextTypes = {

it("should properly add continuous newlines", async () => {
let { code } = await cq(src, "window(.ThreadList,0,0),.contextTypes,window(.ThreadList,0,0,true)", {
gapFiller: "\n // ...\n"
});
let { code } = await cq(
src,
"window(.ThreadList,0,0),.contextTypes,window(.ThreadList,0,0,true)",
{
gapFiller: "\n // ...\n"
}
);
const wanted = `class ThreadList extends React.Component {

@@ -828,7 +820,11 @@ static contextTypes = {

it("should properly add continuous newlines again", async () => {
let { code } = await cq(src, "window(.ThreadList,0,0),.contextTypes,window(.ThreadList,0,0,true),window(.ChatWindow,0,0),.ChatWindow .contextTypes,window(.ChatWindow,0,0,true)", {
gapFiller: "\n // ...\n"
});
// let { code } = await cq(src, "firstLineOf(.ThreadList),.contextTypes,lastLineOf(.ThreadList),firstLineOf(.ChatWindow),.ChatWindow .contextTypes,lastLineOf(.ChatWindow)", {
it("should properly add continuous newlines again", async () => {
let { code } = await cq(
src,
"window(.ThreadList,0,0),.contextTypes,window(.ThreadList,0,0,true),window(.ChatWindow,0,0),.ChatWindow .contextTypes,window(.ChatWindow,0,0,true)",
{
gapFiller: "\n // ...\n"
}
);
// let { code } = await cq(src, "firstLineOf(.ThreadList),.contextTypes,lastLineOf(.ThreadList),firstLineOf(.ChatWindow),.ChatWindow .contextTypes,lastLineOf(.ChatWindow)", {
const wanted = `class ThreadList extends React.Component {

@@ -850,3 +846,2 @@ static contextTypes = {

});
});

@@ -888,5 +883,9 @@

it("should grab render with ellipsis", async () => {
let { code } = await cq(src, "(firstLineOf(.Clock),.render,lastLineOf(.Clock),choose(.Clock,1))", {
gapFiller: "\n // ...\n"
});
let { code } = await cq(
src,
"(firstLineOf(.Clock),.render,lastLineOf(.Clock),choose(.Clock,1))",
{
gapFiller: "\n // ...\n"
}
);
const wanted = `class Clock extends React.Component {

@@ -907,6 +906,23 @@ // ...

});
});
describe("more JavaScript", async () => {
const jsCode = `var a = 1;
function hello() {
return 'hello';
}`;
it("should return the very first character in a string", async () => {
let query = [
{
type: NodeTypes.IDENTIFIER,
matcher: "a"
}
];
let { code } = await cq(jsCode, query);
const wanted = "var a = 1;";
assert.equal(code, wanted);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc