Skip to content

Commit

Permalink
github workflows (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
azuradara authored Jan 9, 2024
1 parent 71acd86 commit cc70281
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 109 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Lint & Build

on:
push:
branches:
- main
pull_request:

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v3

- name: install node
uses: actions/setup-node@v3
with:
node-version: 20.9.0

- uses: pnpm/action-setup@v2
name: install pnpm
with:
version: 8.10.0
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: set up pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: install dependencies
run: pnpm install

- name: lint
run: npm run lint

- name: build
run: npm run build
10 changes: 5 additions & 5 deletions packages/eslint-plugin-youcan/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import genericSpacing from './rules/generic-spacing'
import ifNewline from './rules/if-newline'
import importDedupe from './rules/import-dedupe'
import preferInlineTypeImport from './rules/prefer-inline-type-import'
import genericSpacing from './rules/generic-spacing';
import ifNewline from './rules/if-newline';
import importDedupe from './rules/import-dedupe';
import preferInlineTypeImport from './rules/prefer-inline-type-import';

export default {
rules: {
Expand All @@ -10,4 +10,4 @@ export default {
'prefer-inline-type-import': preferInlineTypeImport,
'generic-spacing': genericSpacing,
},
}
};
56 changes: 29 additions & 27 deletions packages/eslint-plugin-youcan/src/rules/generic-spacing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createEslintRule } from '../utils'
import { createEslintRule } from '../utils';

export const RULE_NAME = 'generic-spacing'
export type MessageIds = 'genericSpacingMismatch'
export type Options = []
export const RULE_NAME = 'generic-spacing';
export type MessageIds = 'genericSpacingMismatch';
export type Options = [];

export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
Expand All @@ -20,68 +20,70 @@ export default createEslintRule<Options, MessageIds>({
},
defaultOptions: [],
create: (context) => {
const sourceCode = context.getSourceCode()
const sourceCode = context.getSourceCode();

return {
TSTypeParameterDeclaration: (node) => {
if (!['TSCallSignatureDeclaration', 'ArrowFunctionExpression', 'TSFunctionType'].includes(node.parent.type)) {
const pre = sourceCode.text.slice(0, node.range[0])
const preSpace = pre.match(/(\s+)$/)?.[0]
if (!['TSCallSignatureDeclaration', 'ArrowFunctionExpression', 'TSFunctionType'].includes(node.parent!.type)) {
const pre = sourceCode.text.slice(0, node.range[0]);
const preSpace = pre.match(/(\s+)$/)?.[0];
// strip space before <T>
if (preSpace && preSpace.length) {
context.report({
node,
messageId: 'genericSpacingMismatch',
*fix(fixer) {
yield fixer.replaceTextRange([node.range[0] - preSpace.length, node.range[0]], '')
yield fixer.replaceTextRange([node.range[0] - preSpace.length, node.range[0]], '');
},
})
});
}
}

// add space between <T,K>
const params = node.params
const params = node.params;
for (let i = 1; i < params.length; i++) {
const prev = params[i - 1]
const current = params[i]
const from = prev.range[1]
const to = current.range[0]
const span = sourceCode.text.slice(from, to)
const prev = params[i - 1];
const current = params[i];
const from = prev.range[1];
const to = current.range[0];
const span = sourceCode.text.slice(from, to);
if (span !== ', ' && !span.match(/,\n/)) {
context.report({
*fix(fixer) {
yield fixer.replaceTextRange([from, to], ', ')
yield fixer.replaceTextRange([from, to], ', ');
},
loc: {
start: prev.loc.end,
end: current.loc.start,
},
messageId: 'genericSpacingMismatch',
node,
})
});
}
}
},
// add space around = in type Foo<T = true>
TSTypeParameter: (node) => {
if (!node.default)
return
const endNode = node.constraint || node.name
const from = endNode.range[1]
const to = node.default.range[0]
if (!node.default) {
return;
}
const endNode = node.constraint || node.name;
const from = endNode.range[1];
const to = node.default.range[0];
if (sourceCode.text.slice(from, to) !== ' = ') {
context.report({
*fix(fixer) {
yield fixer.replaceTextRange([from, to], ' = ')
yield fixer.replaceTextRange([from, to], ' = ');
},
loc: {
start: endNode.loc.end,
end: node.default.loc.start,
},
messageId: 'genericSpacingMismatch',
node,
})
});
}
},
}
};
},
})
});
28 changes: 16 additions & 12 deletions packages/eslint-plugin-youcan/src/rules/if-newline.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createEslintRule } from '../utils'
import { createEslintRule } from '../utils';

export const RULE_NAME = 'if-newline'
export type MessageIds = 'missingIfNewline'
export type Options = []
export const RULE_NAME = 'if-newline';
export type MessageIds = 'missingIfNewline';
export type Options = [];

export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
Expand All @@ -22,10 +22,14 @@ export default createEslintRule<Options, MessageIds>({
create: (context) => {
return {
IfStatement(node) {
if (!node.consequent)
return
if (node.consequent.type === 'BlockStatement')
return
if (!node.consequent) {
return;
}

if (node.consequent.type === 'BlockStatement') {
return;
}

if (node.test.loc.end.line === node.consequent.loc.start.line) {
context.report({
node,
Expand All @@ -35,11 +39,11 @@ export default createEslintRule<Options, MessageIds>({
},
messageId: 'missingIfNewline',
fix(fixer) {
return fixer.replaceTextRange([node.consequent.range[0], node.consequent.range[0]], '\n')
return fixer.replaceTextRange([node.consequent.range[0], node.consequent.range[0]], '\n');
},
})
});
}
},
}
};
},
})
});
39 changes: 21 additions & 18 deletions packages/eslint-plugin-youcan/src/rules/import-dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createEslintRule } from '../utils'
import { createEslintRule } from '../utils';

