Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-boats-slide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-labs/analyzer': patch
---

Adjust attribute source locations in template parser
15 changes: 14 additions & 1 deletion packages/labs/analyzer/src/lib/lit/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,14 @@ export const parseLitTemplate = (

if (node.attrs.length > 0) {
for (const attr of node.attrs) {
// TODO (justinfagnani): adjust attribute locations
const attrSourceLocation =
node.sourceCodeLocation?.attrs?.[attr.name];

if (attrSourceLocation !== undefined) {
attrSourceLocation.startLine += lineAdjust;
attrSourceLocation.startCol += colAdjust;
attrSourceLocation.startOffset += offsetAdjust;
}

if (attr.name.startsWith(marker)) {
// An element binding, like <div ${}>
Expand Down Expand Up @@ -515,6 +522,12 @@ export const parseLitTemplate = (
);
spanIndex += strings.length - 1;
}

if (attrSourceLocation !== undefined) {
attrSourceLocation.endLine += lineAdjust;
attrSourceLocation.endCol += colAdjust;
attrSourceLocation.endOffset += offsetAdjust;
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/labs/analyzer/src/test/server/lit/template_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,27 @@ const assertTemplateNodeText = (
assert.equal(elementTextFromLinesAndCols, expected);
};

const assertAttributeText = (
node: Element,
attrName: string,
templateExpression: ts.TaggedTemplateExpression,
expected: string
) => {
// Trim off the leading and trailing backticks
const templateText = templateExpression.template.getFullText().slice(1, -1);
assert.ok(node.sourceCodeLocation?.attrs);
const sourceLocation = node.sourceCodeLocation?.attrs?.[attrName];
assert.ok(
sourceLocation,
`source location for attribute not found: ${attrName}`
);
const {startOffset, endOffset} = sourceLocation;

// Check that the offsets are correct:
const textFromOffsets = templateText.substring(startOffset, endOffset);
assert.equal(textFromOffsets, expected);
};

suite('parseLitTemplate', () => {
const testFilePath = path.resolve(testFilesDir, 'hello.ts');
const {sourceFile, checker} = getTestSourceFile(testFilePath);
Expand Down Expand Up @@ -325,6 +346,13 @@ suite('parseLitTemplate', () => {
`<div class="\${'a'}"><span>Hello, world!</span></div>`
);

assertAttributeText(
div as Element,
'class$lit$',
templateExpression,
`class="\${'a'}"`
);

const span = (div as Element).childNodes[0];
assert.equal(span.nodeName, 'span');
assertTemplateNodeText(
Expand Down
Loading