最近折腾了一番,总算把Eslint插件在离线的情况下安装好了。之前查了挺多,但是很多方法还是在没有完全离线的情况下进行的。之所以想完全离线安装,主要是因为我们工作的地方是禁止访问外网的,所以像直接执行npm命令来进行一些包的安装,其实对我来说是用不了的。
好了,现在进入正题,说下我的解决方式吧。
1.在无法联网的电脑上先把VSCode安装好。
C:\Users\Administrator\.vscode\extensions)下,
把eslint插件 的文件夹 (dbaeumer.vscode-eslint-1.2.11)直接拷贝出来,放到无法联网的电脑对应的目录。
3.有网的和无法连网电脑上都安装nodeJs。安装这个的目的是可以使用接下来要使用到的nmp库以及nmp命令(无法连网的不用使用到这个命令)。
4.在有网的电脑上的命令行界面执行以下的命令:
npm i eslint -g
npm eslint init
npm install eslint-plugin-html -g
npm install babel-eslint -g
这些命令执行的结果都是把这些对应的包下载到本地的nmp库中,以供后面启动Eslint插件的时候使用。(-g是全局安装的意思)
C:\Users\Administrator\AppData\Roaming\npm)下的所有文件拷贝到无法联网的电脑对应的目录上就行了。
.eslintrc.json(最前面有个点的,看清楚啦!我就被这个坑过。),要把它放到要使用eslint的项目根目录下。代码在后面提供了,可直接使用。
6.重新启动VSCode,就可以正常使用上Eslint插件啦。
1 {
2 "plugins": [
3 // "react",
4 "html"
5 ],
6 "env": {
7 "node": true,
8 "jquery": true,
9 "es6": true,
10 "browser": true
11 },
12 "globals": {
13 "angular": false
14 },
15 "parser": "babel-eslint",
16 "rules": {
17 //官方文档 http://eslint.org/docs/rules/
18 //参数:0 关闭,1 警告,2 错误
19 // "quotes": [0, "single"], //建议使用单引号
20 // "no-inner-declarations": [0, "both"], //不建议在{}代码块内部声明变量或函数
21 "no-extra-boolean-cast": 1, //多余的感叹号转布尔型
22 "no-extra-semi": 1, //多余的分号
23 "no-extra-parens": 0, //多余的括号
24 "no-empty": 1, //空代码块
25
26 //使用前未定义
27 "no-use-before-define": [
28 0,
29 "nofunc"
30 ],
31
32 "complexity": [0, 10], //圈复杂度大于*
33
34 //定义数组或对象最后多余的逗号
35 "comma-dangle": [
36 0,
37 "never"
38 ],
39
40 // 不允许对全局变量赋值,如 window = 'abc'
41 "no-global-assign": ["error", {
42 // 定义例外
43 // "exceptions": ["Object"]
44 }],
45 "no-var": 0, //用let或const替代var
46 "no-const-assign": 2, //不允许const重新赋值
47 "no-class-assign": 2, //不允许对class重新赋值
48 "no-debugger": 1, //debugger 调试代码未删除
49 "no-console": 0, //console 未删除
50 "no-constant-condition": 2, //常量作为条件
51 "no-dupe-args": 2, //参数重复
52 "no-dupe-keys": 2, //对象属性重复
53 "no-duplicate-case": 2, //case重复
54 "no-empty-character-class": 2, //正则无法匹配任何值
55 "no-invalid-regexp": 2, //无效的正则
56 "no-func-assign": 2, //函数被赋值
57 "valid-typeof": 1, //无效的类型判断
58 "no-unreachable": 2, //不可能执行到的代码
59 "no-unexpected-multiline": 2, //行尾缺少分号可能导致一些意外情况
60 "no-sparse-arrays": 1, //数组中多出逗号
61 "no-shadow-restricted-names": 2, //关键词与命名冲突
62 "no-undef": 1, //变量未定义
63 "no-unused-vars": 1, //变量定义后未使用
64 "no-cond-assign": 2, //条件语句中禁止赋值操作
65 "no-native-reassign": 2, //禁止覆盖原生对象
66 "no-mixed-spaces-and-tabs": 0,
67
68
69
70 //代码风格优化 --------------------------------------
71 "no-irregular-whitespace": 0,
72 "no-else-return": 0, //在else代码块中return,else是多余的
73 "no-multi-spaces": 0, //不允许多个空格
74
75 //object直接量建议写法 : 后一个空格前面不留空格
76 "key-spacing": [
77 0,
78 {
79 "beforeColon": false,
80 "afterColon": true
81 }
82 ],
83
84 "block-scoped-var": 1, //变量应在外部上下文中声明,不应在{}代码块中
85 "consistent-return": 1, //函数返回值可能是不同类型
86 "accessor-pairs": 1, //object getter/setter方法需要成对出现
87
88 //换行调用对象方法 点操作符应写在行首
89 "dot-location": [
90 1,
91 "property"
92 ],
93 "no-lone-blocks": 1, //多余的{}嵌套
94 "no-labels": 1, //无用的标记
95 "no-extend-native": 1, //禁止扩展原生对象
96 "no-floating-decimal": 1, //浮点型需要写全 禁止.1 或 2.写法
97 "no-loop-func": 1, //禁止在循环体中定义函数
98 "no-new-func": 1, //禁止new Function(...) 写法
99 "no-self-compare": 1, //不允与自己比较作为条件
100 "no-sequences": 1, //禁止可能导致结果不明确的逗号操作符
101 "no-throw-literal": 1, //禁止抛出一个直接量 应是Error对象
102
103 //不允return时有赋值操作
104 "no-return-assign": [
105 1,
106 "always"
107 ],
108
109 //不允许重复声明
110 "no-redeclare": [
111 1,
112 {
113 "builtinGlobals": true
114 }
115 ],
116
117 //不执行的表达式
118 "no-unused-expressions": [
119 0,
120 {
121 "allowShortCircuit": true,
122 "allowTernary": true
123 }
124 ],
125 "no-useless-call": 1, //无意义的函数call或apply
126 "no-useless-concat": 1, //无意义的string concat
127 "no-void": 1, //禁用void
128 "no-with": 1, //禁用with
129 "space-infix-ops": 0, //操作符前后空格
130
131 //jsdoc
132 "valid-jsdoc": [
133 0,
134 {
135 "requireParamDescription": true,
136 "requireReturnDescription": true
137 }
138 ],
139
140 //标记未写注释
141 "no-warning-comments": [
142 1,
143 {
144 "terms": [
145 "todo",
146 "fixme",
147 "any other term"
148 ],
149 "location": "anywhere"
150 }
151 ],
152 "curly": 0 //if、else、while、for代码块用{}包围
153 }
154 }
.eslintrc.json