export const RULE_NAME = 'import-dedupe'
export type MessageIds = 'importDedupe'
export type Options = []
export const RULE_NAME = 'import-dedupe';
export type MessageIds = 'importDedupe';
export type Options = [];

export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
Expand All @@ -22,12 +22,13 @@ export default createEslintRule<Options, MessageIds>({
create: (context) => {
return {
ImportDeclaration(node) {
if (node.specifiers.length <= 1)
return
if (node.specifiers.length <= 1) {
return;
}

const names = new Set<string>()
const names = new Set<string>();
node.specifiers.forEach((n) => {
const id = n.local.name
const id = n.local.name;
if (names.has(id)) {
context.report({
node,
Expand All @@ -37,19 +38,21 @@ export default createEslintRule<Options, MessageIds>({
},
messageId: 'importDedupe',
fix(fixer) {
const s = n.range[0]
let e = n.range[1]
if (context.getSourceCode().text[e] === ',')
e += 1
return fixer.removeRange([s, e])
const s = n.range[0];
let e = n.range[1];
if (context.getSourceCode().text[e] === ',') {
e += 1;
}

return fixer.removeRange([s, e]);
},
})
});
}
names.add(id)
})
names.add(id);
});

// console.log(node)
},
}
};
},
})
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Ported from https://github.com/gajus/eslint-plugin-canonical/blob/master/src/rules/preferInlineTypeImport.js
// by Gajus Kuizinas https://github.com/gajus
import type { TSESTree } from '@typescript-eslint/utils'
import type { RuleFixer, SourceCode } from '@typescript-eslint/utils/dist/ts-eslint'
import { createEslintRule } from '../utils'
import type { TSESTree } from '@typescript-eslint/utils';
import type { RuleFixer, SourceCode } from '@typescript-eslint/utils/dist/ts-eslint';
import { createEslintRule } from '../utils';

export const RULE_NAME = 'prefer-inline-type-import'
export type MessageIds = 'preferInlineTypeImport'
export type Options = []
export const RULE_NAME = 'prefer-inline-type-import';
export type MessageIds = 'preferInlineTypeImport';
export type Options = [];

export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
Expand All @@ -24,41 +24,44 @@ export default createEslintRule<Options, MessageIds>({
},
defaultOptions: [],
create: (context) => {
const sourceCode = context.getSourceCode()
const sourceCode = context.getSourceCode();

return {
ImportDeclaration: (node) => {
// ignore bare type imports
if (node.specifiers.length === 1 && ['ImportNamespaceSpecifier', 'ImportDefaultSpecifier'].includes(node.specifiers[0].type))
return
if (node.specifiers.length === 1 && ['ImportNamespaceSpecifier', 'ImportDefaultSpecifier'].includes(node.specifiers[0].type)) {
return;
}
if (node.importKind === 'type') {
context.report({
*fix(fixer) {
yield * removeTypeSpecifier(fixer, sourceCode, node)
yield * removeTypeSpecifier(fixer, sourceCode, node);

for (const specifier of node.specifiers)
yield fixer.insertTextBefore(specifier, 'type ')
for (const specifier of node.specifiers) {
yield fixer.insertTextBefore(specifier, 'type ');
}
},
loc: node.loc,
messageId: 'preferInlineTypeImport',
node,
})
});
}
},
}
};
},
})
});

function * removeTypeSpecifier(fixer: RuleFixer, sourceCode: Readonly<SourceCode>, node: TSESTree.ImportDeclaration) {
const importKeyword = sourceCode.getFirstToken(node)
const importKeyword = sourceCode.getFirstToken(node);

const typeIdentifier = sourceCode.getTokenAfter(importKeyword)
const typeIdentifier = sourceCode.getTokenAfter(importKeyword!);

yield fixer.remove(typeIdentifier)
yield fixer.remove(typeIdentifier!);

if (importKeyword.loc.end.column + 1 === typeIdentifier.loc.start.column) {
if (importKeyword!.loc.end.column + 1 === typeIdentifier!.loc.start.column) {
yield fixer.removeRange([
importKeyword.range[1],
importKeyword.range[1] + 1,
])
importKeyword!.range[1],
importKeyword!.range[1] + 1,
]);
}
}
4 changes: 2 additions & 2 deletions packages/eslint-plugin-youcan/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESLintUtils } from '@typescript-eslint/utils'
import { ESLintUtils } from '@typescript-eslint/utils';

export const createEslintRule = ESLintUtils.RuleCreator(
ruleName => ruleName,
)
);
2 changes: 1 addition & 1 deletion packages/eslint-plugin-youcan/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"moduleResolution": "node"
},
"include": [
"./packages/**/*.ts"
"src"
]
}
1 change: 1 addition & 0 deletions packages/vue3/src/components/Checkbox/Checkbox.stories.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { Meta } from '@storybook/vue3';
import Checkbox from './Checkbox.vue';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { Meta } from '@storybook/vue3';
import DateInput_ from './DateInput.vue';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { Meta } from '@storybook/vue3';
import Increment_ from './Increment.vue';

Expand Down
Loading

0 comments on commit cc70281

Please sign in to comment.