admin管理员组文章数量:1406039
I am looking for an exception to the no-undef
rule that will permit undeclared globals matching a naming rule. In this case, a regex like [A-Z][a-z]*Model
should be allowed, so "CustomerModel" and "PatientModel" would all be allowed, because it's too cumbersome to place /* global CustomerModel */ in every unit and too cumbersome to list every *Model
global even in the eslint global configuration.
I would like to have a rule like this:
"rules": {
"no-undef": [2, "[A-Z][a-z]*Model"],
Where the above syntax is invented by me and I hope obviously means "only plain when the above reg-expression name is not matched."
Alternatively if there is a way to specify regular expression matching in the .eslintrc file globals list.
I am looking for an exception to the no-undef
rule that will permit undeclared globals matching a naming rule. In this case, a regex like [A-Z][a-z]*Model
should be allowed, so "CustomerModel" and "PatientModel" would all be allowed, because it's too cumbersome to place /* global CustomerModel */ in every unit and too cumbersome to list every *Model
global even in the eslint global configuration.
I would like to have a rule like this:
"rules": {
"no-undef": [2, "[A-Z][a-z]*Model"],
Where the above syntax is invented by me and I hope obviously means "only plain when the above reg-expression name is not matched."
Alternatively if there is a way to specify regular expression matching in the .eslintrc file globals list.
Share Improve this question asked Sep 27, 2016 at 20:00 Warren PWarren P 69.3k40 gold badges192 silver badges321 bronze badges 4-
If you use a JavaScript config file, does the
/[A-Z][a-z]*Model/
regex syntax work? – ssube Commented Sep 27, 2016 at 20:01 - So rename my .eslintrc basically? – Warren P Commented Sep 27, 2016 at 20:31
-
That's what I would try. Some (most?) tools just load their config with
require
, so you can often use a JS file instead of JSON. It's great for returning different configs if you do theenv == PROD
thing from Rails, but obviously allows real RegExps as well. It's just a matter of whether the consuming tool will recognize and use them. – ssube Commented Sep 27, 2016 at 20:33 - eslintrc.js like this, doesn't work: module.exports = { "extends": "eslint:remended", "env": { "browser": true }, "globals": { /[A-Z][a-z]*Model/: true, ... – Warren P Commented Sep 27, 2016 at 20:44
1 Answer
Reset to default 5Well, you could create your own rule, if you feel like it.
The source of the no-undef rule is pretty short. Probably you will need to replace the condition defined there
if (!considerTypeOf && hasTypeOfOperator(identifier)) {
return;
}
with something like
if (/Model$/.test(identifier.name) || !considerTypeOf && hasTypeOfOperator(identifier)) {
return;
}
To make sure that global identifiers ending with Model
won't trigger the error.
You may also want to parameterize the identifier format rather than having it hard coded in the source. Since you are an experienced programmer, you will certainly be able to figure out the details and caveats of this approach yourself.
This is probably the most simple part of the job. It still takes some machinery to get your custom rule to work. More infos on how to create custom rules can be found here: http://eslint/docs/developer-guide/working-with-rules.
本文标签:
版权声明:本文标题:javascript - Can I create an ESLint rule that will allow all globals matching a regular expression? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744957980a2634473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论