-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from projectwallace/max-lines-of-code
rule: Max lines of code
- Loading branch information
Showing
8 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
# stylelint-plugins | ||
# stylelint-plugins | ||
|
||
TODO: note about only use on standard CS, do not use non-standard CSS |
This file contains 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 |
---|---|---|
@@ -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 | ||
] |
This file contains 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
Empty file.
This file contains 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 |
---|---|---|
@@ -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); |
This file contains 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 |
---|---|---|
@@ -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() |
This file contains 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
This file contains 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