Skip to content

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

@43081j

Description

@43081j

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions