Skip to content

Commit

Permalink
feat(config): add calcMethodName (#1)
Browse files Browse the repository at this point in the history
* feat(config): add calcMethodName

* chore: run GitHub Action to tests and release
  • Loading branch information
tujoworker committed Jan 11, 2023
1 parent 62952b5 commit ee9d12a
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 8 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Release

on:
push:
branches:
- 'main'

jobs:
action:
name: Release Action

runs-on: ubuntu-latest

env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

steps:
- name: Git checkout
uses: actions/checkout@v3
with:
persist-credentials: false

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version-file: 'package.json'

- name: Use node_modules cache
uses: actions/cache@v3
id: modules-cache
with:
path: '**/node_modules'
key: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-

- name: Install dependencies
if: steps.modules-cache.outputs.cache-hit != 'true'
run: yarn install --immutable

- name: Run tests
run: yarn test

- name: Run release
run: yarn release
51 changes: 51 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Verify

on:
push:
branches:
- '**'
- '!**--skip-ci'
- '!wip/**'
- '!experiments/**'
pull_request:
branches:
- 'main'
types: [opened]

jobs:
action:
name: Verify Action

runs-on: ubuntu-latest

env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

steps:
- name: Git checkout
uses: actions/checkout@v3
with:
persist-credentials: false

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version-file: 'package.json'

- name: Use node_modules cache
uses: actions/cache@v3
id: modules-cache
with:
path: '**/node_modules'
key: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ secrets.CACHE_VERSION }}-${{ runner.os }}-modules-

- name: Install dependencies
if: steps.modules-cache.outputs.cache-hit != 'true'
run: yarn install --immutable

- name: Run type check
run: yarn test:types

- name: Run tests
run: yarn test
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org/
always-auth=true
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ Then configure the rules you want to use under the rules section:
```json
{
"rules": {
"@eufemia/calc-arguments": "error"
"@eufemia/calc-arguments": [
"error",
{ "calcMethodName": "optionalOtherName" }
]
}
}
```

The option `calcMethodName` defaults to `calc`.

## Supported Rules

- [calc-arguments](https://github.com/dnbexperience/eslint-plugin-eufemia/blob/main/docs/rules/calc-arguments.md) (optional)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eufemia/eslint-plugin",
"version": "1.0.5",
"version": "1.1.0",
"description": "ESLint rules for DNB Eufemia",
"keywords": [
"eslint",
Expand All @@ -15,7 +15,8 @@
"release": "yarn build && npm publish",
"build": "preconstruct build",
"test": "vitest",
"test:watch": "vitest --watch"
"test:watch": "vitest --watch",
"test:types": "tsc --noEmit"
},
"prepublish": "yarn build",
"exports": {
Expand Down
14 changes: 14 additions & 0 deletions src/__test__/calc-arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ it('calc-arguments', () => {
{
code: `calc('2rem', '1rem')`,
},
{
code: `newName('large', 'small')`,
options: [{ calcMethodName: 'newName' }],
},
],

invalid: [
Expand All @@ -35,6 +39,16 @@ it('calc-arguments', () => {
},
],
},
{
code: `newName('small', 'large')`,
options: [{ calcMethodName: 'newName' }],
errors: [
{
message: '"large" should come before "small".',
type: 'Literal',
},
],
},
],
})
})
28 changes: 23 additions & 5 deletions src/rules/calc-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import type { TSESTree } from '@typescript-eslint/utils'
import { createRule } from '../utils/RuleCreator'

export const rule = createRule({
defaultOptions: [],
type Options = [
{
calcMethodName?: string
}
]

type MessageIds = 'order' | 'whitespaces'

export const rule = createRule<Options, MessageIds>({
defaultOptions: [{ calcMethodName: 'calc' }],
name: 'calc-arguments',
meta: {
type: 'suggestion',
Expand All @@ -15,9 +23,19 @@ export const rule = createRule({
order: '"{{type}}" should come before "{{prev}}".',
whitespaces: 'Use function arguments instead of whitespaces.',
},
schema: [],
schema: [
{
type: 'object',
properties: {
calcMethodName: {
type: 'string',
},
},
additionalProperties: false,
},
],
},
create(context) {
create(context, options) {
const correctOrder = [
'xx-large',
'x-large',
Expand All @@ -31,7 +49,7 @@ export const rule = createRule({
CallExpression(node) {
const callee = node.callee as TSESTree.Identifier

if (callee.name === 'calc') {
if (callee.name === options?.[0]?.calcMethodName) {
for (let i = 0, l = node.arguments.length; i < l; i++) {
const type = node.arguments[i] as TSESTree.StringLiteral
const prev = node.arguments[i - 1] as TSESTree.StringLiteral
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"isolatedModules": true,
"strict": true,
"sourceMap": true,
"skipLibCheck": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
Expand Down

0 comments on commit ee9d12a

Please sign in to comment.