Skip to content
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

rule: Max lines of code #12

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# stylelint-plugins
# stylelint-plugins

TODO: note about only use on standard CS, do not use non-standard CSS
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import max_selector_complexity from './rules/max-selector-complexity/index.js'
import no_unused_custom_properties from './rules/no-unused-custom-properties/index.js'
import no_property_browserhacks from './rules/no-property-browserhacks/index.js'
import max_lines_of_code from './rules/max-lines-of-code/index.js'

export default [
max_selector_complexity,
max_lines_of_code,
no_unused_custom_properties,
no_property_browserhacks
]
1 change: 1 addition & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import rules from "./index.js";
test('exports an array of stylelint rules', () => {
assert.equal(rules.map(rule => rule.ruleName), [
'project-wallace/max-selector-complexity',
'project-wallace/max-lines-of-code',
'project-wallace/no-unused-custom-properties',
'project-wallace/no-property-browserhacks'
])
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions src/rules/max-lines-of-code/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import stylelint from "stylelint";
import { analyze } from '../../analyzer.modern.js'

const { createPlugin, utils } = stylelint;

const rule_name = "project-wallace/max-lines-of-code";

const messages = utils.ruleMessages(rule_name, {
rejected: ({ actual, expected }) => `Counted ${actual} Lines of Code which is greater than the allowed ${expected}`,
});

const meta = {
url: "https://github.com/projectwallace/stylelint-plugins",
};

/** @type {import('stylelint').Rule<string>} */
const ruleFunction = (primaryOption) => {
return (root, result) => {
const validOptions = utils.validateOptions(result, rule_name, {
actual: primaryOption,
allowEmpty: false,
possible: [Number],
check: (value) => Number.isInteger(value) && value > 0,
});

if (!validOptions) {
return;
}

if (primaryOption <= 0) {
return
}

let analysis = analyze(root.source.input.css)
let actual = analysis.stylesheet.sourceLinesOfCode

if (actual > primaryOption) {
utils.report({
message: messages.rejected({ expected: primaryOption, actual }),
node: root,
result,
ruleName: rule_name,
});
}
};
};

ruleFunction.ruleName = rule_name;
ruleFunction.messages = messages;
ruleFunction.meta = meta;

export default createPlugin(rule_name, ruleFunction);
85 changes: 85 additions & 0 deletions src/rules/max-lines-of-code/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import stylelint from 'stylelint';
import { test } from 'uvu'
import * as assert from 'uvu/assert'

const rule_name = 'project-wallace/max-lines-of-code'
const rule_path = './src/rules/max-lines-of-code/index.js'

test('should not run when config is set to a value lower than 0', async () => {
const config = {
plugins: [rule_path],
rules: {
[rule_name]: -1,
},
};

const {
results: [{ warnings, errored }],
} = await stylelint.lint({
code: `a {}`,
config,
});

assert.is(errored, false)
assert.equal(warnings, [])
});

test('should not error on a very simple stylesheet with max-lines=2', async () => {
const config = {
plugins: [rule_path],
rules: {
[rule_name]: 2,
},
};

const {
results: [{ warnings, errored }],
} = await stylelint.lint({
code: `a {}`,
config,
});

assert.is(errored, false)
assert.equal(warnings, [])
});

test('should error when lines of code exceeds allowed setting', async () => {
const config = {
plugins: [rule_path],
rules: {
[rule_name]: 2,
},
};

let code = `
a {
color: green;
}

a {
color: red;
}
`

const {
results: [{ warnings, errored }],
} = await stylelint.lint({
code,
config,
});

assert.is(errored, true)
assert.equal(warnings, [
{
line: 1,
column: 1,
endLine: 9,
endColumn: 3,
rule: rule_name,
severity: 'error',
text: 'Counted 4 Lines of Code which is greater than the allowed 2 (project-wallace/max-lines-of-code)',
}
])
});

test.run()
6 changes: 3 additions & 3 deletions src/rules/max-selector-complexity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const { createPlugin, utils } = stylelint;
const rule_name = "project-wallace/max-selector-complexity";

const messages = utils.ruleMessages(rule_name, {
rejected: (property) =>
`"${property}" was declared but never used in a var()`,
rejected: ({ selector, actual, expected }) =>
`Selector complexity of "${selector}" is ${actual} which is greater than the allowed ${expected}`,
});

const meta = {
Expand Down Expand Up @@ -39,7 +39,7 @@ const ruleFunction = (primaryOption) => {

if (complexity > primaryOption) {
utils.report({
message: `Selector complexity of "${stringified}" is ${complexity} which is greater than the allowed ${primaryOption}`,
message: messages.rejected({ selector: stringified, expected: primaryOption, actual: complexity }),
node: rule, // TODO: only report the selector
result,
ruleName: rule_name,
Expand Down
10 changes: 5 additions & 5 deletions src/rules/max-selector-complexity/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test('should error on a very complex selector', async () => {
endColumn: 17,
rule: 'project-wallace/max-selector-complexity',
severity: 'error',
text: 'Selector complexity of "a b c d e f g" is 13 which is greater than the allowed 2',
text: 'Selector complexity of "a b c d e f g" is 13 which is greater than the allowed 2 (project-wallace/max-selector-complexity)',
}
])
});
Expand Down Expand Up @@ -119,7 +119,7 @@ test('should error on multiple complex selectors', async () => {
"endColumn": 20,
"rule": "project-wallace/max-selector-complexity",
"severity": "error",
"text": "Selector complexity of \"a b c d e f g\" is 13 which is greater than the allowed 2"
"text": "Selector complexity of \"a b c d e f g\" is 13 which is greater than the allowed 2 (project-wallace/max-selector-complexity)"
},
{
"line": 4,
Expand All @@ -128,7 +128,7 @@ test('should error on multiple complex selectors', async () => {
"endColumn": 27,
"rule": "project-wallace/max-selector-complexity",
"severity": "error",
"text": "Selector complexity of \".a .b .c .d #e #f #g\" is 13 which is greater than the allowed 2"
"text": "Selector complexity of \".a .b .c .d #e #f #g\" is 13 which is greater than the allowed 2 (project-wallace/max-selector-complexity)"
}
])
});
Expand Down Expand Up @@ -157,7 +157,7 @@ test('should error on a low-specificity/high-complexity selector', async () => {
"endColumn": 33,
"rule": "project-wallace/max-selector-complexity",
"severity": "error",
"text": "Selector complexity of \":-moz-any(#a #b #c, #d #e #f)\" is 12 which is greater than the allowed 2"
"text": "Selector complexity of \":-moz-any(#a #b #c, #d #e #f)\" is 12 which is greater than the allowed 2 (project-wallace/max-selector-complexity)"
}
])
});
Expand Down Expand Up @@ -186,7 +186,7 @@ test('should only report the one selector in a list thats problematic', async ()
"endColumn": 14,
"rule": "project-wallace/max-selector-complexity",
"severity": "error",
"text": "Selector complexity of \"a a a a\" is 7 which is greater than the allowed 2"
"text": "Selector complexity of \"a a a a\" is 7 which is greater than the allowed 2 (project-wallace/max-selector-complexity)"
}
])
});
Expand Down