Skip to content

Commit

Permalink
add no-restricted-syntax to prevent class uses
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuunen committed Dec 23, 2023
1 parent e575690 commit 8ac9f47
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@
"max-statements-per-line": "off",
"no-console": "error",
"no-inline-comments": "off",
"no-restricted-syntax": [
"error",
{
"message": "Avoid using Class, just use good old unit-testable functions :)",
"selector": "ClassDeclaration"
}
],
"object-curly-spacing": [
"error",
"always"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ If file already exists, use `--force` to overwrite it.
- [ ] check last tag, suggest to tag if last one is old
- [ ] extends unit tests to src/files (remove `c8 ignore start` temporary exclusions)
- [ ] prepare a json schema for .repo-checker.json
- [ ] check vitest config file
- [ ] try to avoid using class in this project and remove `eslint-disable-next-line no-restricted-syntax`

## Benchmarks

Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const dataFileName = '.repo-checker.json'
export const repoCheckerPath = path.join(__dirname, '..')
export const templatePath = path.join(repoCheckerPath, 'templates')

// eslint-disable-next-line no-restricted-syntax
export class ProjectData {

/**
Expand Down
1 change: 1 addition & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileExists, getFileSizeInKo, join, messageToCode, readFileInFolder, rea

const defaultAmount = 99

// eslint-disable-next-line no-restricted-syntax
export class FileBase {

public passed: string[] = []
Expand Down
1 change: 1 addition & 0 deletions src/files/dependency-cruiser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileBase } from '../file'

// eslint-disable-next-line no-restricted-syntax
export class DependencyCruiserFile extends FileBase {
public async start (): Promise<void> {
const isUsingDepCruiser = this.test(this.data.isUsingDependencyCruiser, 'use dependency cruiser', true)
Expand Down
1 change: 1 addition & 0 deletions src/files/editor-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileBase } from '../file'

// eslint-disable-next-line no-restricted-syntax
export class EditorConfigFile extends FileBase {
public async start (): Promise<void> {
const hasFile = await this.checkFileExists('.editorconfig')
Expand Down
1 change: 1 addition & 0 deletions src/files/eslint.file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { log } from '../logger'
import { objectToJson, readFileInFolder } from '../utils'
import { recommendedVueRules, specificRepoCheckerRules, type EslintConfigRules, type EslintRcJsonFile } from './eslint.model'

// eslint-disable-next-line no-restricted-syntax
export class EsLintFile extends FileBase {

private checkExtends () {
Expand Down
1 change: 1 addition & 0 deletions src/files/gh-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class GithubWorkflowFile extends FileBase {

private checkPnpm (): void {
Expand Down
1 change: 1 addition & 0 deletions src/files/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileBase } from '../file'

// eslint-disable-next-line no-restricted-syntax
export class GitFile extends FileBase {
public async start (): Promise<void> {
this.test(!this.data.hasMainBranch, 'avoid "main" branch reference, use master instead & git bclean', true)
Expand Down
1 change: 1 addition & 0 deletions src/files/license.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class LicenseFile extends FileBase {
public async start (): Promise<void> {
const hasFile = await this.checkFileExists('LICENSE')
Expand Down
1 change: 1 addition & 0 deletions src/files/npmrc.file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileBase } from '../file'

// eslint-disable-next-line no-restricted-syntax
export class NpmRcFile extends FileBase {

private checkPrePost () {
Expand Down
1 change: 1 addition & 0 deletions src/files/nvmrc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class NvmrcFile extends FileBase {
public async start (): Promise<void> {
const hasFile = await this.checkFileExists('.nvmrc')
Expand Down
1 change: 1 addition & 0 deletions src/files/nycrc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'
import { log } from '../logger'

// eslint-disable-next-line no-restricted-syntax
export class NycRcFile extends FileBase {

private async getConfigFileName (): Promise<string> {
Expand Down
1 change: 1 addition & 0 deletions src/files/package.file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FileBase } from '../file'
import { log } from '../logger'
import { findInFolder, join } from '../utils'

// eslint-disable-next-line no-restricted-syntax
export class PackageJsonFile extends FileBase {

// eslint-disable-next-line max-statements
Expand Down
3 changes: 3 additions & 0 deletions src/files/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FileBase } from '../file'
import { log } from '../logger'
import { fileExists, join, readFile } from '../utils'

// eslint-disable-next-line no-restricted-syntax
class Thanks {
public markdown = ''

Expand All @@ -16,6 +17,7 @@ class Thanks {
}
}

// eslint-disable-next-line no-restricted-syntax
class Badge {
public markdown = ''

Expand All @@ -33,6 +35,7 @@ const deprecatedBadges = [
]

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class ReadmeFile extends FileBase {
private checkMarkdown (): void {
let hasNoCrLf = this.shouldContains('no CRLF Windows carriage return', /\r/u, 0, false, 'prefer Unix LF', true)
Expand Down
1 change: 1 addition & 0 deletions src/files/renovate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class RenovateFile extends FileBase {
public async start (): Promise<void> {
const hasFile = await this.checkFileExists('renovate.json', true)
Expand Down
1 change: 1 addition & 0 deletions src/files/repo-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dataFileName } from '../constants'
import { FileBase } from '../file'
import { deleteFile, join, jsToJson, readFileInFolder, writeFile } from '../utils'

// eslint-disable-next-line no-restricted-syntax
export class RepoCheckerConfigFile extends FileBase {
public async start (): Promise<void> {
await this.migrateOldConfig('.repo-checker.js')
Expand Down
1 change: 1 addition & 0 deletions src/files/tailwind.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileBase } from '../file'

// eslint-disable-next-line no-restricted-syntax
export class TailwindFile extends FileBase {
public async start (): Promise<void> {
if (!this.data.isUsingTailwind) return
Expand Down
1 change: 1 addition & 0 deletions src/files/travis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FileBase } from '../file'

/* c8 ignore start */
// eslint-disable-next-line no-restricted-syntax
export class TravisFile extends FileBase {
public async start (): Promise<void> {
const hasFile = await this.fileExists('.travis.yml')
Expand Down
1 change: 1 addition & 0 deletions src/files/ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface TsConfigJsonFile {
include: string[]
}

// eslint-disable-next-line no-restricted-syntax
export class TsConfigFile extends FileBase {

private fileContentObject: TsConfigJsonFile | undefined
Expand Down
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bgBlue, black, Logger } from 'shuutils'

// eslint-disable-next-line no-restricted-syntax
class ExtendedLogger extends Logger {
public fix (...stuff: readonly string[]): void {
const items = Array.from(stuff)
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/check.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ exports[`check C data/tsProject 1`] = `
"warnings": [
"use-dependency-cruiser",
"eslintrc-json-could-have-unicorn-rules-extend-plugin-unicorn-all",
"eslintrc-json-current-eslintrc-json-has-only-1-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"eslintrc-json-current-eslintrc-json-has-only-1-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
"has-a-npmrc-file",
"package-json-main-file-maximum-size-is-specified-in-data-file-ex-max-size-ko-100",
"package-json-could-have-a-schema-declaration-like-schema-https-json-schemastore-org-package-43m30m-fixable-39m49m",
Expand Down Expand Up @@ -333,7 +333,7 @@ exports[`check E data folders and not throw 1`] = `
"warnings": [
"use-dependency-cruiser",
"eslintrc-json-could-have-unicorn-rules-extend-plugin-unicorn-all",
"eslintrc-json-current-eslintrc-json-has-only-1-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"eslintrc-json-current-eslintrc-json-has-only-1-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
"has-a-npmrc-file",
"package-json-main-file-maximum-size-is-specified-in-data-file-ex-max-size-ko-100",
"package-json-could-have-a-schema-declaration-like-schema-https-json-schemastore-org-package-43m30m-fixable-39m49m",
Expand Down
9 changes: 9 additions & 0 deletions tests/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const missingFilepath = join(repoCheckerPath, missingFilename)
const fakeContent = 'zorglub'

it('file A simple validator', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFile extends FileBase {
public async start (): Promise<void> {
await writeFile(missingFilepath, 'Foobar content')
Expand All @@ -38,6 +39,7 @@ it('file A simple validator', async () => {
})

it('file B validator with fix', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFix extends FileBase {
public async start (): Promise<void> {
await this.checkFileExists(existingFilename)
Expand All @@ -55,6 +57,7 @@ it('file B validator with fix', async () => {
})

it('file C validator with fix & force, overwrite a problematic file with template', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFixForce extends FileBase {
public async start (): Promise<void> {
await this.inspectFile(existingFilename)
Expand All @@ -73,6 +76,7 @@ it('file C validator with fix & force, overwrite a problematic file with templat

it('file D validator with fix & force, update a problematic file on the go', async () => {
let originalContent = ''
// eslint-disable-next-line no-restricted-syntax
class MyFileFixForce extends FileBase {
public async start (): Promise<void> {
await this.inspectFile(existingFilename)
Expand All @@ -90,6 +94,7 @@ it('file D validator with fix & force, update a problematic file on the go', asy

it('file E validator without force cannot fix a problematic file on the go', async () => {
let originalContent = ''
// eslint-disable-next-line no-restricted-syntax
class MyFileFixForce extends FileBase {
public async start (): Promise<void> {
await this.inspectFile(existingFilename)
Expand All @@ -106,6 +111,7 @@ it('file E validator without force cannot fix a problematic file on the go', asy
})

it('file F validator with fix cannot fix if the template require data that is missing', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFix extends FileBase {
public async start (): Promise<void> {
await this.checkFileExists('template-example.json')
Expand All @@ -118,6 +124,7 @@ it('file F validator with fix cannot fix if the template require data that is mi
})

it('file G validator can detect a missing schema', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFix extends FileBase {
public async start (): Promise<void> {
await sleep(1)
Expand All @@ -132,6 +139,7 @@ it('file G validator can detect a missing schema', async () => {
})

it('file H validator can detect an existing schema', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFix extends FileBase {
public async start (): Promise<void> {
await sleep(1)
Expand All @@ -150,6 +158,7 @@ it('file H validator can detect an existing schema', async () => {
})

it('file I validator can fix a missing schema', async () => {
// eslint-disable-next-line no-restricted-syntax
class MyFileFix extends FileBase {
public async start (): Promise<void> {
await sleep(1)
Expand Down
25 changes: 16 additions & 9 deletions tests/files/__snapshots__/eslint.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports[`eslint B config file empty 1`] = `
"could-have-hardcore-rules-extend-hardcore",
"could-have-eslint-recommended-rules-extend-eslint-recommended",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-16-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-16-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
],
}
`;
Expand Down Expand Up @@ -142,7 +142,7 @@ exports[`eslint C config file empty for ts project 1`] = `
"could-have-hardcore-rules-extend-hardcore",
"could-have-eslint-recommended-rules-extend-eslint-recommended",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-0-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-0-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
"could-have-hardcore-typescript-rules-extend-hardcore-ts",
],
}
Expand Down Expand Up @@ -196,7 +196,7 @@ exports[`eslint D config file empty for vue ts project 1`] = `
"could-have-hardcore-rules-extend-hardcore",
"could-have-eslint-recommended-rules-extend-eslint-recommended",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-0-of-the-72-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-0-of-the-73-custom-rules-in-repo-checker-eslintrc-json",
"could-have-hardcore-typescript-rules-extend-hardcore-ts",
],
}
Expand Down Expand Up @@ -249,7 +249,7 @@ exports[`eslint E config file empty for tailwind project 1`] = `
"could-have-hardcore-rules-extend-hardcore",
"could-have-eslint-recommended-rules-extend-eslint-recommended",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-16-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-16-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
],
}
`;
Expand Down Expand Up @@ -309,7 +309,7 @@ exports[`eslint F config partial file for js project 1`] = `
"warnings": [
"could-have-hardcore-rules-extend-hardcore",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-16-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-16-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
],
}
`;
Expand Down Expand Up @@ -368,7 +368,7 @@ exports[`eslint G config partial file for ts project 1`] = `
"warnings": [
"could-have-hardcore-rules-extend-hardcore",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-0-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-0-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
"could-have-hardcore-typescript-rules-extend-hardcore-ts",
],
}
Expand Down Expand Up @@ -440,7 +440,7 @@ exports[`eslint H config partial file for vue ts project 1`] = `
"warnings": [
"could-have-hardcore-rules-extend-hardcore",
"could-have-unicorn-rules-extend-plugin-unicorn-all",
"current-eslintrc-json-has-only-0-of-the-72-custom-rules-in-repo-checker-eslintrc-json",
"current-eslintrc-json-has-only-0-of-the-73-custom-rules-in-repo-checker-eslintrc-json",
"could-have-hardcore-typescript-rules-extend-hardcore-ts",
],
}
Expand Down Expand Up @@ -578,7 +578,7 @@ exports[`eslint J config file with no rules 1`] = `
],
"warnings": [
"eslintrc-json-could-have-unicorn-rules-extend-plugin-unicorn-all",
"eslintrc-json-current-eslintrc-json-has-only-16-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"eslintrc-json-current-eslintrc-json-has-only-16-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
],
}
`;
Expand Down Expand Up @@ -654,7 +654,7 @@ exports[`eslint K config file with just rules (no override) 1`] = `
],
"warnings": [
"eslintrc-json-could-have-unicorn-rules-extend-plugin-unicorn-all",
"eslintrc-json-current-eslintrc-json-has-only-17-of-the-65-custom-rules-in-repo-checker-eslintrc-json",
"eslintrc-json-current-eslintrc-json-has-only-17-of-the-66-custom-rules-in-repo-checker-eslintrc-json",
],
}
`;
Expand Down Expand Up @@ -878,6 +878,13 @@ exports[`eslint M fix rules in overrides 1`] = `
"max-statements-per-line": "off",
"no-console": "error",
"no-inline-comments": "off",
"no-restricted-syntax": [
"error",
{
"message": "Avoid using Class, just use good old unit-testable functions :)",
"selector": "ClassDeclaration"
}
],
"object-curly-spacing": [
"error",
"always"
Expand Down

0 comments on commit 8ac9f47

Please sign in to comment.