Skip to content

Commit

Permalink
Refactor to make it possible to add component level rules
Browse files Browse the repository at this point in the history
  • Loading branch information
runem committed May 6, 2020
1 parent ac08b5c commit 72727bc
Show file tree
Hide file tree
Showing 49 changed files with 650 additions and 176 deletions.
8 changes: 7 additions & 1 deletion dev/src/my-element-1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { LitElement, html, customElement } from "lit-element";
import { customElement, html, LitElement, property } from "lit-element";
import "./my-element-2";

@customElement("my-element")
export class MyElement extends LitElement {
@property({ attribute: "hello" }) test: number | undefined;

@property({ type: String }) test2: number | undefined;

@internalProperty() internal: number | undefined;

render() {
return html`
<my-tsconfig-element size="large"></my-tsconfig-element>
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-analyzer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"ts-simple-type": "~0.3.6",
"vscode-css-languageservice": "4.0.2-next.1",
"vscode-html-languageservice": "2.1.12",
"web-component-analyzer": "~1.0.2"
"web-component-analyzer": "~1.0.3"
},
"devDependencies": {
"rimraf": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ export function getBuiltInHtmlCollection(): HtmlDataCollection {
valueSets: EXTENDED_HTML5_VALUE_MAP
});

// Force all tags to be built in
for (const tag of result.tags) {
tag.builtIn = true;
}

result.tags.push({
attributes: [],
properties: [],
events: [],
slots: [],
tagName: "svg",
description: ""
description: "",
builtIn: true
});

result.tags.push({
builtIn: true,
properties: [],
events: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import noBooleanInAttributeBindingRule from "../rules/no-boolean-in-attribute-bi
import noComplexAttributeBindingRule from "../rules/no-complex-attribute-binding";
import noExpressionlessPropertyBindingRule from "../rules/no-expressionless-property-binding";
import noIncompatibleTypeBindingRule from "../rules/no-incompatible-type-binding";
import noInvalidAttributeName from "../rules/no-invalid-attribute-name";
import noInvalidDirectiveBindingRule from "../rules/no-invalid-directive-binding";
import noInvalidProperty from "../rules/no-invalid-property";
import noInvalidTagName from "../rules/no-invalid-tag-name";
import noMissingImport from "../rules/no-missing-import";
import noNoncallableEventBindingRule from "../rules/no-noncallable-event-binding";
import noNullableAttributeBindingRule from "../rules/no-nullable-attribute-binding";
Expand Down Expand Up @@ -48,7 +51,10 @@ const rules: RuleModule[] = [
noUnknownTagName,
noUnknownAttribute,
noUnknownProperty,
noUnknownEvent
noUnknownEvent,
noInvalidProperty,
noInvalidTagName,
noInvalidAttributeName
];

export class DefaultLitAnalyzerContext implements LitAnalyzerContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CodeActionKind, LitCodeFixAction } from "../../../types/lit-code-fix-ac
import { LitHtmlDiagnostic, LitHtmlDiagnosticKind } from "../../../types/lit-diagnostic";
import { iterableFirst } from "../../../util/iterable-util";

export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document, htmlStore }: LitAnalyzerRequest): LitCodeFix[] {
export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document, file, htmlStore }: LitAnalyzerRequest): LitCodeFix[] {
switch (htmlReport.kind) {
case LitHtmlDiagnosticKind.UNKNOWN_TARGET: {
const fixes: LitCodeFix[] = [];
Expand All @@ -21,10 +21,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: htmlReport.htmlAttr.location.name.start,
end: htmlReport.htmlAttr.location.name.start
},
Expand All @@ -44,11 +44,11 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
// Make a range that includes the modifier.
range: {
document,
document: document!,
start: htmlReport.htmlAttr.location.start,
end: htmlReport.htmlAttr.location.name.end
},
Expand All @@ -74,20 +74,20 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: { document, ...htmlReport.location },
range: htmlReport.location,
newText: htmlReport.suggestedName
}
},
...(endTagRange == null
? []
: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: endTagRange.start + 2,
end: endTagRange.end - 1
},
Expand Down Expand Up @@ -117,10 +117,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: htmlAttr.location.name.start - existingModifierLength,
end: htmlAttr.location.name.start
},
Expand Down Expand Up @@ -157,10 +157,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: htmlReport.htmlNode.location.name.end,
end: htmlReport.htmlNode.location.name.end
},
Expand All @@ -181,10 +181,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
...htmlReport.htmlAttr.location.name
},
newText
Expand All @@ -204,10 +204,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: assignment.location.start + 2, // Offset 2 for '${'
end: assignment.location.end - 1 // Offset 1 for '}'
},
Expand All @@ -229,10 +229,10 @@ export function codeFixesForHtmlReport(htmlReport: LitHtmlDiagnostic, { document
htmlReport,
actions: [
{
kind: CodeActionKind.DOCUMENT_TEXT_CHANGE,
kind: CodeActionKind.TEXT_CHANGE,
change: {
range: {
document,
document: document!,
start: assignment.location.start + 2, // Offset 2 for '${'
end: assignment.location.end - 1 // Offset 1 for '}'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { HtmlDocument } from "../../../parse/document/text-document/html-document/html-document";
import { LitCompletion } from "../../../types/lit-completion";
import { getPositionContextInDocument } from "../../../util/get-position-context-in-document";
import { rangeFromHtmlNodeAttr } from "../../../util/lit-range-util";
import { completionsForHtmlAttrValues } from "./completions-for-html-attr-values";
import { completionsForHtmlAttrs } from "./completions-for-html-attrs";
import { completionsForHtmlNodes } from "./completions-for-html-nodes";
Expand All @@ -24,7 +25,7 @@ export function completionsAtOffset(document: HtmlDocument, offset: number, requ
// Make sure that every entry overwrites the entire attribute name.
return entries.map(entry => ({
...entry,
range: { document: request.document, ...intersectingAttr.location.name }
range: rangeFromHtmlNodeAttr(request.document, intersectingAttr)
}));
} else if (intersectingAttrAssignment != null) {
return completionsForHtmlAttrValues(intersectingAttrAssignment, positionContext, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { documentationForHtmlTag } from "../../../parse/parse-html-data/html-tag";
import { HtmlNode } from "../../../types/html-node/html-node-types";
import { LitCompletion } from "../../../types/lit-completion";
import { isCustomElementTagName, lazy } from "../../../util/general-util";
import { lazy } from "../../../util/general-util";
import { DocumentPositionContext } from "../../../util/get-position-context-in-document";
import { isCustomElementTagName } from "../../../util/is-valid-name";

export function completionsForHtmlNodes(
intersectingClosestNode: HtmlNode | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isHtmlEvent, isHtmlMember } from "../../../parse/parse-html-data/html-t
import { HtmlNodeAttr } from "../../../types/html-node/html-node-attr-types";
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { DefinitionKind, LitDefinition } from "../../../types/lit-definition";
import { rangeFromHtmlNodeAttr } from "../../../util/lit-range-util";

export function definitionForHtmlAttr(htmlAttr: HtmlNodeAttr, { htmlStore, document }: LitAnalyzerRequest): LitDefinition | undefined {
const target = htmlStore.getHtmlAttrTarget(htmlAttr);
Expand All @@ -10,13 +11,13 @@ export function definitionForHtmlAttr(htmlAttr: HtmlNodeAttr, { htmlStore, docum
if (isHtmlMember(target) && target.declaration != null) {
return {
kind: DefinitionKind.MEMBER,
fromRange: { document, ...htmlAttr.location.name },
fromRange: rangeFromHtmlNodeAttr(document, htmlAttr),
target: target.declaration
};
} else if (isHtmlEvent(target) && target.declaration != null) {
return {
kind: DefinitionKind.EVENT,
fromRange: { document, ...htmlAttr.location.name },
fromRange: rangeFromHtmlNodeAttr(document, htmlAttr),
target: target.declaration
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { HtmlNode } from "../../../types/html-node/html-node-types";
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { HtmlNode } from "../../../types/html-node/html-node-types";
import { DefinitionKind, LitDefinition } from "../../../types/lit-definition";
import { rangeFromHtmlNode } from "../../../util/lit-range-util";

export function definitionForHtmlNode(htmlNode: HtmlNode, { document, htmlStore }: LitAnalyzerRequest): LitDefinition | undefined {
const tag = htmlStore.getHtmlTag(htmlNode);
if (tag == null || tag.declaration == null) return undefined;

return {
kind: DefinitionKind.COMPONENT,
fromRange: { document, ...htmlNode.location.name },
fromRange: rangeFromHtmlNode(document, htmlNode),
target: tag.declaration
};
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { descriptionForTarget, targetKindAndTypeText } from "../../../parse/parse-html-data/html-tag";
import { HtmlNodeAttr } from "../../../types/html-node/html-node-attr-types";
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { LitQuickInfo } from "../../../types/lit-quick-info";
import { rangeFromHtmlNodeAttr } from "../../../util/lit-range-util";

export function quickInfoForHtmlAttr(htmlAttr: HtmlNodeAttr, { document, htmlStore }: LitAnalyzerRequest): LitQuickInfo | undefined {
const target = htmlStore.getHtmlAttrTarget(htmlAttr);
if (target == null) return undefined;

return {
range: { document, ...htmlAttr.location.name },
range: rangeFromHtmlNodeAttr(document, htmlAttr),
primaryInfo: targetKindAndTypeText(target, { modifier: htmlAttr.modifier }),
secondaryInfo: descriptionForTarget(target, { markdown: true })
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { documentationForHtmlTag } from "../../../parse/parse-html-data/html-tag";
import { HtmlNode } from "../../../types/html-node/html-node-types";
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { LitQuickInfo } from "../../../types/lit-quick-info";
import { rangeFromHtmlNode } from "../../../util/lit-range-util";

export function quickInfoForHtmlNode(htmlNode: HtmlNode, { htmlStore, document }: LitAnalyzerRequest): LitQuickInfo | undefined {
const htmlTag = htmlStore.getHtmlTag(htmlNode);
if (htmlTag == null) return undefined;

return {
range: { document, ...htmlNode.location.name },
range: rangeFromHtmlNode(document, htmlNode),
primaryInfo: `<${htmlNode.tagName}>`,
secondaryInfo: documentationForHtmlTag(htmlTag, { markdown: true })
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JSDocUnknownTag } from "typescript";
import { LitAnalyzerContext } from "../../../lit-analyzer-context";
import { LitAnalyzerRequest } from "../../../lit-analyzer-context";
import { HtmlDocument } from "../../../parse/document/text-document/html-document/html-document";
import { HtmlNode } from "../../../types/html-node/html-node-types";
import { LitRenameLocation } from "../../../types/lit-rename-location";
import { findChild } from "../../../util/ast-util";
import { iterableFirst } from "../../../util/iterable-util";

export function renameLocationsForTagName(tagName: string, request: LitAnalyzerContext): LitRenameLocation[] {
export function renameLocationsForTagName(tagName: string, request: LitAnalyzerRequest): LitRenameLocation[] {
const locations: LitRenameLocation[] = [];

for (const sourceFile of request.program.getSourceFiles()) {
Expand Down Expand Up @@ -41,7 +41,7 @@ export function renameLocationsForTagName(tagName: string, request: LitAnalyzerC
if (stringLiteralNode != null) {
locations.push({
fileName,
range: { start: stringLiteralNode.getStart() + 1, end: stringLiteralNode.getEnd() - 1 }
range: { file: request.file, start: stringLiteralNode.getStart() + 1, end: stringLiteralNode.getEnd() - 1 }
});
}
} else if (definitionNode.kind === request.ts.SyntaxKind.JSDocTag) {
Expand All @@ -52,7 +52,7 @@ export function renameLocationsForTagName(tagName: string, request: LitAnalyzerC

locations.push({
fileName,
range: { start, end: start + jsDocTagNode.comment.length }
range: { file: request.file, start, end: start + jsDocTagNode.comment.length }
});
}
} else if (request.ts.isInterfaceDeclaration(definitionNode)) {
Expand All @@ -61,7 +61,7 @@ export function renameLocationsForTagName(tagName: string, request: LitAnalyzerC
if (stringLiteralNode != null) {
locations.push({
fileName,
range: { start: stringLiteralNode.getStart() + 1, end: stringLiteralNode.getEnd() - 1 }
range: { file: request.file, start: stringLiteralNode.getStart() + 1, end: stringLiteralNode.getEnd() - 1 }
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/lit-analyzer/src/analyze/lit-analyzer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HtmlData } from "./parse/parse-html-data/html-data-tag";
import { LitDiagnosticSeverity } from "./types/lit-diagnostic";

export type LitAnalyzerRuleName =
| "no-invalid-property"
| "no-unknown-tag-name"
| "no-missing-import"
| "no-unclosed-tag"
Expand Down Expand Up @@ -53,6 +54,7 @@ export type LitAnalyzerRuleSeverity = "off" | "warn" | "warning" | "error" | 0 |
export type LitAnalyzerRules = Partial<Record<LitAnalyzerRuleName, LitAnalyzerRuleSeverity | [LitAnalyzerRuleSeverity]>>;

const DEFAULT_RULES_NOSTRICT: Required<LitAnalyzerRules> = {
"no-invalid-property": "warn",
"no-unknown-tag-name": "off",
"no-missing-import": "off",
"no-unclosed-tag": "warn",
Expand All @@ -77,6 +79,7 @@ const DEFAULT_RULES_NOSTRICT: Required<LitAnalyzerRules> = {
};

const DEFAULT_RULES_STRICT: Required<LitAnalyzerRules> = {
"no-invalid-property": "warn",
"no-unknown-tag-name": "warn",
"no-missing-import": "warn",
"no-unclosed-tag": "error",
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-analyzer/src/analyze/lit-analyzer-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface LitAnalyzerContext {

export interface LitAnalyzerRequest extends LitAnalyzerContext {
file: SourceFile;
document: TextDocument;
document?: TextDocument;
}

export interface LitPluginContextHandler {
Expand Down
Loading

0 comments on commit 72727bc

Please sign in to comment.