-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
59 lines (46 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { createPlugin } = require('stylelint')
const report = require('stylelint/lib/utils/report')
const ruleMessages = require('stylelint/lib/utils/ruleMessages')
const validateOptions = require('stylelint/lib/utils/validateOptions')
const ruleName = 'stylistic/no-missing-end-of-source-newline'
const messages = ruleMessages(ruleName, {
rejected: 'Unexpected missing end-of-source newline',
})
const meta = {
url: 'https://github.com/elirasza/stylelint-stylistic/tree/main/lib/rules/no-missing-end-of-source-newline',
fixable: true,
}
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary })
if (!validOptions) {
return
}
if (root.source == null) {
throw new Error('The root node must have a source property')
}
// @ts-expect-error -- TS2339: Property 'inline' does not exist on type 'Source'.
if (root.source.inline || root.source.lang === 'object-literal') {
return
}
const rootString = context.fix ? root.toString() : root.source.input.css
if (!rootString.trim() || rootString.endsWith('\n')) {
return
}
// Fix
if (context.fix) {
root.raws.after = context.newline
return
}
report({
message: messages.rejected,
node: root,
index: rootString.length - 1,
result,
ruleName,
})
}
rule.ruleName = ruleName
rule.messages = messages
rule.meta = meta
module.exports = { messages, meta, plugin: createPlugin(ruleName, rule), rule, ruleName }