Skip to content

Commit

Permalink
test: Extend test coverage for general code style cases
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jan 14, 2025
1 parent 05f1d64 commit 09c1998
Show file tree
Hide file tree
Showing 30 changed files with 433 additions and 87 deletions.
15 changes: 14 additions & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later

version = 1
SPDX-PackageName = "eslint-config"
SPDX-PackageSupplier = "Nextcloud <[email protected]>"
Expand All @@ -12,7 +13,19 @@ SPDX-FileCopyrightText = "2019 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "AGPL-3.0-or-later"

[[annotations]]
path = ["tsconfig.json"]
path = ["tsconfig.json", "tests/tsconfig.json"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2025 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "CC0-1.0"

# Test fixtures might not be able to have headers (as e.g. the header is tested)
[[annotations]]
path = [
"tests/fixtures/codestyle/input/*.ts",
"tests/fixtures/codestyle/input/*.js",
"tests/fixtures/codestyle/output/*.ts",
"tests/fixtures/codestyle/output/*.js"
]
precedence = "aggregate"
SPDX-FileCopyrightText = "2025 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "AGPL-3.0-or-later"
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
import { recommended } from './dist/index.mjs'

export default [
{
ignores: [
'tests/fixtures/',
],
},
...recommended,
]
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
],
"scripts": {
"build": "vite --mode production build",
"lint": "eslint lib *.js *.ts",
"lint:fix": "eslint --fix lib *.js *.ts",
"test": "jest"
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "vitest"
},
"dependencies": {
"@eslint/json": "^0.9.0",
Expand All @@ -52,7 +52,8 @@
"@nextcloud/vite-config": "^2.3.0",
"@types/node": "^22.10.6",
"eslint": "^9.18.0",
"vite": "^6.0.7"
"vite": "^6.0.7",
"vitest": "^2.1.8"
},
"peerDependencies": {
"eslint": "^9"
Expand Down
35 changes: 35 additions & 0 deletions tests/config-codestyle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Linter } from 'eslint'

import { ESLint } from 'eslint'
import { expect, test } from 'vitest'
import * as path from 'path'
import * as eslintConfig from '../lib/index.js'

const eslint = new ESLint({
fix: true,
overrideConfigFile: true,
overrideConfig: eslintConfig.recommended as Linter.Config<Linter.RulesRecord>,
})

const lintFile = async (file) => {
const real = path.resolve(path.join(__dirname, file))
return await eslint.lintFiles(real)
}

test.for([
'array',
'arrow-function',
'function',
'indention',
'objects',
'quotes',
'semicolons',
])('Code style', async (testCase: string) => {
const results = await lintFile(`fixtures/codestyle/input/${testCase}.{t,j}s`)
expect(results).toHaveLength(1)
expect(results[0].output).toMatchFileSnapshot(results[0].filePath.replace('input', 'output'))
})
33 changes: 22 additions & 11 deletions tests/eslint-config.test.ts → tests/config-javascript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { ESLint } from "eslint"
import type { Linter } from "eslint"
import * as path from 'path'
import * as eslintConfig from '../index.js'
import type { Linter } from 'eslint'

import { ESLint } from 'eslint'
import { expect, test } from 'vitest'
import * as path from 'path'
import * as eslintConfig from '../lib/index.js'

const eslint = new ESLint({
baseConfig: eslintConfig as unknown as Linter.Config<Linter.RulesRecord>
overrideConfigFile: true,
overrideConfig: eslintConfig.recommendedJavascript as Linter.Config<Linter.RulesRecord>[],
})

const lintFile = async (file) => {
Expand All @@ -20,24 +22,33 @@ const lintFile = async (file) => {
test('some basic issues should fail', async () => {
const results = await lintFile('fixtures/example-fail.js')
expect(results).toHaveIssueCount(3)
expect(results).toHaveIssue('spaced-comment')
expect(results).toHaveIssue({ ruleId: 'no-console', line: 3 })
expect(results).toHaveIssue('@stylistic/spaced-comment')
expect(results).toHaveIssue('@stylistic/semi')
expect(results).toHaveIssue({
ruleId: 'no-console',
line: 7,
})
})

test('TSX is linted', async () => {
const ignored = await eslint.isPathIgnored('./fixtures/some.tsx')
expect(ignored).toBe(false)

const results = await lintFile('fixtures/some.tsx')
expect(results).toHaveIssue({ruleId: 'jsdoc/check-tag-names', line: 5})
expect(results).toHaveIssue({ruleId: '@typescript-eslint/no-unused-vars', line: 7})
expect(results).toHaveIssue({
ruleId: 'jsdoc/check-tag-names',
line: 10,
})
expect(results).toHaveIssue({
ruleId: '@typescript-eslint/no-unused-vars',
line: 12,
})
expect(results).toHaveIssueCount(2)
})

test('ignore camelcase for webpack', async () => {
const results = await lintFile('fixtures/webpack-nonce.js')
expect(results).toHaveIssueCount(1)
expect(results).toHaveIssue({ruleId: 'no-undef', line: 2 })
expect(results).toHaveIssueCount(0)
})

test('works with Vue Composition API', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { ESLint } from 'eslint'
import type { Linter } from 'eslint'

import { ESLint } from 'eslint'
import { expect, test } from 'vitest'
import * as path from 'path'
import * as eslintConfig from '../typescript.js'
import * as eslintConfig from '../lib/index.js'

const eslint = new ESLint({
baseConfig: eslintConfig as unknown as Linter.Config<Linter.RulesRecord>,
overrideConfigFile: true,
overrideConfig: eslintConfig.recommended as Linter.Config<Linter.RulesRecord>,
})

const lintFile = async (file) => {
Expand All @@ -23,5 +26,10 @@ test('works with Typescript + Vue', async () => {

test('Typescript overrides have higher priority than vue', async () => {
const results = await lintFile('fixtures/typescript-vue-overrides.vue')
expect(results).toHaveIssueCount(0)
expect(results).toHaveIssueCount(1)
// Expect "'@type' is redundant when using a type system."
expect(results).toHaveIssue({
ruleId: 'jsdoc/check-tag-names',
line: 15,
})
})
15 changes: 0 additions & 15 deletions tests/eslint.d.ts

This file was deleted.

9 changes: 9 additions & 0 deletions tests/fixtures/codestyle/input/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// this should be multi line
const arr = ['first', 'second', 'third', 'and', 'so', 'on']

// this has a missing trailing comma causing too much git diff
const arr2 = [
'first',
'second',
'third'
]
10 changes: 10 additions & 0 deletions tests/fixtures/codestyle/input/arrow-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* An arrow function with the { on the wrong line
*/
const arrow = (name: string) =>
{
//
}

// Arrow functions should always have parenthesis for readability
const arr = ['myArray'].map(item => item.length)
46 changes: 46 additions & 0 deletions tests/fixtures/codestyle/input/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* A function with the { on the wrong line
*
* @param name
*/
export function foo(name: string): boolean
{
return true
}

/**
* Also a function with the { on the wrong line
*
* @param firstName
* @param lastName
*/
export function bar(
firstName: string,
lastName: string,
): boolean
{
return false
}

/**
* Parameters should always be either on the same line or have consistent new lines
*
* @param num
* @param enable
*/
export function doSomething(num: number,
enable: boolean) {
// ...
}

/**
* Same here: Parameters should always be either on the same line or have consistent new lines
*
* @param num
* @param enable
*/
export function doSomethingDifferent(
num: number, enable: boolean
) {
// ...
}
7 changes: 7 additions & 0 deletions tests/fixtures/codestyle/input/indention.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const obj = {
foo: 'bar'
}

class Foo {
a = 1
}
24 changes: 24 additions & 0 deletions tests/fixtures/codestyle/input/objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Only quote if needed
const obj = {
'noQuotesNeeded': true,
'quotes-needed': false,
}

// Prefer shorthand property
const user = 'jdoe'
const obj2 = {
user: user,
id: 123,
}

// Require spaces around braces
const obj3 = {first: 1}

// Prefer multi line objects
const obj4 = { first: 1, second: 'two' }

// Use trailing commas to be git diff friendly
const obj5 = {
first: 1,
second: 2
}
5 changes: 5 additions & 0 deletions tests/fixtures/codestyle/input/quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const foo = "foo"
const bar = 'bar'
const baz = `baz`
const escape = `Escape 'the' single quote`
const escape2 = "Escape 'the' single quote"
8 changes: 8 additions & 0 deletions tests/fixtures/codestyle/input/semicolons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const text = 'foo';
console.log(text);

const other = 'foo'
;[
'1',
'2',
].map(console.log)
16 changes: 16 additions & 0 deletions tests/fixtures/codestyle/output/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// this should be multi line
const arr = [
'first',
'second',
'third',
'and',
'so',
'on',
]

// this has a missing trailing comma causing too much git diff
const arr2 = [
'first',
'second',
'third',
]
11 changes: 11 additions & 0 deletions tests/fixtures/codestyle/output/arrow-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* An arrow function with the { on the wrong line
*
* @param name
*/
const arrow = (name: string) => {
//
}

// Arrow functions should always have parenthesis for readability
const arr = ['myArray'].map((item) => item.length)
Loading

0 comments on commit 09c1998

Please sign in to comment.