Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update location info test to conform to newly constrained location type #457

Open
43081j opened this issue Mar 14, 2022 · 1 comment
Open
Labels

Comments

@43081j
Copy link
Collaborator

43081j commented Mar 14, 2022

it('Updating node source code location (GH-314)', () => {
const sourceCodeLocationSetter = {
setNodeSourceCodeLocation(node: any, location: any): void {
node.sourceCodeLocation =
location === null
? null
: {
start: {
line: location.startLine,
column: location.startCol,
offset: location.startOffset,
},
end: {
line: location.endLine,
column: location.endCol,
offset: location.endOffset,
},
};
},
updateNodeSourceCodeLocation(node: any, endLocation: any): void {
node.sourceCodeLocation = {
start: node.sourceCodeLocation.start,
end: {
line: endLocation.endLine,
column: endLocation.endCol,
offset: endLocation.endOffset,
},
};
},
};
const treeAdapter = { ...treeAdapters.default, ...sourceCodeLocationSetter };
const document = parse5.parse('<!doctype><body>Testing location</body>', {
treeAdapter,
sourceCodeLocationInfo: true,
});
const [doctype, html] = document.childNodes;
assert.ok(treeAdapters.default.isElementNode(html));
const [head, body] = html.childNodes;
assert.ok(treeAdapters.default.isElementNode(body));
const [text] = body.childNodes;
assert.deepEqual(doctype.sourceCodeLocation, {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 11, offset: 10 },
});
assert.strictEqual(html.sourceCodeLocation, null);
assert.strictEqual(head.sourceCodeLocation, null);
assert.deepEqual(body.sourceCodeLocation, {
start: { line: 1, column: 11, offset: 10 },
end: { line: 1, column: 40, offset: 39 },
});
assert.deepEqual(text.sourceCodeLocation, {
start: { line: 1, column: 17, offset: 16 },
end: { line: 1, column: 33, offset: 32 },
});
});

This test currently creates its own location structure and asserts that it ends up in the resulting node.

It currently passes because of its parameters being any. However, if we strongly type those, the test fails to compile.

This is because during the TS conversion, we introduced a new constraint: all locations must be a Location (i.e. you can no longer create your own location structure, but could before).

We should probably just update this test to follow the Location type. Though maybe put something in it to identify it actually called our test callback rather than the default (since both would produce the same result at that point...)

This is basically a prerequisite of enforcing the no-explicit-any lint rule.

cc @fb55

@fb55
Copy link
Collaborator

fb55 commented Mar 16, 2022

We should probably just update this test to follow the Location type. Though maybe put something in it to identify it actually called our test callback rather than the default (since both would produce the same result at that point...)

This makes sense to me. The best option I can come up with is to create a class that implements Location, and then do instanceof checks in the assertions.

@fb55 fb55 added the testing label Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants