网站后台数据库设计,上海何鹏seo,外贸网站建设评价,石材公司网站一、ESLint介绍
1.为什么要用ESLint
统一团队编码规范#xff08;命名#xff0c;格式等#xff09;
统一语法
减少git不必要的提交
减少低级错误
在编译时检查语法#xff0c;而不是等js引擎运行时才检查
2.eslint用法
可以手动下载配置
可以通过vue脚手架创建项…一、ESLint介绍
1.为什么要用ESLint
统一团队编码规范命名格式等
统一语法
减少git不必要的提交
减少低级错误
在编译时检查语法而不是等js引擎运行时才检查
2.eslint用法
可以手动下载配置
可以通过vue脚手架创建项目时自动下载
3.ESLint包
安装方式
通过npm直接进行全局安装npm i eslint -D
通过vue脚手架创建项目时选择安装eslint模块包vue create 创建项目时选择eslint安装的包
vscode中ESLint扩展工具
二、手动下载配置(js)
1.创建一个测试文件夹eslint-test
2.初始化项目npm init -y(创建package.json)
3.直接在项目中安装eslint包npm i eslint -D node_modules中下载了很多包.bin/eslint.cmd脚本文件内部通过nodejs执行eslint运行包的代码eslint包用来规范eslint配置文件eslint开头的包是eslint运行包包含eslint代码。
4.生成ESLint配置文件
创建eslint.config.js文件
export default {rules: {no-unused-vars: error,no-console: error,no-sparse-arrays: error,no-undef: error,no-unreachable: error,no-dupe-keys: error}
}
在package.json文件中添加type属性添加命令
{name: eslint-test,version: 1.0.0,main: index.js,type: module,scripts: {lint: eslint .},keywords: [],author: ,license: ISC,description: ,dependencies: {eslint: 9.14.0}
} 创建js文件src/index.js
//不允许变量声明但没有使用no-unused-vars
const name zs//不允许打印no-console
console.log(name);//不允许数组中有空元素no-sparse-arrays
const arr [1, , 3]//不允许有未声明的变量no-undef
console.log(afffffff);//不允许函数return后还有代码no-unreachable
function showFn(toName, fromName) {let a 0;return a;a 1
}//不允许对象有重复的key,no-dupe-keys
const obj {name: zs,name: zs1
} 终端执行命令npm run lint规范代码 ESLint可以创建独立的配置文件.eslintrc.js也可以直接在package.json中配置
a.执行node_modules/.bin文件夹里的eslint脚本来创建配置文件
包含完整脚本路径的命令./node_modules/.bin/eslint --init
也可以用npx来执行npxeslint --init
创建配置文件过程中需要选择配置 自动生成eslint.config.mjs文件
eslint.config.mjs
import globals from globals;
import pluginJs from eslint/js;/** type {import(eslint).Linter.Config[]} */
export default [{files: [**/*.js], languageOptions: {sourceType: commonjs}},{languageOptions: { globals: globals.browser }},pluginJs.configs.recommended,
];
创建js文件输入npx eslint 文件名执行语法检查 5.规范集简化配置npm ieslint/js
// export default {
// rules: {
// no-unused-vars: error,
// no-console: error,
// no-sparse-arrays: error,
// no-undef: error,
// no-unreachable: error,
// no-dupe-keys: error
// }
// }
//规则集
import js from eslint/js
export default [js.configs.recommended]
三、手动安装eslint(ts)
1.创建一个测试文件夹eslint-ts-test
2.初始化项目npm init -y(创建package.json)
3.直接在项目中安装eslint包npm i eslint eslint/js typescript-eslint/parser -D
{name: pro,version: 1.0.0,main: index.js,type: module,scripts: {lint: eslint .},keywords: [],author: ,license: ISC,description: ,devDependencies: {eslint/js: 9.14.0,eslint: 9.14.0,typescript-eslint/parser: 8.14.0}
} 4.新建配置文件eslint.config.js文件
import tsPsrser from typescript-eslint/parserexport default {//文件配置哪些文件需要被校验,忽略eslint.config.js文件ignores: [eslint.config.js],files: [**/*.ts],//规范配置rules: {no-unused-vars: error,no-console: error},//语言配置languageOptions: {//指定解析器parser: tsPsrser}
} 5.创建ts文件src/index.ts
const age18
console.log(name)/*ts类型定义
*ts相关的校验eslint自带的校验espress解析器无法识别
*我们需要ts解析器来解析ts代码完成类型校验
*/
interface Uesr{name:string;age:number;
} Parsing error: The keyword interface is reserved
ts相关的校验eslint自带的校验espress解析器无法识别
我们需要ts解析器来解析ts代码完成类型校验 npm i typescript-eslint/parser
四、手动安装eslint(vue)
1.创建一个测试文件夹eslint-ts-test
2.初始化项目npm init -y(创建package.json)
3.直接在项目中安装eslint包npm i eslint eslint/js typescript-eslint/parser vue-eslint-parser -D
{name: eslint-test,version: 1.0.0,main: index.js,type: module,scripts: {lint: eslint .},keywords: [],author: ,license: ISC,description: ,dependencies: {eslint/js: 9.14.0,typescript-eslint/parser: 8.14.0,eslint: 9.14.0,vue-eslint-parser: 9.4.3}
} 4.新建配置文件eslint.config.js文件
import tsParser from typescript-eslint/parser
import vueParser from vue-eslint-parser
export default {ignores: [eslint.config.js],files: [**/*.ts, **/*.vue],rules: {no-unused-vars: error,no-console: error,no-sparse-arrays: error,no-undef: error,no-unreachable: error,no-dupe-keys: error},languageOptions: {//指定解析器parser: vueParser,//解析器的语法parser设置parserOptions: {parser: tsParser}}
} 5.创建vue文件src/index.vue
template/template
script setup langts
//不允许变量声明但没有使用no-unused-vars
const name zs//不允许打印no-console
console.log(name);//不允许数组中有空元素no-sparse-arrays
const arr [1, , 3]//不允许有未声明的变量no-undef
console.log(afffffff);//不允许函数return后还有代码no-unreachable
function showFn(toName, fromName) {let a 0;return a;a 1
}//不允许对象有重复的key,no-dupe-keys
const obj {name: zs,name: zs1
}//类型定义
interface User{name:string;age:number;
}
/script 五、自定义插件 1.rule定义
针对这个规范的需求编写一个rule原理是通过ast节点处理来完成
//规则的本质是一个对象
//eslint插件必须长得像一个约定好的对象
export const noMiaomiVars {//插件的元信息meta: {messages: {noMiaomiVars: 不允许使用miaomi变量}},create(context) {return {// 这是一个访问者模式访问到某一个ast的节点就进行处理VariableDeclaration(node) {console.log(VariableDeclaration, node);},VariableDeclarator(node) {console.log(VariableDeclarator, node);},Identifier(node) {console.log(Identifier, node);if (node.name miaomi) {context.report({node,messageId: noMiaomiVars,data: {name: node.name}})}},Literal(node) {console.log(Identifier, node);}}}
}
2.plugin插件定义
将rule进行插件化提供给外部使用‘
import { noMiaomiVars } from ../rules/no-miaomi-vars.js
export const eslintMiaomiPlugin {rules: {no-miaomi-vars: noMiaomiVars}
}
3.use将插件引入到eslint配置文件中使用插件
import tsParser from typescript-eslint/parser
import vueParser from vue-eslint-parser
import { eslintMiaomiPlugin } from ./eslint/plugins/eslint-plugin-miaomi.js
export default {ignores: [eslint.config.js],files: [src/**/*.js, **/*.ts, **/*.vue],plugins: {miaomi: eslintMiaomiPlugin //插件定义好后插件名称就是规则的作用域},rules: {miaomi/no-miaomi-vars: error,},languageOptions: {//指定解析器parser: vueParser,//解析器的语法parser设置parserOptions: {parser: tsParser}}
}
文章转载自: http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.jtszm.cn.gov.cn.jtszm.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn