feat: custom plugin

This commit is contained in:
Anthony Fu
2022-04-02 23:18:36 +08:00
parent f1644757c8
commit ded3cf2da2
14 changed files with 1172 additions and 39 deletions

View File

@@ -0,0 +1,9 @@
{
"extends": "@antfu",
"plugins": [
"antfu"
],
"rules": {
"antfu/no-leading-newline": "error"
}
}

View File

@@ -0,0 +1,12 @@
import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
entries: [
'src/index',
],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
},
})

View File

@@ -0,0 +1,29 @@
{
"name": "eslint-plugin-antfu",
"version": "0.0.0",
"license": "MIT",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"build": "rimraf dist && unbuild",
"stub": "unbuild --stub",
"prepublishOnly": "nr build"
},
"dependencies": {
"@typescript-eslint/utils": "^5.17.0"
},
"devDependencies": {
"unbuild": "^0.7.0"
}
}

View File

@@ -0,0 +1,7 @@
import noLeadingNewline from './rules/no-leading-newline'
export default {
rules: {
'no-leading-newline': noLeadingNewline,
},
}

View File

@@ -0,0 +1,44 @@
import { createEslintRule } from '../utils'
export const RULE_NAME = 'no-leading-newline'
export type MessageIds = 'noLeadingNewline'
export type Options = []
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Do not allow leading newline',
recommended: 'error',
},
fixable: 'whitespace',
schema: [],
messages: {
noLeadingNewline: 'No leading newline allowed',
},
},
defaultOptions: [],
create: (context) => {
return {
Program(node) {
const code = context.getSourceCode()
const match = code.text.match(/^[\s]+/)?.[0] || ''
if (match.includes('\n')) {
const line = match.split('\n')
context.report({
node,
loc: {
start: { line: 0, column: 0 },
end: { line: line.length - 1, column: line[line.length - 1].length },
},
messageId: 'noLeadingNewline',
fix(fixer) {
return fixer.replaceTextRange([0, match.length], '')
},
})
}
},
}
},
})

View File

@@ -0,0 +1,5 @@
import { ESLintUtils } from '@typescript-eslint/utils'
export const createEslintRule = ESLintUtils.RuleCreator(
ruleName => ruleName,
)