Skip to content

Commit

Permalink
switch from jstestr to mocha; track coverage; drop node <8 (#84)
Browse files Browse the repository at this point in the history
- Breaking change: To ensure passing `nyc`, bump `engines` to Node 8
- Fix: Multiple leading decimals were not being handled correctly (produced inner array followed by "." such that `21.` (e.g., at beginning of "21.35") would be processed instead as `2,1.`)
- Enhancement: Throw for unknown operator
- Refactoring: Avoid unnecessary code to unescape `\\a` (`\a` has no special meaning)
- Maintenance: Add pegjs as 2 sp. in `.editorconfig`
- Testing: Expand to effective 100% coverage
- Testing: Switch from jstestr to mocha
- Testing: Get coverage for bad operator, class name, and selector type
- Simplify `leadingDecimals` setting in grammar
- Add coverage thresholds (tentatively at 100%)
- Update .eslintrc.js
- Remove deprecated `preferGlobal`: https://docs.npmjs.com/files/package.json#preferglobal
- Fix test name to reflect contents
- cover more lines
- fix linting
- gitignore nyc_output

Co-authored-by: Michael Ficarra <[email protected]>
  • Loading branch information
brettz9 and michaelficarra authored Mar 9, 2020
1 parent 8a5b594 commit 176a6b1
Show file tree
Hide file tree
Showing 37 changed files with 1,485 additions and 1,261 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ indent_size = 4

[*.json]
indent_size = 2

[*.pegjs]
indent_size = 2
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ module.exports = {
// Remove after converting to ESM
define: "readonly"
},
overrides: [{
files: 'tests/**',
globals: {
assert: true
},
env: {
mocha: true
}
}],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018
Expand Down
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist
/.nyc_output
/node_modules
/coverage
/dist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The following selectors are supported:
* [attribute existence](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr]`
* [attribute value](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr="foo"]` or `[attr=123]`
* attribute regex: `[attr=/foo.*/]`
* attribute conditions: `[attr!="foo"]`, `[attr>2]`, `[attr<3]`, `[attr>=2]`, or `[attr<=3]`
* attribute conditions: `[attr!="foo"]`, `[attr>2]`, `[attr<3]`, `[attr>=2]`, or `[attr<=3]`
* nested attribute: `[attr.level2="foo"]`
* field: `FunctionDeclaration > Identifier.id`
* [First](http://dev.w3.org/csswg/selectors4/#the-first-child-pseudo) or [last](http://dev.w3.org/csswg/selectors4/#the-last-child-pseudo) child: `:first-child` or `:last-child`
Expand Down
4 changes: 1 addition & 3 deletions esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function matches(node, selector, ancestry) {
case 'attribute':
p = getPath(node, selector.name);
switch (selector.operator) {
case null:
case void 0:
return p != null;
case '=':
Expand All @@ -135,7 +134,7 @@ function matches(node, selector, ancestry) {
case '>': return p > selector.value.value;
case '>=': return p >= selector.value.value;
}
break;
throw new Error('Unknown operator: ' + selector.operator);
case 'sibling':
return matches(node, selector.right, ancestry) &&
sibling(node, selector.left, ancestry, LEFT_SIDE) ||
Expand All @@ -162,7 +161,6 @@ function matches(node, selector, ancestry) {
});

case 'class':
if(!node.type) return false;
switch(selector.name.toLowerCase()){
case 'statement':
if(node.type.slice(-9) === 'Statement') return true;
Expand Down
5 changes: 3 additions & 2 deletions grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
function strUnescape(s) {
return s.replace(/\\(.)/g, function(match, ch) {
switch(ch) {
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
Expand Down Expand Up @@ -76,7 +75,9 @@ attr
}
number
= a:([0-9]* ".")? b:[0-9]+ {
return { type: 'literal', value: parseFloat((a ? a.join('') : '') + b.join('')) };
// Can use `a.flat().join('')` once supported
var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
return { type: 'literal', value: parseFloat(leadingDecimals + b.join('')) };
}
path = i:identifierName { return { type: 'literal', value: i }; }
type = "type(" _ t:[^ )]+ _ ")" { return { type: 'type', value: t.join('') }; }
Expand Down
26 changes: 22 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "esquery",
"preferGlobal": false,
"version": "1.1.0",
"author": "Joel Feenstra <[email protected]>",
"contributors": [],
Expand All @@ -13,11 +12,27 @@
"license.txt",
"README.md"
],
"nyc": {
"branches": 100,
"lines": 100,
"functions": 100,
"statements": 100,
"reporter": [
"html",
"text"
],
"exclude": [
"parser.js",
"dist",
"tests"
]
},
"scripts": {
"build-parser": "rm parser.js && pegjs --cache --format umd -o \"parser.js\" \"grammar.pegjs\"",
"build-browser-only": "rollup -c",
"build-browser": "npm run build-parser && npm run build-browser-only",
"test": "jstestr path=tests",
"mocha": "mocha --require chai/register-assert --require esm tests",
"test": "nyc npm run mocha",
"lint": "eslint ."
},
"repository": {
Expand All @@ -36,16 +51,19 @@
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"chai": "^4.2.0",
"eslint": "^6.8.0",
"esm": "^3.2.25",
"esprima": "~4.0.1",
"jstestr": ">=0.4",
"mocha": "^7.1.0",
"nyc": "^15.0.0",
"pegjs": "~0.10.0",
"rollup": "^1.32.0",
"rollup-plugin-terser": "^5.2.0"
},
"license": "BSD-3-Clause",
"engines": {
"node": ">=4.0"
"node": ">=8.0"
},
"dependencies": {
"estraverse": "^4.3.0"
Expand Down
5 changes: 3 additions & 2 deletions parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

213 changes: 103 additions & 110 deletions tests/fixtures/allClasses.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,112 @@
// eslint-disable-next-line no-unused-vars
define(["esprima"], function (esprima) {

// return esprima.parse("function a(){ [a] = () => 0; new.target; `test`; `hello,${name}`; }");

return {
"type": "Program",
"body": [
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "a"
},
"params": [],
"defaults": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "ArrayPattern",
"elements": [
{
"type": "Identifier",
"name": "a"
}
]
export default {
"type": "Program",
"body": [
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "a"
},
"params": [],
"defaults": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "ArrayPattern",
"elements": [
{
"type": "Identifier",
"name": "a"
}
]
},
"right": {
"type": "ArrowFunctionExpression",
"params": [],
"defaults": [],
"rest": null,
"body": {
"type": "Literal",
"value": 0,
"raw": "0"
},
"right": {
"type": "ArrowFunctionExpression",
"params": [],
"defaults": [],
"rest": null,
"body": {
"type": "Literal",
"value": 0,
"raw": "0"
},
"generator": false,
"expression": false
}
"generator": false,
"expression": false
}
}
},
{
"type": "ExpressionStatement",
"expression": {
"type": "MetaProperty",
"meta": {
"type": "Identifier",
"name": "new",
},
{
"type": "ExpressionStatement",
"expression": {
"type": "MetaProperty",
"meta": {
"type": "Identifier",
"name": "new",
},
"property": {
"type": "Identifier",
"name": "target",
},
},
"property": {
"type": "Identifier",
"name": "target",
},
{
"type": "ExpressionStatement",
"expression": {
},
},
{
"type": "ExpressionStatement",
"expression": {
"type": "TemplateLiteral",
"quasis": [
{
"type": "TemplateElement",
"value": {
"raw": "test",
"cooked": "test"
},
"tail": true,
}
],
"expressions": [],
},
},
{
"type": "ExpressionStatement",
"expression": {
"type": "TemplateLiteral",
"quasis": [
{
"type": "TemplateElement",
"value": {
"raw": "test",
"cooked": "test"
{
"type": "TemplateElement",
"value": {
"raw": "hello,",
"cooked": "hello,"
},
"tail": false,
},
"tail": true,
}
{
"type": "TemplateElement",
"value": {
"raw": "",
"cooked": ""
},
"tail": true,
}
],
"expressions": [
{
"type": "Identifier",
"name": "name",
}
],
"expressions": [],
},
},
{
"type": "ExpressionStatement",
"expression": {
"type": "TemplateLiteral",
"quasis": [
{
"type": "TemplateElement",
"value": {
"raw": "hello,",
"cooked": "hello,"
},
"tail": false,
},
{
"type": "TemplateElement",
"value": {
"raw": "",
"cooked": ""
},
"tail": true,
}
],
"expressions": [
{
"type": "Identifier",
"name": "name",
}
],
},
}
]
},
"rest": null,
"generator": false,
"expression": false
}
]
};

});
}
]
},
"rest": null,
"generator": false,
"expression": false
}
]
};
10 changes: 5 additions & 5 deletions tests/fixtures/bigArray.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
define(["esprima"], function (esprima) {
import * as esprima from "esprima";

return esprima.parse(
'[1, 2, 3, foo, bar, 4, 5, baz, qux, 6]'
);
var parsed = esprima.parse(
'[1, 2, 3, foo, bar, 4, 5, baz, qux, 6]'
);

});
export default parsed;
Loading

0 comments on commit 176a6b1

Please sign in to comment.