Skip to content

Commit

Permalink
Rename LitDefinition's target to targets and remove the non-arr…
Browse files Browse the repository at this point in the history
…ay case. (#282)
  • Loading branch information
bicknellr authored Dec 2, 2022
1 parent 075e559 commit 2dfef44
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class LitCssDocumentAnalyzer {

return {
fromRange: documentRangeToSFRange(document, { start, end }),
target: nodes.map(node => ({
targets: nodes.map(node => ({
kind: "node",
node: getNodeIdentifier(node, context.ts) || node
}))
Expand All @@ -85,10 +85,12 @@ export class LitCssDocumentAnalyzer {

return {
fromRange: documentRangeToSFRange(document, { start, end }),
target: {
kind: "node",
node: getNodeIdentifier(node, context.ts) || node
}
targets: [
{
kind: "node",
node: getNodeIdentifier(node, context.ts) || node
}
]
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ export function definitionForHtmlAttr(htmlAttr: HtmlNodeAttr, { htmlStore, ts }:

return {
fromRange: rangeFromHtmlNodeAttr(htmlAttr),
target: {
kind: "node",
node: getNodeIdentifier(node, ts) || node,
name: target.name
}
targets: [
{
kind: "node",
node: getNodeIdentifier(node, ts) || node,
name: target.name
}
]
};
} else if (isHtmlEvent(target) && target.declaration != null) {
const node = target.declaration.node;

return {
fromRange: rangeFromHtmlNodeAttr(htmlAttr),
target: {
kind: "node",
node: getNodeIdentifier(node, ts) || node,
name: target.name
}
targets: [
{
kind: "node",
node: getNodeIdentifier(node, ts) || node,
name: target.name
}
]
};
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export function definitionForHtmlNode(htmlNode: HtmlNode, { htmlStore, ts }: Lit

return {
fromRange: rangeFromHtmlNode(htmlNode),
target: {
kind: "node",
node: getNodeIdentifier(node, ts) || node
}
targets: [
{
kind: "node",
node: getNodeIdentifier(node, ts) || node
}
]
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export type LitDefinitionTarget = LitDefinitionTargetNode | LitDefinitionTargetR

export interface LitDefinition {
fromRange: SourceFileRange;
target: LitDefinitionTarget[] | LitDefinitionTarget;
targets: LitDefinitionTarget[];
}
20 changes: 8 additions & 12 deletions packages/lit-analyzer/src/test/indexer/index-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const assertIdentifiesClass = ({

/**
* Asserts that `entry` is a `HtmlNodeIndexEntry` that describes an element with
* tag name `tagName` that is defined by a class named `className` in
* tag name `tagName` that is defined by a single class named `className` in
* `sourceFile`.
*/
const assertEntryTargetsClass = ({
Expand All @@ -128,12 +128,10 @@ const assertEntryTargetsClass = ({
t.is(entryNode.kind, HtmlNodeKind.NODE, "The entry should not originate from an `<svg>` or `<style>`.");
t.is(entryNode.tagName, tagName, `The origin element is not a \`<${tagName}>\`.`);

const { target } = entry.definition;
if (Array.isArray(target)) {
// TODO: What about single item arrays?
throw new Error("The definition should have a single target.");
}
const { targets } = entry.definition;
t.is(targets.length, 1, "The definition should have a single target.");

const [target] = targets;
if (target.kind !== "node") {
throw new Error("The definition target should be a `LitDefinitionTargetNode`.");
}
Expand Down Expand Up @@ -301,7 +299,7 @@ tsTest("Attribute references are not created for attributes that don't map to kn

/**
* Asserts that `entry` is a `HtmlNodeAttrIndexEntry` with name `name` and kind
* `kind` that targets a `LitDefinitionTargetNode`.
* `kind` that has a single target `LitDefinitionTargetNode`.
*/
const assertIsAttrRefAndGetTarget = ({
t,
Expand All @@ -322,12 +320,10 @@ const assertIsAttrRefAndGetTarget = ({
t.is(entryAttr.name, name, `The attribute name should be \`${name}\`.`);
t.is(entryAttr.kind, kind, `The attribute kind should be \`${kind}\`.`);

const { target } = entry.definition;
if (Array.isArray(target)) {
// TODO: What about single item arrays?
throw new Error("The definition should have a single target.");
}
const { targets } = entry.definition;
t.is(targets.length, 1, "The definition should have a single target.");

const [target] = targets;
if (target.kind !== "node") {
throw new Error("The definition target should be a `LitDefinitionTargetNode`.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { translateRange } from "./translate-range.js";

export function translateDefinition(definition: LitDefinition): DefinitionInfoAndBoundSpan {
return {
definitions: [...(Array.isArray(definition.target) ? definition.target : [definition.target])].map(translateDefinitionInfo),
definitions: definition.targets.map(translateDefinitionInfo),
textSpan: translateRange(definition.fromRange)
};
}
Expand Down

0 comments on commit 2dfef44

Please sign in to comment.