Skip to content

Commit

Permalink
style: add import and stricter lint (element-plus#3440)
Browse files Browse the repository at this point in the history
* style: add import lint

* chore: apply eslint rules

* chore: add stricter lint

* chore: lint all files

* auto fix

* manually fix

* restore build-indices.ts
  • Loading branch information
sxzz authored Sep 17, 2021
1 parent 1615b9e commit 0636e1e
Show file tree
Hide file tree
Showing 147 changed files with 669 additions and 387 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
packages/*/es
packages/*/lib
57 changes: 51 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ module.exports = {
browser: true,
node: true,
},
plugins: ['@typescript-eslint', 'prettier'],
globals: {
jest: 'readonly',
},
plugins: ['@typescript-eslint', 'prettier', 'import'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:vue/vue3-recommended',
'prettier',
Expand All @@ -26,7 +30,6 @@ module.exports = {
},
},
{
// not tested
files: ['**/__tests__/**'],
rules: {
'no-console': 'off',
Expand All @@ -44,6 +47,9 @@ module.exports = {
camelcase: ['error', { properties: 'never' }],

'no-var': 'error',
'no-empty': ['error', { allowEmptyCatch: true }],
'no-with': 'error',
'no-void': 'error',
'prefer-const': [
'warn',
{ destructuring: 'all', ignoreReadBeforeAssign: true },
Expand All @@ -55,20 +61,59 @@ module.exports = {
{ ignoreConstructors: false, avoidQuotes: true },
],
'block-scoped-var': 'error',
complexity: ['off', 11],
'no-with': 'error',
'no-void': 'error',
'no-constant-condition': ['error', { checkLoops: false }],

'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
'@typescript-eslint/consistent-type-imports': [
'error',
{ disallowTypeAnnotations: false },
],

// vue
'vue/no-v-html': 'off',
'vue/require-default-prop': 'off',
'vue/require-explicit-emits': 'off',

'prettier/prettier': 'warn',
'prettier/prettier': 'error',

// import
'import/first': 'error',
'import/no-duplicates': 'error',
'import/order': [
'error',
{
groups: [
'builtin',
'external',
'internal',
'parent',
'sibling',
'index',
'object',
'type',
],

pathGroups: [
{
pattern: 'vue',
group: 'external',
position: 'before',
},
{
pattern: '@vue/**',
group: 'external',
position: 'before',
},
{
pattern: '@element-plus/**',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: ['type'],
},
],
},
}
17 changes: 7 additions & 10 deletions build/build-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import helper from 'components-helper'
import path from 'path'
import helper from 'components-helper'
import { epRoot } from './paths'

const { name, version } = require(path.resolve(epRoot, './package.json'))
Expand Down Expand Up @@ -30,24 +30,21 @@ helper({
})

function reComponentName(title) {
return (
'el-' +
title
.replace(/\B([A-Z])/g, '-$1')
.replace(/[ ]+/g, '-')
.toLowerCase()
)
return `el-${title
.replace(/\B([A-Z])/g, '-$1')
.replace(/[ ]+/g, '-')
.toLowerCase()}`
}

function reDocUrl(fileName, header) {
const docs = 'https://element-plus.org/#/en-US/component/'
const _header = header
? header.replace(/[ ]+/g, '-').toLowerCase()
: undefined
return docs + fileName + (_header ? '#' + _header : '')
return docs + fileName + (_header ? `#${_header}` : '')
}

function reAttribute(value, key, item) {
function reAttribute(value, key /* , item */) {
const _value = value.match(/^\*\*(.*)\*\*$/)
const str = _value ? _value[1] : value

Expand Down
121 changes: 60 additions & 61 deletions build/build-indices.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict'
// TODO: implement this feature

import fs from 'fs'
import algoliasearch from 'algoliasearch'
import fg from 'fast-glob'
// import fs from 'fs'
// import algoliasearch from 'algoliasearch'
// import fg from 'fast-glob'

interface Index {
component: string
title: string
anchor: string
content: string
sort: number
path: string
}
// interface Index {
// component: string
// title: string
// anchor: string
// content: string
// sort: number
// path: string
// }

const algoliaKey = process.env.ALGOLIA_KEY!
// const algoliaKey = process.env.ALGOLIA_KEY!

const client = algoliasearch('7DCTSU0WBW', algoliaKey)
const langs = {
'zh-CN': 'element-zh',
'en-US': 'element-en',
es: 'element-es',
'fr-FR': 'element-fr',
jp: 'element-jp',
}
;['zh-CN', 'en-US', 'es', 'fr-FR', 'jp'].forEach((lang) => {
const indexName = langs[lang]
const index = client.initIndex(indexName)
index
.clearObjects()
.then(() => {
const files = fg.sync(`website/docs/${lang}/*.md`)
let indices: Index[] = []
files.forEach((file) => {
const regExp = new RegExp(`website\/docs\/${lang}\/(.*).md`)
const pathContent = file.match(regExp)!
const path = pathContent[1]
const index = path.lastIndexOf('/')
const names = index !== -1 ? path.split('/') : []
const component = names.length ? names[names.length - 1] : path
const content = fs.readFileSync(file, 'utf8')
const matches = content
.replace(/:::[\s\S]*?:::/g, '')
.replace(/```[\s\S]*?```/g, '')
.match(/#{2,4}[^#]*/g)!
.map((match) =>
match
.replace(/\n+/g, '\n')
.split('\n')
.filter((part) => !!part)
)
})
// const client = algoliasearch('7DCTSU0WBW', algoliaKey)
// const langs = {
// 'zh-CN': 'element-zh',
// 'en-US': 'element-en',
// es: 'element-es',
// 'fr-FR': 'element-fr',
// jp: 'element-jp',
// }
// ;['zh-CN', 'en-US', 'es', 'fr-FR', 'jp'].forEach((lang) => {
// const indexName = langs[lang]
// const index = client.initIndex(indexName)
// index
// .clearObjects()
// .then(() => {
// const files = fg.sync(`website/docs/${lang}/*.md`)
// let indices: Index[] = []
// files.forEach((file) => {
// const regExp = new RegExp(`website\/docs\/${lang}\/(.*).md`)
// const pathContent = file.match(regExp)!
// const path = pathContent[1]
// const index = path.lastIndexOf('/')
// const names = index !== -1 ? path.split('/') : []
// const component = names.length ? names[names.length - 1] : path
// const content = fs.readFileSync(file, 'utf8')
// const matches = content
// .replace(/:::[\s\S]*?:::/g, '')
// .replace(/```[\s\S]*?```/g, '')
// .match(/#{2,4}[^#]*/g)!
// .map((match) =>
// match
// .replace(/\n+/g, '\n')
// .split('\n')
// .filter((part) => !!part)
// )
// })

index
.saveObjects(indices, {
autoGenerateObjectIDIfNotExist: true,
})
.catch((e) => {
console.log(e)
})
})
.catch((e) => {
console.log(e)
})
})
// index
// .saveObjects(indices, {
// autoGenerateObjectIDIfNotExist: true,
// })
// .catch((e) => {
// console.log(e)
// })
// })
// .catch((e) => {
// console.log(e)
// })
// })
5 changes: 3 additions & 2 deletions build/crowdin-credentials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import fs from 'fs'
import chalk from 'chalk'
import { errorAndExit } from './utils'

const credentialPlaceholder = 'API_TOKEN_PLACEHOLDER'

Expand All @@ -18,7 +19,7 @@ const CREDENTIAL = process.env.CROWDIN_TOKEN
file.replace(credentialPlaceholder, CREDENTIAL!)
)
console.info(chalk.green('Crowdin credential update successfully'))
} catch (e) {
throw e
} catch (e: any) {
errorAndExit(e)
}
})()
4 changes: 2 additions & 2 deletions build/full-bundle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import fs from 'fs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import rollup from 'rollup'
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import commonjs from '@rollup/plugin-commonjs'
import vue from 'rollup-plugin-vue'
import esbuild from 'rollup-plugin-esbuild'
Expand Down
9 changes: 5 additions & 4 deletions build/gen-dts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import path from 'path'
import fs from 'fs'
import { Project, SourceFile } from 'ts-morph'
import vueCompiler from '@vue/compiler-sfc'
import { Project } from 'ts-morph'
import { sync as globSync } from 'fast-glob'
import chalk from 'chalk'
import type { SourceFile } from 'ts-morph'

const TSCONFIG_PATH = path.resolve(__dirname, '../tsconfig.json')

Expand Down Expand Up @@ -130,9 +131,9 @@ const genVueTypes = async (
)
console.log(
chalk.green(
'Definition for file: ' +
chalk.bold(sourceFile.getBaseName()) +
' generated'
`Definition for file: ${chalk.bold(
sourceFile.getBaseName()
)} generated`
)
)
}
Expand Down
11 changes: 6 additions & 5 deletions build/gen-entry-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import path from 'path'
import fs from 'fs'
import chalk from 'chalk'
import glob from 'fast-glob'
import { Project, SourceFile } from 'ts-morph'
import { Project } from 'ts-morph'
import { epRoot, buildOutput } from './paths'
import type { SourceFile } from 'ts-morph'
const TSCONFIG_PATH = path.resolve(__dirname, '../tsconfig.dts.json')

const gen = async () => {
const files = await glob(epRoot + '/*.ts')
const files = await glob(`${epRoot}/*.ts`)
const project = new Project({
compilerOptions: {
allowJs: true,
Expand Down Expand Up @@ -51,9 +52,9 @@ const gen = async () => {
)
console.log(
chalk.green(
'Definition for file: ' +
chalk.bold(sourceFile.getBaseName()) +
' generated'
`Definition for file: ${chalk.bold(
sourceFile.getBaseName()
)} generated`
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion build/gulpfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import gulp from 'gulp'
import ts from 'gulp-typescript'
import path from 'path'
import through2 from 'through2'

const output = path.resolve(__dirname, '../dist/styles')
Expand Down
4 changes: 2 additions & 2 deletions build/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import vue from 'rollup-plugin-vue'
import css from 'rollup-plugin-css-only'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import esbuild from 'rollup-plugin-esbuild'
import path from 'path'
import { getPackagesSync } from '@lerna/project'
import pkg from '../package.json'

Expand Down Expand Up @@ -61,7 +61,7 @@ export default inputs.map((name: string) => ({
return (
/^vue/.test(id) ||
/^@element-plus/.test(id) ||
deps.some((k) => new RegExp('^' + k).test(id))
deps.some((k) => new RegExp(`^${k}`).test(id))
)
},
}))
2 changes: 1 addition & 1 deletion build/size-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk'
import { FileSizeReporter } from 'rollup-plugin-filesize'
import type { FileSizeReporter } from 'rollup-plugin-filesize'

const reporter: FileSizeReporter = (opt, outputOptions, info) => {
const values = [
Expand Down
2 changes: 1 addition & 1 deletion build/update-version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import chalk from 'chalk'
import { epRoot } from './paths'

const tagVersion = process.env.TAG_VERSION
Expand Down
Loading

0 comments on commit 0636e1e

Please sign in to comment.