Skip to content

Commit

Permalink
Merge branch 'master' into feat/builder-cache-strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Jun 7, 2021
2 parents b5f4c62 + fb81948 commit fabd4ee
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 119 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Please consider creating an isolated reproduction repo to make it easy for the v
| package | version |
| ---------------------------------- | ------- |
| `@angular-eslint/builder` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `ESLint` | `X.Y.Z` |
| `node` | `X.Y.Z` |

Expand Down
13 changes: 12 additions & 1 deletion .github/ISSUE_TEMPLATE/eslint-plugin-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ Are you opening an issue because the rule you're trying to use is not found?
Please consider creating an isolated reproduction repo to make it easy for the volunteer maintainers debug your issue.
-->

```JSON
{
"rules": {
"@angular-eslint/template/<rule>": ["<setting>"]
}
}
```

```HTML
// your repro code case
```

**Versions**

| package | version |
| ---------------------------------- | ------- |
| `@angular-eslint/eslint-plugin-template` | `X.Y.Z` |
| `@angular-eslint/template-parser` | `X.Y.Z` |
| `@typescript-eslint/parser` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `ESLint` | `X.Y.Z` |
| `node` | `X.Y.Z` |

Expand Down
13 changes: 12 additions & 1 deletion .github/ISSUE_TEMPLATE/eslint-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ Are you opening an issue because the rule you're trying to use is not found?
Please consider creating an isolated reproduction repo to make it easy for the volunteer maintainers debug your issue.
-->

```JSON
{
"rules": {
"@angular-eslint/<rule>": ["<setting>"]
}
}
```

```TS
// your repro code case
```

**Versions**

| package | version |
| ---------------------------------- | ------- |
| `@angular-eslint/eslint-plugin` | `X.Y.Z` |
| `@typescript-eslint/parser` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `ESLint` | `X.Y.Z` |
| `node` | `X.Y.Z` |

Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/template-parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Please consider creating an isolated reproduction repo to make it easy for the v
| package | version |
| ---------------------------------- | ------- |
| `@angular-eslint/template-parser` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `ESLint` | `X.Y.Z` |
| `node` | `X.Y.Z` |

Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Please consider creating an isolated reproduction repo to make it easy for the v
| package | version |
| ---------------------------------- | ------- |
| `@angular-eslint/utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `ESLint` | `X.Y.Z` |
| `node` | `X.Y.Z` |

Expand Down
56 changes: 30 additions & 26 deletions packages/eslint-plugin-template/src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function isFileLikelyToContainComponentDeclarations(
return false;
}

type PreprocessResult = Array<string | { text: string; filename: string }>;
type PreprocessResult = (string | { text: string; filename: string })[];

export function preprocessComponentFile(
text: string,
Expand Down Expand Up @@ -186,11 +186,24 @@ export function preprocessComponentFile(
}

export function postprocessComponentFile(
multiDimensionalMessages: any[][],
multiDimensionalMessages: {
ruleId: string;
severity: number;
message: string;
line: number;
column: number;
nodeType: string;
messageId: string;
endLine: number;
endColumn: number;
fix?: {
range: number[];
text: string;
};
}[][],
// We don't currently need the filename in our implementation, but keeping it for clarity regarding the postprocess() function signature
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_filename: string,
): any[] {
): readonly unknown[] {
const messagesFromComponentSource = multiDimensionalMessages[0];
/**
* If the Component did not have one or more inline templates defined within it
Expand Down Expand Up @@ -224,30 +237,21 @@ export function postprocessComponentFile(
return [];
}

return messagesFromInlineTemplateHTML.map(
(message: {
line: string | number;
column: any;
endLine: string | number;
endColumn: any;
fix?: { range: [number, number]; text: string };
}) => {
message.line =
message.line + rangeData.lineAndCharacter.start.line;
return messagesFromInlineTemplateHTML.map((message) => {
message.line = message.line + rangeData.lineAndCharacter.start.line;

message.endLine =
message.endLine + rangeData.lineAndCharacter.start.line;
message.endLine =
message.endLine + rangeData.lineAndCharacter.start.line;

if (message.fix) {
const startOffset = rangeData.range[0];
message.fix.range = [
startOffset + message.fix.range[0],
startOffset + message.fix.range[1],
];
}
return message;
},
);
if (message.fix) {
const startOffset = rangeData.range[0];
message.fix.range = [
startOffset + message.fix.range[0],
startOffset + message.fix.range[1],
];
}
return message;
});
},
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TmplAstElement } from '@angular/compiler';

import type { Node, TmplAstElement } from '@angular/compiler';
import {
createESLintRule,
getTemplateParserServices,
Expand Down Expand Up @@ -53,7 +52,7 @@ export default createESLintRule<Options, MessageIds>({
},
});

function isValidNode(node: any): boolean {
function isValidNode(node: TmplAstElement): boolean {
if (node.name === 'img') {
return isValidImgNode(node);
} else if (node.name === 'object') {
Expand All @@ -79,7 +78,7 @@ function isValidImgNode(node: TmplAstElement): boolean {
* In this case, we check that the `<object>` element has a `title` or `aria-label` attribute.
* Otherwise, we check for the presence of `attr.title` or `attr.aria-label` bindings.
*/
function isValidObjectNode(node: any): boolean {
function isValidObjectNode(node: TmplAstElement): boolean {
let hasTitleAttribute = false,
hasAriaLabelAttribute = false;

Expand Down Expand Up @@ -107,7 +106,10 @@ function isValidObjectNode(node: any): boolean {
return true;
}

return node.children.length > 0 && !!node.children[0].value;
return (
node.children.length > 0 &&
!!((node.children[0] as unknown) as Node & { value?: unknown }).value
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ function getAttributeName(
}

function findDuplicates(
elements: Array<TmplAstBoundEvent>,
): Array<TmplAstBoundEvent>;
elements: readonly TmplAstBoundEvent[],
): readonly TmplAstBoundEvent[];
function findDuplicates(
elements: Array<TmplAstBoundAttribute | TmplAstTextAttribute>,
): Array<TmplAstBoundAttribute | TmplAstTextAttribute>;
elements: readonly (TmplAstBoundAttribute | TmplAstTextAttribute)[],
): readonly (TmplAstBoundAttribute | TmplAstTextAttribute)[];
function findDuplicates(
elements: Array<{ name: string }>,
): Array<{ name: string }> {
elements: readonly { name: string }[],
): readonly { name: string }[] {
return elements.filter((element) => {
return elements.some(
(otherElement) =>
Expand Down
18 changes: 7 additions & 11 deletions packages/eslint-plugin-template/src/utils/attributes-comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type {
TmplAstElement,
TmplAstTextAttribute,
} from '@angular/compiler';

import { getLiteralValue } from './get-literal-value';
import type { ARIARoleRelationConceptAttribute } from 'aria-query';
import { getAttributeValue } from './get-attribute-value';
import { getLiteralValue } from './get-literal-value';

export function attributesComparator(
baseAttributes: any[] = [],
baseAttributes: readonly ARIARoleRelationConceptAttribute[] = [],
node: TmplAstElement,
): boolean {
const attributes: (TmplAstTextAttribute | TmplAstBoundAttribute)[] = [
Expand All @@ -27,15 +27,11 @@ export function attributesComparator(
return false;
}

if (
baseAttribute.value &&
baseAttribute.value !==
return (
!baseAttribute.value ||
baseAttribute.value ===
getLiteralValue(getAttributeValue(node, baseAttribute.name))
) {
return false;
} else {
return true;
}
);
},
),
);
Expand Down
13 changes: 5 additions & 8 deletions packages/eslint-plugin-template/src/utils/create-eslint-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ interface ParserServices {
convertNodeSourceSpanToLoc: (
sourceSpan: ParseSourceSpan,
) => TSESTree.SourceLocation;
convertElementSourceSpanToLoc: <TMessageIds extends string>(
context: TSESLint.RuleContext<TMessageIds, []>,
convertElementSourceSpanToLoc: (
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>,
node: TmplAstElement,
) => TSESTree.SourceLocation;
}

export function getTemplateParserServices<
TMessageIds extends string,
TOptions extends readonly unknown[]
>(
context: Readonly<TSESLint.RuleContext<TMessageIds, TOptions>>,
export function getTemplateParserServices(
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>,
): ParserServices {
ensureTemplateParser(context);
return (context.parserServices as unknown) as ParserServices;
Expand All @@ -31,7 +28,7 @@ export function getTemplateParserServices<
* If @angular-eslint/template-parser is not the configured parser when the function is invoked it will throw
*/
export function ensureTemplateParser(
context: TSESLint.RuleContext<string, readonly unknown[]>,
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>,
): void {
if (
!((context.parserServices as unknown) as ParserServices)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { AST } from '@angular/compiler';
import type { Node } from '@angular/compiler/src/render3/r3_ast';
import type { TSESTree } from '@typescript-eslint/experimental-utils';
import { AST_NODE_TYPES } from '@typescript-eslint/types';

type ASTOrNodeWithParent = (AST | Node) & { parent?: ASTOrNodeWithParent };

function isProgram({ type }: any): boolean {
return type === 'Program';
function isProgram(node: unknown): node is TSESTree.Program {
return (node as { type?: string }).type === AST_NODE_TYPES.Program;
}

export function getNearestNodeFrom<T extends ASTOrNodeWithParent>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ export function getInteractiveElementAXObjectSchemas(): AXObjectSchema[] {

// This will contain all schemas that are related to ARIA roles
// listed in the above set `interactiveAXObjects`.
interactiveElementAXObjectSchemas = [...elementAXObjects].reduce(
(accumulator, [elementSchema, AXObjectSet]) => {
if ([...AXObjectSet].every((role) => interactiveAXObjects.has(role))) {
accumulator.push(elementSchema);
}
return accumulator;
},
[],
);
interactiveElementAXObjectSchemas = [...elementAXObjects].reduce<
AXObjectSchema[]
>((accumulator, [elementSchema, AXObjectSet]) => {
return accumulator.concat(
[...AXObjectSet].every((role) => interactiveAXObjects.has(role))
? elementSchema
: [],
);
}, []);
}

return interactiveElementAXObjectSchemas!;
return interactiveElementAXObjectSchemas;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ariaQuery from 'aria-query';
import type { ARIARoleDefintionKey, ARIARoleRelationConcept } from 'aria-query';
import { elementRoles, roles } from 'aria-query';

let interactiveElementRoleSchemas: ARIARoleRelationConcept[] | null = null;

Expand All @@ -9,16 +9,17 @@ let interactiveElementRoleSchemas: ARIARoleRelationConcept[] | null = null;
// for the first time, so we will not take up the memory.
export function getInteractiveElementRoleSchemas(): ARIARoleRelationConcept[] {
if (interactiveElementRoleSchemas === null) {
const roleKeys = [...ariaQuery.roles.keys()];
const elementRoleEntries = [...ariaQuery.elementRoles];
const roleKeys = [...roles.keys()];
const elementRoleEntries = [...elementRoles];

// This set will contain all possible values for the `role` attribute,
// e.g. `button`, `navigation` or `presentation`.
const interactiveRoles = new Set<ARIARoleDefintionKey>(
roleKeys
.filter((name: ARIARoleDefintionKey) => {
const role = ariaQuery.roles.get(name)!;
const role = roles.get(name);
return (
role &&
!role.abstract &&
// The `progressbar` is descended from `widget`, but in practice, its
// value is always `readonly`, so we treat it as a non-interactive role.
Expand All @@ -33,15 +34,15 @@ export function getInteractiveElementRoleSchemas(): ARIARoleRelationConcept[] {
),
);

interactiveElementRoleSchemas = elementRoleEntries.reduce(
(accumulator: ARIARoleRelationConcept[], [elementSchema, roleSet]) => {
if ([...roleSet].some((role) => interactiveRoles.has(role))) {
accumulator.push(elementSchema);
}
return accumulator;
},
[],
);
interactiveElementRoleSchemas = elementRoleEntries.reduce<
ARIARoleRelationConcept[]
>((accumulator, [elementSchema, roleSet]) => {
return accumulator.concat(
[...roleSet].every((role) => interactiveRoles.has(role))
? elementSchema
: [],
);
}, []);
}

return interactiveElementRoleSchemas;
Expand Down

0 comments on commit fabd4ee

Please sign in to comment.