Skip to content

Commit

Permalink
chore: add linting to repo (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Jan 16, 2021
1 parent 571624d commit 926c402
Show file tree
Hide file tree
Showing 41 changed files with 164 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
jest.config.js
fixtures
coverage
__snapshots__
11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
]
}
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ jobs:
run: |
yarn check-configs
- name: Run linting
run: |
yarn lint
- name: Run unit tests
run: |
yarn test
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"pre-push": "yarn check-readme && yarn format-check",
"format": "prettier --write \"./**/*.{ts,js,json,md}\"",
"format-check": "prettier --check \"./**/*.{ts,js,json,md}\"",
"lint": "eslint . --ext .js,.ts",
"typecheck": "lerna run typecheck",
"check-readme": "yarn exec-tool check-readme",
"update-readme": "yarn exec-tool update-readme",
Expand Down Expand Up @@ -54,7 +55,9 @@
"@types/node": "^10.12.2",
"@types/prettier": "^1.19.0",
"@typescript-eslint/parser": "4.3.0",
"@typescript-eslint/eslint-plugin": "4.3.0",
"eslint": "^7.6.0",
"eslint-config-prettier": "7.1.0",
"execa": "^3.4.0",
"husky": "^3.0.0",
"jest": "^26.6.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/builder/src/utils/eslint-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ESLint } from 'eslint';
import type * as ESLintLibrary from 'eslint';
import { Schema } from '../schema';

export async function loadESLint() {
export async function loadESLint(): Promise<typeof ESLintLibrary> {
let eslint;
try {
eslint = await import('eslint');
Expand All @@ -14,7 +14,7 @@ export async function loadESLint() {
export async function lint(
eslintConfigPath: string | undefined,
options: Schema,
): Promise<ESLint.LintResult[]> {
): Promise<ESLintLibrary.ESLint.LintResult[]> {
const projectESLint = await loadESLint();

const eslint = new projectESLint.ESLint({
Expand Down
5 changes: 4 additions & 1 deletion packages/builder/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MockESLint {
loadFormatter = mockLoadFormatter;
}

let mockReports: any[] = [
let mockReports: unknown[] = [
{ results: [], messages: [], usedDeprecatedRules: [] },
];
function mockEslint() {
Expand Down Expand Up @@ -72,6 +72,7 @@ function createValidRunBuilderOptions(
function setupMocks() {
jest.resetModules();
jest.clearAllMocks();
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(process, 'chdir').mockImplementation(() => {});
mockEslint();
}
Expand All @@ -91,6 +92,7 @@ async function runBuilder(options: Schema) {
* to run a build before tests run and it is dynamic enough
* to come after jest does its mocking
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { default: builderImplementation } = require('../src/index');
testArchitectHost.addBuilder(builderName, builderImplementation);

Expand Down Expand Up @@ -132,6 +134,7 @@ describe('Linter Builder', () => {

it('should invoke the linter with the options that were passed to the builder', async () => {
setupMocks();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { lint } = require('../src/utils/eslint-utils');
await runBuilder(
createValidRunBuilderOptions({
Expand Down
8 changes: 6 additions & 2 deletions packages/builder/tests/utils/eslint-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ jest.mock('eslint', () => ({
ESLint: jest.fn(),
}));

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { ESLint } = require('eslint');
(<jest.SpyInstance>ESLint).mockImplementation(() => ({
lintFiles: (args: string[]) => args,
}));

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { lint } = require('../../src/utils/eslint-utils');

describe('eslint-utils', () => {
Expand All @@ -15,10 +17,11 @@ describe('eslint-utils', () => {
});

it('should create the ESLint instance with the proper parameters', async () => {
await lint('./.eslintrc.json', <any>{
await lint('./.eslintrc.json', <unknown>{
fix: true,
cache: true,
cacheLocation: '/root/cache',
// eslint-disable-next-line @typescript-eslint/no-empty-function
}).catch(() => {});

expect(ESLint).toHaveBeenCalledWith({
Expand All @@ -33,10 +36,11 @@ describe('eslint-utils', () => {
});

it('should create the ESLint instance with the proper parameters', async () => {
await lint(undefined, <any>{
await lint(undefined, <unknown>{
fix: true,
cache: true,
cacheLocation: '/root/cache',
// eslint-disable-next-line @typescript-eslint/no-empty-function
}).catch(() => {});

expect(ESLint).toHaveBeenCalledWith({
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin-template/src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export function preprocessComponentFile(

export function postprocessComponentFile(
multiDimensionalMessages: any[][],
// 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[] {
const messagesFromComponentSource = multiDimensionalMessages[0];
Expand All @@ -213,7 +215,7 @@ export function postprocessComponentFile(
...messagesFromComponentSource,

// Ah, multi-dimensional arrays without .flat() ...
...([] as any[]).concat(
...([] as unknown[]).concat(
...messagesFromAllInlineTemplateHTML.map(
(messagesFromInlineTemplateHTML, i) => {
const inlineTemplateTmpFilename = `inline-template-${++i}.component.html`;
Expand All @@ -232,11 +234,9 @@ export function postprocessComponentFile(
}) => {
message.line =
message.line + rangeData.lineAndCharacter.start.line;
message.column = message.column;

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

if (message.fix) {
const startOffset = rangeData.range[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ function isValidAriaPropertyValue(
case 'string':
return isString(attributeValue);
case 'token':
case 'tokenlist':
case 'tokenlist': {
const parsedAttributeValue = isBooleanLike(attributeValue)
? JSON.parse((attributeValue as unknown) as string)
: attributeValue;
return Boolean(values?.includes(parsedAttributeValue));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let interactiveElementAXObjectSchemas: AXObjectSchema[] | null = null;
export function getInteractiveElementAXObjectSchemas(): AXObjectSchema[] {
if (interactiveElementAXObjectSchemas === null) {
// This package doesn't have type definitions.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { AXObjects, elementAXObjects } = require('axobject-query');

// This set will contain all possible roles in ARIA, which are
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-template/tests/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ESLINT_PLUGIN_TEMPLATE_PREFIX = '@angular-eslint/template/';

interface Config {
extends?: string | string[];
rules?: { [ruleName: string]: string | object };
rules?: { [ruleName: string]: string | Record<string, unknown> };
overrides?: Config[];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-template/tests/processors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('extract-inline-html', () => {
});

describe('components with inline templates and CRLF', () => {
let inlineTemplate = `\r\n
const inlineTemplate = `\r\n
<div [style.height]="aBool ? '100px' : '200px'"></div>\r\n
<div [style]="{height: this.aBool ? '100px' : '200px'}"></div>\r\n
<div [style]="{height: '100px'}"></div>\r\n
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-input-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default createESLintRule<Options, MessageIds>({
if (inputCallExpression.arguments.length === 0) return;

// handle directive's selector is also an input property
let directiveSelectors: ReadonlyArray<any>;
let directiveSelectors: ReadonlyArray<unknown>;

const canPropertyBeAliased = (
propertyAlias: string,
Expand Down Expand Up @@ -129,7 +129,7 @@ export default createESLintRule<Options, MessageIds>({

if (selector && isLiteral(selector) && selector.value) {
directiveSelectors = (selector.value.toString() || '')
.replace(/[\[\]\s]/g, '')
.replace(/[[\]\s]/g, '')
.split(',');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-output-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default createESLintRule<Options, MessageIds>({
if (selector && isLiteral(selector) && selector.value) {
directiveSelectors = new Set(
(selector.value.toString() || '')
.replace(/[\[\]\s]/g, '')
.replace(/[[\]\s]/g, '')
.split(','),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/relative-url-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type MessageIds = 'relativeUrlPrefix';
export const RULE_NAME = 'relative-url-prefix';

const STYLE_GUIDE_LINK = 'https://angular.io/styleguide#style-05-04';
const RELATIVE_URL_PREFIX_MATCHER = /^\.{1,2}\/[^.\/]/;
const RELATIVE_URL_PREFIX_MATCHER = /^\.{1,2}\/[^./]/;

export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,15 @@ export const SelectorValidator = {
},

camelCase(selector: string): boolean {
return /^[a-zA-Z0-9\[\]]+$/.test(selector);
return /^[a-zA-Z0-9[\]]+$/.test(selector);
},

element(selector: string): boolean {
return selector !== null;
},

kebabCase(selector: string): boolean {
return /^[a-z0-9\-]+-[a-z0-9\-]+$/.test(selector);
return /^[a-z0-9-]+-[a-z0-9-]+$/.test(selector);
},

prefix(
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ESLINT_PLUGIN_PREFIX = '@angular-eslint/';

interface Config {
extends?: string | string[];
rules?: { [ruleName: string]: string | object };
rules?: { [ruleName: string]: string | unknown };
overrides?: Config[];
}

Expand Down
39 changes: 21 additions & 18 deletions packages/integration-tests/integration-tests-setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import execa from 'execa';
const { spawn } = require('child_process');
const kill = require('tree-kill');
import { ChildProcess, spawn } from 'child_process';
import kill from 'tree-kill';

let localRegistryProcess: any;
let localRegistryProcess: ChildProcess;

const VERSION = `9999.0.1-local-integration-tests`;

Expand All @@ -20,7 +20,7 @@ async function spawnLocalRegistry() {
'4872',
]);

let collectedOutput: string[] = [];
const collectedOutput: string[] = [];
let resolvedOrRejected = false;

setTimeout(() => {
Expand Down Expand Up @@ -51,7 +51,7 @@ async function spawnLocalRegistry() {
}

async function publishPackagesToVerdaccio() {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -67,7 +67,7 @@ async function publishPackagesToVerdaccio() {
}

async function runNpmInstall() {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -83,7 +83,7 @@ async function runNpmInstall() {
}

async function runYarnInstall() {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -99,7 +99,7 @@ async function runYarnInstall() {
}

async function runNgAdd() {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -119,7 +119,7 @@ async function runNgAdd() {
}

async function runNgNew(workspaceName: string) {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -144,7 +144,7 @@ async function runNgNew(workspaceName: string) {
}

async function runNgGenerate(args: string[]) {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -160,7 +160,7 @@ async function runNgGenerate(args: string[]) {
}

async function runConvertTSLintToESLint(projectName: string) {
if (process.env.npm_config_registry!.indexOf('http://localhost') === -1) {
if (process.env.npm_config_registry?.indexOf('http://localhost') === -1) {
throw Error(`
------------------
💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
Expand All @@ -183,7 +183,6 @@ async function runConvertTSLintToESLint(projectName: string) {

async function setupFixtures() {
try {
// @ts-ignore
await spawnLocalRegistry();
await publishPackagesToVerdaccio();

Expand Down Expand Up @@ -233,19 +232,23 @@ function cleanUp(code: number) {
if (!process.env.CI) {
kill(0);
}
} catch (e) {}
// eslint-disable-next-line no-empty
} catch {}
try {
if (localRegistryProcess) localRegistryProcess.kill(0);
} catch (e) {}
if (localRegistryProcess) localRegistryProcess.kill();
// eslint-disable-next-line no-empty
} catch {}
// try killing everything after in case something hasn't terminated
try {
if (!process.env.CI) {
kill(0, 'SIGKILL');
}
} catch (e) {}
// eslint-disable-next-line no-empty
} catch {}
try {
if (localRegistryProcess) localRegistryProcess.kill(0, 'SIGKILL');
} catch (e) {}
if (localRegistryProcess) localRegistryProcess.kill('SIGKILL');
// eslint-disable-next-line no-empty
} catch {}

process.exit(code);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import * as execa from 'execa';
import path from 'path';

Expand Down
Loading

0 comments on commit 926c402

Please sign in to comment.