chore(web): add and apply eslint formatting
This commit is contained in:
parent
5f63074057
commit
96a440af35
11 changed files with 622 additions and 569 deletions
|
|
@ -1,11 +1,11 @@
|
|||
const {assert} = require('chai');
|
||||
let Parser, JavaScript;
|
||||
let Parser; let JavaScript;
|
||||
|
||||
describe("Tree", () => {
|
||||
let parser, tree;
|
||||
describe('Tree', () => {
|
||||
let parser; let tree;
|
||||
|
||||
before(async () =>
|
||||
({Parser, JavaScript} = await require('./helper'))
|
||||
({Parser, JavaScript} = await require('./helper')),
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
|
|
@ -18,14 +18,14 @@ describe("Tree", () => {
|
|||
});
|
||||
|
||||
describe('.edit', () => {
|
||||
let input, edit
|
||||
let input; let edit;
|
||||
|
||||
it('updates the positions of nodes', () => {
|
||||
input = 'abc + cde';
|
||||
tree = parser.parse(input);
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (identifier) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (identifier) right: (identifier))))',
|
||||
);
|
||||
|
||||
let sumNode = tree.rootNode.firstChild.firstChild;
|
||||
|
|
@ -51,17 +51,17 @@ describe("Tree", () => {
|
|||
tree = parser.parse(input, tree);
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))',
|
||||
);
|
||||
});
|
||||
|
||||
it("handles non-ascii characters", () => {
|
||||
it('handles non-ascii characters', () => {
|
||||
input = 'αβδ + cde';
|
||||
|
||||
tree = parser.parse(input);
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (identifier) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (identifier) right: (identifier))))',
|
||||
);
|
||||
|
||||
let variableNode = tree.rootNode.firstChild.firstChild.lastChild;
|
||||
|
|
@ -76,65 +76,65 @@ describe("Tree", () => {
|
|||
tree = parser.parse(input, tree);
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".getChangedRanges(previous)", () => {
|
||||
it("reports the ranges of text whose syntactic meaning has changed", () => {
|
||||
let sourceCode = "abcdefg + hij";
|
||||
describe('.getChangedRanges(previous)', () => {
|
||||
it('reports the ranges of text whose syntactic meaning has changed', () => {
|
||||
let sourceCode = 'abcdefg + hij';
|
||||
tree = parser.parse(sourceCode);
|
||||
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (identifier) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (identifier) right: (identifier))))',
|
||||
);
|
||||
|
||||
sourceCode = "abc + defg + hij";
|
||||
sourceCode = 'abc + defg + hij';
|
||||
tree.edit({
|
||||
startIndex: 2,
|
||||
oldEndIndex: 2,
|
||||
newEndIndex: 5,
|
||||
startPosition: { row: 0, column: 2 },
|
||||
oldEndPosition: { row: 0, column: 2 },
|
||||
newEndPosition: { row: 0, column: 5 }
|
||||
startPosition: {row: 0, column: 2},
|
||||
oldEndPosition: {row: 0, column: 2},
|
||||
newEndPosition: {row: 0, column: 5},
|
||||
});
|
||||
|
||||
const tree2 = parser.parse(sourceCode, tree);
|
||||
assert.equal(
|
||||
tree2.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))))',
|
||||
);
|
||||
|
||||
const ranges = tree.getChangedRanges(tree2);
|
||||
assert.deepEqual(ranges, [
|
||||
{
|
||||
startIndex: 0,
|
||||
endIndex: "abc + defg".length,
|
||||
startPosition: { row: 0, column: 0 },
|
||||
endPosition: { row: 0, column: "abc + defg".length }
|
||||
}
|
||||
endIndex: 'abc + defg'.length,
|
||||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 'abc + defg'.length},
|
||||
},
|
||||
]);
|
||||
|
||||
tree2.delete();
|
||||
});
|
||||
|
||||
it('throws an exception if the argument is not a tree', () => {
|
||||
tree = parser.parse("abcdefg + hij");
|
||||
tree = parser.parse('abcdefg + hij');
|
||||
|
||||
assert.throws(() => {
|
||||
tree.getChangedRanges({});
|
||||
}, /Argument must be a Tree/);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe(".walk()", () => {
|
||||
let cursor
|
||||
describe('.walk()', () => {
|
||||
let cursor;
|
||||
|
||||
afterEach(() => {
|
||||
cursor.delete();
|
||||
})
|
||||
});
|
||||
|
||||
it('returns a cursor that can be used to walk the tree', () => {
|
||||
tree = parser.parse('a * b + c / d');
|
||||
|
|
@ -146,7 +146,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 13},
|
||||
startIndex: 0,
|
||||
endIndex: 13
|
||||
endIndex: 13,
|
||||
});
|
||||
|
||||
assert(cursor.gotoFirstChild());
|
||||
|
|
@ -156,7 +156,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 13},
|
||||
startIndex: 0,
|
||||
endIndex: 13
|
||||
endIndex: 13,
|
||||
});
|
||||
|
||||
assert(cursor.gotoFirstChild());
|
||||
|
|
@ -166,7 +166,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 13},
|
||||
startIndex: 0,
|
||||
endIndex: 13
|
||||
endIndex: 13,
|
||||
});
|
||||
|
||||
assert(cursor.gotoFirstChild());
|
||||
|
|
@ -176,7 +176,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 0,
|
||||
endIndex: 5
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
assert(cursor.gotoFirstChild());
|
||||
|
|
@ -187,10 +187,10 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 1},
|
||||
startIndex: 0,
|
||||
endIndex: 1
|
||||
endIndex: 1,
|
||||
});
|
||||
|
||||
assert(!cursor.gotoFirstChild())
|
||||
assert(!cursor.gotoFirstChild());
|
||||
assert(cursor.gotoNextSibling());
|
||||
assert.equal(cursor.nodeText, '*');
|
||||
assertCursorState(cursor, {
|
||||
|
|
@ -199,7 +199,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 2},
|
||||
endPosition: {row: 0, column: 3},
|
||||
startIndex: 2,
|
||||
endIndex: 3
|
||||
endIndex: 3,
|
||||
});
|
||||
|
||||
assert(cursor.gotoNextSibling());
|
||||
|
|
@ -210,7 +210,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 4},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 4,
|
||||
endIndex: 5
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
assert(!cursor.gotoNextSibling());
|
||||
|
|
@ -221,7 +221,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 0,
|
||||
endIndex: 5
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
assert(cursor.gotoNextSibling());
|
||||
|
|
@ -231,7 +231,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 6},
|
||||
endPosition: {row: 0, column: 7},
|
||||
startIndex: 6,
|
||||
endIndex: 7
|
||||
endIndex: 7,
|
||||
});
|
||||
|
||||
assert(cursor.gotoNextSibling());
|
||||
|
|
@ -241,7 +241,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 8},
|
||||
endPosition: {row: 0, column: 13},
|
||||
startIndex: 8,
|
||||
endIndex: 13
|
||||
endIndex: 13,
|
||||
});
|
||||
|
||||
{
|
||||
|
|
@ -255,7 +255,7 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 6},
|
||||
endPosition: {row: 0, column: 7},
|
||||
startIndex: 6,
|
||||
endIndex: 7
|
||||
endIndex: 7,
|
||||
});
|
||||
|
||||
assert(copy.gotoPreviousSibling());
|
||||
|
|
@ -265,28 +265,28 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 0,
|
||||
endIndex: 5
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
assert(copy.gotoLastChild());
|
||||
assertCursorState(copy, {
|
||||
nodeType: "identifier",
|
||||
nodeType: 'identifier',
|
||||
nodeIsNamed: true,
|
||||
startPosition: {row: 0, column: 4},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 4,
|
||||
endIndex: 5
|
||||
})
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
assert(copy.gotoParent());
|
||||
assert(copy.gotoParent());
|
||||
assert.equal(copy.nodeType, 'binary_expression')
|
||||
assert.equal(copy.nodeType, 'binary_expression');
|
||||
assert(copy.gotoParent());
|
||||
assert.equal(copy.nodeType, 'expression_statement')
|
||||
assert.equal(copy.nodeType, 'expression_statement');
|
||||
assert(copy.gotoParent());
|
||||
assert.equal(copy.nodeType, 'program')
|
||||
assert.equal(copy.nodeType, 'program');
|
||||
assert(!copy.gotoParent());
|
||||
}
|
||||
}
|
||||
|
||||
// const childIndex = cursor.gotoFirstChildForIndex(12);
|
||||
// assertCursorState(cursor, {
|
||||
|
|
@ -302,11 +302,11 @@ describe("Tree", () => {
|
|||
// assert(cursor.gotoParent());
|
||||
|
||||
assert(cursor.gotoParent());
|
||||
assert.equal(cursor.nodeType, 'binary_expression')
|
||||
assert.equal(cursor.nodeType, 'binary_expression');
|
||||
assert(cursor.gotoParent());
|
||||
assert.equal(cursor.nodeType, 'expression_statement')
|
||||
assert.equal(cursor.nodeType, 'expression_statement');
|
||||
assert(cursor.gotoParent());
|
||||
assert.equal(cursor.nodeType, 'program')
|
||||
assert.equal(cursor.nodeType, 'program');
|
||||
assert(!cursor.gotoParent());
|
||||
});
|
||||
|
||||
|
|
@ -350,31 +350,31 @@ describe("Tree", () => {
|
|||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 5},
|
||||
startIndex: 0,
|
||||
endIndex: 5
|
||||
endIndex: 5,
|
||||
});
|
||||
|
||||
cursor.gotoFirstChild()
|
||||
cursor.gotoFirstChild();
|
||||
assertCursorState(cursor, {
|
||||
nodeType: 'identifier',
|
||||
nodeIsNamed: true,
|
||||
startPosition: {row: 0, column: 0},
|
||||
endPosition: {row: 0, column: 1},
|
||||
startIndex: 0,
|
||||
endIndex: 1
|
||||
endIndex: 1,
|
||||
});
|
||||
|
||||
assert(cursor.gotoParent());
|
||||
assert(!cursor.gotoParent());
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe(".copy", () => {
|
||||
it("creates another tree that remains stable if the original tree is edited", () => {
|
||||
describe('.copy', () => {
|
||||
it('creates another tree that remains stable if the original tree is edited', () => {
|
||||
input = 'abc + cde';
|
||||
tree = parser.parse(input);
|
||||
assert.equal(
|
||||
tree.rootNode.toString(),
|
||||
"(program (expression_statement (binary_expression left: (identifier) right: (identifier))))"
|
||||
'(program (expression_statement (binary_expression left: (identifier) right: (identifier))))',
|
||||
);
|
||||
|
||||
const tree2 = tree.copy();
|
||||
|
|
@ -386,10 +386,10 @@ describe("Tree", () => {
|
|||
const leftNode2 = tree2.rootNode.firstChild.firstChild.firstChild;
|
||||
const rightNode = tree.rootNode.firstChild.firstChild.lastChild;
|
||||
const rightNode2 = tree2.rootNode.firstChild.firstChild.lastChild;
|
||||
assert.equal(leftNode.endIndex, 6)
|
||||
assert.equal(leftNode2.endIndex, 3)
|
||||
assert.equal(rightNode.startIndex, 9)
|
||||
assert.equal(rightNode2.startIndex, 6)
|
||||
assert.equal(leftNode.endIndex, 6);
|
||||
assert.equal(leftNode2.endIndex, 3);
|
||||
assert.equal(rightNode.startIndex, 9);
|
||||
assert.equal(rightNode2.startIndex, 6);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -406,16 +406,16 @@ function spliceInput(input, startIndex, lengthRemoved, newText) {
|
|||
{
|
||||
startIndex, startPosition,
|
||||
oldEndIndex, oldEndPosition,
|
||||
newEndIndex, newEndPosition
|
||||
}
|
||||
newEndIndex, newEndPosition,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function getExtent(text) {
|
||||
let row = 0
|
||||
let row = 0;
|
||||
let index;
|
||||
for (index = 0; index != -1; index = text.indexOf('\n', index)) {
|
||||
index++
|
||||
index++;
|
||||
row++;
|
||||
}
|
||||
return {row, column: text.length - index};
|
||||
|
|
@ -429,7 +429,7 @@ function assertCursorState(cursor, params) {
|
|||
assert.deepEqual(cursor.startIndex, params.startIndex);
|
||||
assert.deepEqual(cursor.endIndex, params.endIndex);
|
||||
|
||||
const node = cursor.currentNode()
|
||||
const node = cursor.currentNode();
|
||||
assert.equal(node.type, params.nodeType);
|
||||
assert.equal(node.isNamed(), params.nodeIsNamed);
|
||||
assert.deepEqual(node.startPosition, params.startPosition);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue