Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jan 2, 2023
1 parent 20fe2fb commit 62952b5
Show file tree
Hide file tree
Showing 7 changed files with 768 additions and 225 deletions.
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eufemia/eslint-plugin",
"version": "1.0.4",
"version": "1.0.5",
"description": "ESLint rules for DNB Eufemia",
"keywords": [
"eslint",
Expand All @@ -13,7 +13,9 @@
"module": "dist/eufemia-eslint-plugin.esm.js",
"scripts": {
"release": "yarn build && npm publish",
"build": "preconstruct build"
"build": "preconstruct build",
"test": "vitest",
"test:watch": "vitest --watch"
},
"prepublish": "yarn build",
"exports": {
Expand All @@ -34,12 +36,16 @@
"devDependencies": {
"@babel/preset-typescript": "^7.18.6",
"@preconstruct/cli": "^2.2.2",
"@typescript-eslint/utils": "^5.48.0",
"eslint": "^7.10.0",
"typescript": "^4.9.4"
"@types/eslint": "^8.4.10",
"eslint": "^8.31.0",
"typescript": "^4.9.4",
"vitest": "^0.26.3"
},
"publishConfig": {
"access": "public"
},
"packageManager": "yarn@3.3.0"
"packageManager": "yarn@3.3.0",
"dependencies": {
"@typescript-eslint/utils": "^5.48.0"
}
}
40 changes: 40 additions & 0 deletions src/__test__/calc-arguments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { it } from 'vitest'
import { RuleTester } from 'eslint'
import { rule } from '../rules/calc-arguments'

const ruleTester = new RuleTester()

it('calc-arguments', () => {
// @ts-ignore
ruleTester.run('calc-arguments', rule, {
valid: [
{
code: `calc('large', 'small')`,
},
{
code: `calc('2rem', '1rem')`,
},
],

invalid: [
{
code: `calc('small', 'large')`,
errors: [
{
message: '"large" should come before "small".',
type: 'Literal',
},
],
},
{
code: `calc('large small')`,
errors: [
{
message: 'Use function arguments instead of whitespaces.',
type: 'Literal',
},
],
},
],
})
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import calcArguments from './rules/calc-arguments'
import { rule as calcArguments } from './rules/calc-arguments'

export let rules = {
export const rules = {
'calc-arguments': calcArguments,
}
21 changes: 8 additions & 13 deletions src/rules/calc-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import type { TSESTree, TSESLint } from '@typescript-eslint/utils'
import type { TSESTree } from '@typescript-eslint/utils'
import { createRule } from '../utils/RuleCreator'

type MessageIds = 'order' | 'whitespaces'

export default {
export const rule = createRule({
defaultOptions: [],
name: 'calc-arguments',
meta: {
type: 'suggestion',
docs: {
description: 'Helps to keep consistent code.',
url: 'https://github.com/dnbexperience/eslint-plugin-eufemia/blob/main/docs/rules/calc-arguments.md',
recommended: false,
},
fixable: 'code',
messages: {
order: '"{{type}}" should come before "{{prev}}".',
whitespaces: 'Use function arguments instead of whitespaces.',
},
schema: [],
},
create(context) {
const correctOrder = [
Expand All @@ -27,7 +28,7 @@ export default {
]

return {
CallExpression(node: TSESTree.CallExpression) {
CallExpression(node) {
const callee = node.callee as TSESTree.Identifier

if (callee.name === 'calc') {
Expand Down Expand Up @@ -60,10 +61,4 @@ export default {
},
}
},
} as TSESLint.RuleModule<MessageIds, Options>

type Options = [
{
// someOption?: '' | ''
}
]
})
8 changes: 8 additions & 0 deletions src/utils/RuleCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ESLintUtils } from '@typescript-eslint/utils'

const getDocsUrl = (name: string): string =>
`https://github.com/dnbexperience/eslint-plugin-eufemia/blob/main/docs/rules/${name}.md`

export const createRule: ReturnType<typeof ESLintUtils.RuleCreator> = (
...args
) => ESLintUtils.RuleCreator(getDocsUrl)(...args)
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "src/__test__"],
"exclude": ["dist"]
}
Loading

0 comments on commit 62952b5

Please sign in to comment.