Skip to content

Commit

Permalink
Add eslint setup (#80)
Browse files Browse the repository at this point in the history
* Add .npmrc to disable package-lock.json so npm i doesn't create it at every opportunity

* Add .editorconfig reflecting the current formatting

* Add eslint

* Add lint npm script

* Run lint with node.js 12 on CI via a build matrix

* Mark define (AMD) as a global

* Add breaks to prevent switch fallthrough on unknown selectors

Should probably also add default cases to prevent silently ignoring
unknown selectors, but this is at least better than falling through.

* Remove unused function parameter

* Add .eslintignore to avoid checking /parser.js (generated)

* Remove unused dependencies in test files

* Tidy up .eslintrc.json

* Disable the no-unused-vars rule in a file where having esprima around is useful

* Fix accidental global vars

* Also build with node.js 4, 6, 8, 10, 12 and latest

#74 (comment)

* OCD: Put node.js versions in chronological order

* Don't run with the latest node

#80 (comment)

* Throw on unknown selector value type

#80 (comment)
  • Loading branch information
papandreou committed Feb 27, 2020
1 parent 1bf9280 commit bf0ab45
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 20 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated file:
/parser.js
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"define": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {}
}
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock = false
17 changes: 14 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
sudo: false
language: node_js
node_js:
- "iojs"
- "0.12"
- "0.11"
- "0.10"
- "0.11"
- "0.12"
- "iojs"
- 4
- 6
- 8
- 10
- 12

matrix:
include:
- name: Lint
node_js: 12
script: npm run lint
7 changes: 4 additions & 3 deletions esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,26 @@
case 'literal': return '' + selector.value.value === '' + p;
case 'type': return selector.value.value === typeof p;
}
throw new Error("Unknown selector value type: " + selector.value.type);
case '!=':
switch (selector.value.type) {
case 'regexp': return !selector.value.value.test(p);
case 'literal': return '' + selector.value.value !== '' + p;
case 'type': return selector.value.value !== typeof p;
}
throw new Error("Unknown selector value type: " + selector.value.type);
case '<=': return p <= selector.value.value;
case '<': return p < selector.value.value;
case '>': return p > selector.value.value;
case '>=': return p >= selector.value.value;
}

break;
case 'sibling':
return matches(node, selector.right, ancestry) &&
sibling(node, selector.left, ancestry, LEFT_SIDE) ||
selector.left.subject &&
matches(node, selector.left, ancestry) &&
sibling(node, selector.right, ancestry, RIGHT_SIDE);

case 'adjacent':
return matches(node, selector.right, ancestry) &&
adjacent(node, selector.left, ancestry, LEFT_SIDE) ||
Expand All @@ -156,7 +157,7 @@

case 'nth-child':
return matches(node, selector.right, ancestry) &&
nthChild(node, ancestry, function (length) {
nthChild(node, ancestry, function () {
return selector.index.value - 1;
});

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"README.md"
],
"scripts": {
"test": "node node_modules/jstestr/bin/jstestr.js path=tests"
"test": "node node_modules/jstestr/bin/jstestr.js path=tests",
"lint": "eslint ."
},
"repository": {
"type": "git",
Expand All @@ -25,10 +26,11 @@
"query"
],
"devDependencies": {
"jstestr": ">=0.4",
"pegjs": "~0.7.0",
"commonjs-everywhere": "~0.9.4",
"esprima": "~1.1.1"
"eslint": "^6.8.0",
"esprima": "~1.1.1",
"jstestr": ">=0.4",
"pegjs": "~0.7.0"
},
"license": "BSD-3-Clause",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/allClasses.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line no-unused-vars
define(["esprima"], function (esprima) {

// return esprima.parse("function a(){ [a] = () => 0; new.target; `test`; `hello,${name}`; }");
Expand Down
18 changes: 18 additions & 0 deletions tests/queryAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ define([
conditional.body[1].test.left.right,
conditional.body[1].alternate.test
], matches);
},

"unknown type": function () {
assert.doesThrow(Error, function () {
esquery.match(forLoop, {
type: 'attribute',
name: 'foo',
operator: '=',
value: { type: 'foobar' } });
});

assert.doesThrow(Error, function () {
esquery.match(forLoop, {
type: 'attribute',
name: 'foo',
operator: '!=',
value: { type: 'foobar' } });
});
}
});
});
5 changes: 2 additions & 3 deletions tests/queryComplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ define([
"./fixtures/conditional",
"./fixtures/forLoop",
"./fixtures/simpleFunction",
"./fixtures/simpleProgram",
"./fixtures/nestedFunctions"
], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram, nestedFunctions) {
"./fixtures/simpleProgram"
], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram) {

test.defineSuite("Complex selector query", {

Expand Down
7 changes: 2 additions & 5 deletions tests/queryCompound.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ define([
"esquery",
"jstestr/assert",
"jstestr/test",
"./fixtures/conditional",
"./fixtures/forLoop",
"./fixtures/simpleFunction",
"./fixtures/simpleProgram"
], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram) {
"./fixtures/conditional"
], function (esquery, assert, test, conditional) {

test.defineSuite("Compound query", {

Expand Down
2 changes: 1 addition & 1 deletion tests/queryNot.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define([
kind: "var"
}]
};
matches = esquery(program, ":not([value=1])");
var matches = esquery(program, ":not([value=1])");

assert.contains([
program,
Expand Down
2 changes: 1 addition & 1 deletion tests/queryWildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define([
kind: "var"
}]
};
matches = esquery(program, "*");
var matches = esquery(program, "*");

assert.contains([
program,
Expand Down

0 comments on commit bf0ab45

Please sign in to comment.