Skip to content

Commit 4e9d584

Browse files
authored
Merge pull request glayzzle#462 from czosel/prettier-tests-2
feat: lint tests as well
2 parents 2fb4c7e + c5845c4 commit 4e9d584

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1450
-1226
lines changed

.eslintrc.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
root: true,
33
parserOptions: {
4-
ecmaVersion: 2017,
4+
ecmaVersion: 2018,
55
sourceType: "module"
66
},
77
plugins: ["prettier"],
@@ -17,5 +17,13 @@ module.exports = {
1717
"prefer-const": "error",
1818
"no-var": "error",
1919
"prettier/prettier": "error"
20-
}
20+
},
21+
overrides: [
22+
{
23+
files: ["test/**/*.js"],
24+
rules: {
25+
"no-console": "off"
26+
}
27+
}
28+
]
2129
};

babel.config.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module.exports = function (api) {
2-
api.cache(true);
3-
4-
const presets = ['@babel/preset-env'];
5-
const plugins = [];
6-
7-
return {
8-
presets,
9-
plugins
10-
};
11-
}
1+
module.exports = function(api) {
2+
api.cache(true);
3+
4+
const presets = ["@babel/preset-env"];
5+
const plugins = [];
6+
7+
return {
8+
presets,
9+
plugins
10+
};
11+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"LICENSE"
1212
],
1313
"scripts": {
14-
"lint": "eslint 'src/**/*.js' --fix",
14+
"lint": "eslint . --fix",
1515
"test": "jest",
1616
"prehusky": "npm run lint",
1717
"husky": "npm run test",

src/parser/array.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ module.exports = {
8484
value = this.read_variable(true, false);
8585
} else if (this.token === this.tok.T_ELLIPSIS && this.php74) {
8686
this.next();
87-
if (this.token === '&') {
88-
this.error()
87+
if (this.token === "&") {
88+
this.error();
8989
}
9090
unpack = true;
9191
value = this.read_expr();

test/ast.test.js

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
const parser = require("./main");
22

33
describe("Test AST class (edge cases)", function() {
4-
it("test source without location", function() {
5-
const test = parser.create({
6-
ast: {
7-
withPositions: false,
8-
withSource: true
9-
}
10-
});
11-
const ast = test.parseEval('echo $foo;');
12-
const echo = ast.children[0];
13-
expect(echo.loc.source).toBe("echo $foo;");
14-
expect(echo.loc.start).not.toBeNull();
15-
expect(echo.loc.end).not.toBeNull();
16-
17-
const variable = echo.expressions[0];
18-
expect(variable.loc.source).toBe("$foo");
4+
it("test source without location", function() {
5+
const test = parser.create({
6+
ast: {
7+
withPositions: false,
8+
withSource: true
9+
}
1910
});
20-
it("test undefined node", function() {
21-
const test = parser.create();
22-
expect(() => {
23-
const node = test.parser.node('foo');
24-
node();
25-
}).toThrow(/foo/);
26-
});
27-
it("test debug mode", function() {
28-
const test = parser.create({
29-
parser: {
30-
debug: true
31-
}
32-
});
33-
test.parseEval('1 + 1;');
11+
const ast = test.parseEval("echo $foo;");
12+
const echo = ast.children[0];
13+
expect(echo.loc.source).toBe("echo $foo;");
14+
expect(echo.loc.start).not.toBeNull();
15+
expect(echo.loc.end).not.toBeNull();
16+
17+
const variable = echo.expressions[0];
18+
expect(variable.loc.source).toBe("$foo");
19+
});
20+
it("test undefined node", function() {
21+
const test = parser.create();
22+
expect(() => {
23+
const node = test.parser.node("foo");
24+
node();
25+
}).toThrow(/foo/);
26+
});
27+
it("test debug mode", function() {
28+
const test = parser.create({
29+
parser: {
30+
debug: true
31+
}
3432
});
35-
it("test debug mode / errors", function() {
36-
const test = parser.create({
37-
parser: {
38-
debug: true
39-
}
40-
});
41-
expect(() => {
42-
test.parseEval('1 + ;');
43-
}).toThrow();
33+
test.parseEval("1 + 1;");
34+
});
35+
it("test debug mode / errors", function() {
36+
const test = parser.create({
37+
parser: {
38+
debug: true
39+
}
4440
});
45-
46-
});
41+
expect(() => {
42+
test.parseEval("1 + ;");
43+
}).toThrow();
44+
});
45+
});

test/debug.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
]
1515
}
1616
*/
17-
const util = require('util');
17+
const util = require("util");
1818
const parser = require("../src/index");
19-
const ast = parser.parseCode(`
19+
const ast = parser.parseCode(
20+
`
2021
<?php
2122
function iter() {
2223
yield 'ator' => $foo;
2324
yield from iter(50);
2425
}
2526
$a = fn($n) => $n * $factor;
26-
`, {
27+
`,
28+
{
2729
parser: {
28-
debug: true,
30+
debug: true
2931
//extractDoc: true
3032
},
3133
ast: {
@@ -34,6 +36,4 @@ $a = fn($n) => $n * $factor;
3436
}
3537
}
3638
);
37-
console.log(
38-
util.inspect(ast, false, 10, true)
39-
);
39+
console.log(util.inspect(ast, false, 10, true));

test/location.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var parser = require("./main");
1+
const parser = require("./main");
22

33
describe("Test offsets", function() {
44
// init a new parser instance
5-
var test = parser.create({
5+
const test = parser.create({
66
ast: {
77
withPositions: true,
88
withSource: true
@@ -16,9 +16,9 @@ describe("Test offsets", function() {
1616
}
1717
});
1818

19-
var lines = ["// a comment", 'echo "string";', "$a = true;"];
20-
var text = lines.join("\r\n");
21-
var ast = test.parseEval(text);
19+
const lines = ["// a comment", 'echo "string";', "$a = true;"];
20+
const text = lines.join("\r\n");
21+
const ast = test.parseEval(text);
2222

2323
describe("to program node", function() {
2424
it("test line", function() {
@@ -47,7 +47,7 @@ describe("Test offsets", function() {
4747
it("test offsets", function() {
4848
expect(ast.comments[0].loc.start.offset).toBe(0);
4949
expect(ast.comments[0].loc.end.offset).toBe(lines[0].length + 2);
50-
expect(ast.comments[0].loc.source).toBe(lines[0] + '\r\n');
50+
expect(ast.comments[0].loc.source).toBe(lines[0] + "\r\n");
5151
});
5252
});
5353

@@ -130,11 +130,11 @@ describe("Test offsets", function() {
130130

131131
describe("test block statements", function() {
132132
it("test if", function() {
133-
const ast = test.parseEval('if(true) {}\n//foo\necho $bar;');
133+
const ast = test.parseEval("if(true) {}\n//foo\necho $bar;");
134134
expect(ast.children[0].loc.start.line).toBe(1);
135135
expect(ast.children[0].loc.end.line).toBe(1);
136136
expect(ast.children[1].loc.start.line).toBe(3);
137137
expect(ast.children[1].loc.end.line).toBe(3);
138138
});
139-
})
139+
});
140140
});

test/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @url http://glayzzle.com
66
*/
77

8-
var parser = require("../src/index");
8+
const parser = require("../src/index");
99
// @todo : move here the debug code
1010
// @todo : add an automated token check (with php7)
1111
module.exports = parser;

test/perf.test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var parser = require("./main");
1+
const parser = require("./main");
22

33
describe("Performance tests", function() {
4-
var code = `
4+
const code = `
55
<?php
66
class foo extends bar {
77
const A = 1, B = 2, C = 3;
@@ -16,18 +16,18 @@ describe("Performance tests", function() {
1616
`;
1717

1818
it("tokenizer", function() {
19-
var reader = new parser();
20-
var tokSize = reader.tokenGetAll(code).length;
21-
var hrTime = process.hrtime();
22-
var iter = 100;
23-
var start = hrTime[0] * 1000000 + hrTime[1] / 1000;
24-
for (var i = 0; i < iter; i++) {
19+
const reader = new parser();
20+
const tokSize = reader.tokenGetAll(code).length;
21+
let hrTime = process.hrtime();
22+
const iter = 100;
23+
const start = hrTime[0] * 1000000 + hrTime[1] / 1000;
24+
for (let i = 0; i < iter; i++) {
2525
reader.tokenGetAll(code);
2626
}
2727
hrTime = process.hrtime();
28-
var end = hrTime[0] * 1000000 + hrTime[1] / 1000;
29-
var duration = (end - start) / 1000; // in MS
30-
var speed = 1000 / duration * (tokSize * iter);
28+
const end = hrTime[0] * 1000000 + hrTime[1] / 1000;
29+
const duration = (end - start) / 1000; // in MS
30+
let speed = (1000 / duration) * (tokSize * iter);
3131
// speed.should.be.greaterThan(10e5 * 0.8);
3232

3333
if (speed > 1000) {
@@ -41,7 +41,7 @@ describe("Performance tests", function() {
4141
}
4242
console.log(" + Tokens speed => " + speed + "/sec");
4343

44-
speed = 1000 / duration * (code.length * iter);
44+
speed = (1000 / duration) * (code.length * iter);
4545
// speed.should.be.greaterThan(10e5 * 2 * 0.8);
4646

4747
if (speed > 1024) {
@@ -57,36 +57,36 @@ describe("Performance tests", function() {
5757
});
5858

5959
it("parser", function() {
60-
var reader = new parser();
61-
var nodeSize = 0;
62-
var countNode = function(node) {
60+
const reader = new parser();
61+
let nodeSize = 0;
62+
function countNode(node) {
6363
nodeSize++;
64-
for (var k in node) {
64+
for (const k in node) {
6565
if (node[k]) {
6666
if (node[k].kind) {
6767
countNode(node[k]);
6868
} else if (Array.isArray(node[k])) {
69-
for (var i = 0; i < node[k].length; i++) {
69+
for (let i = 0; i < node[k].length; i++) {
7070
if (node[k][i] && node[k][i].kind) {
7171
countNode(node[k][i]);
7272
}
7373
}
7474
}
7575
}
7676
}
77-
};
77+
}
7878
countNode(reader.parseCode(code));
7979

80-
var hrTime = process.hrtime();
81-
var iter = 100;
82-
var start = hrTime[0] * 1000000 + hrTime[1] / 1000;
83-
for (var i = 0; i < iter; i++) {
80+
let hrTime = process.hrtime();
81+
const iter = 100;
82+
const start = hrTime[0] * 1000000 + hrTime[1] / 1000;
83+
for (let i = 0; i < iter; i++) {
8484
reader.parseCode(code);
8585
}
8686
hrTime = process.hrtime();
87-
var end = hrTime[0] * 1000000 + hrTime[1] / 1000;
88-
var duration = (end - start) / 1000; // in MS
89-
var speed = 1000 / duration * (nodeSize * iter);
87+
const end = hrTime[0] * 1000000 + hrTime[1] / 1000;
88+
const duration = (end - start) / 1000; // in MS
89+
let speed = (1000 / duration) * (nodeSize * iter);
9090
// speed.should.be.greaterThan(10e4);
9191

9292
if (speed > 1000) {
@@ -100,7 +100,7 @@ describe("Performance tests", function() {
100100
}
101101
console.log(" + Nodes speed => " + speed + "/sec");
102102

103-
speed = 1000 / duration * (code.length * iter);
103+
speed = (1000 / duration) * (code.length * iter);
104104
// speed.should.be.greaterThan(10e5 * 1 * 0.8);
105105

106106
if (speed > 1024) {

0 commit comments

Comments
 (0)