-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/NEWCLICKUI-4391 add new rule #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aychernov
wants to merge
2
commits into
master
Choose a base branch
from
feat/NEWCLICKUI-4391
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ const RULE_USE_MIXINS = 'stylelint-core-vars/use-mixins'; | |
const RULE_USE_ONE_OF_MIXINS = 'stylelint-core-vars/use-one-of-mixins'; | ||
const RULE_DO_NOT_USE_DARK_COLORS = 'stylelint-core-vars/do-not-use-dark-colors'; | ||
const RULE_DO_NOT_USE_OLD_VARS = 'stylelint-core-vars/do-not-use-old-vars'; | ||
const RULE_NO_USELESS_VARS_IMPORT = 'stylelint-core-vars/no-useless-vars-import'; | ||
|
||
const messages = { | ||
[RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, { | ||
|
@@ -59,6 +60,11 @@ const messages = { | |
return 'Do not use dark colors directly. Only light and static colors are allowed'; | ||
}, | ||
}), | ||
[RULE_NO_USELESS_VARS_IMPORT]: stylelint.utils.ruleMessages(RULE_NO_USELESS_VARS_IMPORT, { | ||
expected: () => { | ||
return 'Do not import vars.css unless you use a @mixin or @include'; | ||
}, | ||
}), | ||
}; | ||
|
||
const checkVars = (decl, result, context, ruleName, matcher, shouldReport, options = {}) => { | ||
|
@@ -81,7 +87,7 @@ const checkVars = (decl, result, context, ruleName, matcher, shouldReport, optio | |
|
||
const originalValueIndex = previousValues.reduce( | ||
(acc, sub) => (acc > sub.index + sub.diff ? acc - sub.diff : acc), | ||
substitution.index | ||
substitution.index, | ||
); | ||
|
||
if (shouldReport(substitution.fixable, fixed)) { | ||
|
@@ -91,7 +97,7 @@ const checkVars = (decl, result, context, ruleName, matcher, shouldReport, optio | |
message: messages[ruleName].expected( | ||
substitution.variables, | ||
substitution.value, | ||
substitution.fixable | ||
substitution.fixable, | ||
), | ||
node: decl, | ||
word: value, | ||
|
@@ -155,7 +161,7 @@ const checkTypography = (rule, result, context, ruleName) => { | |
} | ||
}; | ||
|
||
const checkDarkColorsUsage = (decl, result, context, ruleName) => { | ||
const checkDarkColorsUsage = (decl, result) => { | ||
const { prop, raws } = decl; | ||
|
||
let value = toOneLine(decl.value); | ||
|
@@ -174,6 +180,46 @@ const checkDarkColorsUsage = (decl, result, context, ruleName) => { | |
} | ||
}; | ||
|
||
const checkVarsImportUsage = (root, result, context, ruleName) => { | ||
const isVarsCssImport = (params) => /\/?vars\.css(['"])?\s*;?$/.test(params); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Может стоит ограничить конкретным местом? Какая мотивация ограничения для всех |
||
|
||
let hasVarsImport = false; | ||
let usesMixinOrInclude = false; | ||
const importNodes = []; | ||
|
||
root.walkAtRules((atRule) => { | ||
if (atRule.name === 'import' && isVarsCssImport(atRule.params)) { | ||
hasVarsImport = true; | ||
importNodes.push(atRule); | ||
} | ||
|
||
if ( | ||
atRule.name === 'mixin' || | ||
atRule.name === 'include' || | ||
(atRule.name === 'use' && /mixin/.test(atRule.params)) | ||
) { | ||
usesMixinOrInclude = true; | ||
} | ||
}); | ||
|
||
if (hasVarsImport && !usesMixinOrInclude) { | ||
importNodes.forEach((atRule) => { | ||
if (context.fix) { | ||
atRule.remove(); | ||
} else { | ||
stylelint.utils.report({ | ||
result, | ||
ruleName, | ||
message: messages[RULE_NO_USELESS_VARS_IMPORT].expected(), | ||
node: atRule, | ||
word: 'vars.css', | ||
index: atRule.params.indexOf('vars.css'), | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = [ | ||
stylelint.createPlugin(RULE_USE_VARS, (enabled, config, context) => { | ||
if (!enabled || !VARS_AVAILABLE) { | ||
|
@@ -191,7 +237,7 @@ module.exports = [ | |
RULE_USE_VARS, | ||
findVars, | ||
(fixable, fixed) => fixable && !fixed, | ||
{ allowNumericValues } | ||
{ allowNumericValues }, | ||
); | ||
}); | ||
}; | ||
|
@@ -212,7 +258,7 @@ module.exports = [ | |
RULE_USE_ONE_OF_VARS, | ||
findVars, | ||
(fixable, fixed) => !fixable && !fixed, | ||
{ allowNumericValues } | ||
{ allowNumericValues }, | ||
); | ||
}); | ||
}; | ||
|
@@ -230,7 +276,7 @@ module.exports = [ | |
context, | ||
RULE_DO_NOT_USE_OLD_VARS, | ||
findOldVars, | ||
(_, fixed) => !fixed | ||
(_, fixed) => !fixed, | ||
); | ||
}); | ||
}; | ||
|
@@ -268,6 +314,15 @@ module.exports = [ | |
}); | ||
}; | ||
}), | ||
stylelint.createPlugin(RULE_NO_USELESS_VARS_IMPORT, (enabled, _, context) => { | ||
if (!enabled || !VARS_AVAILABLE) { | ||
return () => {}; | ||
} | ||
|
||
return (root, result) => { | ||
checkVarsImportUsage(root, result, context, RULE_NO_USELESS_VARS_IMPORT); | ||
}; | ||
}), | ||
]; | ||
|
||
module.exports.messages = messages; | ||
|
@@ -277,3 +332,4 @@ module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS; | |
module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS; | ||
module.exports.RULE_DO_NOT_USE_DARK_COLORS = RULE_DO_NOT_USE_DARK_COLORS; | ||
module.exports.RULE_DO_NOT_USE_OLD_VARS = RULE_DO_NOT_USE_OLD_VARS; | ||
module.exports.RULE_NO_USELESS_VARS_IMPORT = RULE_NO_USELESS_VARS_IMPORT; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А оно точно везде useless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В контексте названия имеешь ввиду? Там где срабатывает - да, а где vars импорят совместно с миксинами, правило же как раз и не будет ругаться. Или на что-то лучше заменить?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я про то что во всех ли проектах компании это так, библиотека же глобальная
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reme3d2y @SiebenSieben Привет!
Подскажите пожалуйста, ок ли вам по такому правилу?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нам бы обновить кодоунеров, reme3d2y уже с нами не работает, и я по факту не занимаюсь поддержкой линтера
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
По вопросу не уверен, что везде будет ок, предлагаю спросить в рокетчате javascript