123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- // @ts-nocheck
-
- 'use strict';
-
- const declarationValueIndex = require('../../utils/declarationValueIndex');
- const getDeclarationValue = require('../../utils/getDeclarationValue');
- const isSingleLineString = require('../../utils/isSingleLineString');
- const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
- const report = require('../../utils/report');
- const ruleMessages = require('../../utils/ruleMessages');
- const setDeclarationValue = require('../../utils/setDeclarationValue');
- const validateOptions = require('../../utils/validateOptions');
- const valueParser = require('postcss-value-parser');
-
- const ruleName = 'function-parentheses-newline-inside';
-
- const messages = ruleMessages(ruleName, {
- expectedOpening: 'Expected newline after "("',
- expectedClosing: 'Expected newline before ")"',
- expectedOpeningMultiLine: 'Expected newline after "(" in a multi-line function',
- rejectedOpeningMultiLine: 'Unexpected whitespace after "(" in a multi-line function',
- expectedClosingMultiLine: 'Expected newline before ")" in a multi-line function',
- rejectedClosingMultiLine: 'Unexpected whitespace before ")" in a multi-line function',
- });
-
- function rule(expectation, options, context) {
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: expectation,
- possible: ['always', 'always-multi-line', 'never-multi-line'],
- });
-
- if (!validOptions) {
- return;
- }
-
- root.walkDecls((decl) => {
- if (!decl.value.includes('(')) {
- return;
- }
-
- let hasFixed = false;
- const declValue = getDeclarationValue(decl);
- const parsedValue = valueParser(declValue);
-
- parsedValue.walk((valueNode) => {
- if (valueNode.type !== 'function') {
- return;
- }
-
- if (!isStandardSyntaxFunction(valueNode)) {
- return;
- }
-
- const functionString = valueParser.stringify(valueNode);
- const isMultiLine = !isSingleLineString(functionString);
-
- function containsNewline(str) {
- return str.includes('\n');
- }
-
- // Check opening ...
-
- const openingIndex = valueNode.sourceIndex + valueNode.value.length + 1;
- const checkBefore = getCheckBefore(valueNode);
-
- if (expectation === 'always' && !containsNewline(checkBefore)) {
- if (context.fix) {
- hasFixed = true;
- fixBeforeForAlways(valueNode, context.newline);
- } else {
- complain(messages.expectedOpening, openingIndex);
- }
- }
-
- if (isMultiLine && expectation === 'always-multi-line' && !containsNewline(checkBefore)) {
- if (context.fix) {
- hasFixed = true;
- fixBeforeForAlways(valueNode, context.newline);
- } else {
- complain(messages.expectedOpeningMultiLine, openingIndex);
- }
- }
-
- if (isMultiLine && expectation === 'never-multi-line' && checkBefore !== '') {
- if (context.fix) {
- hasFixed = true;
- fixBeforeForNever(valueNode);
- } else {
- complain(messages.rejectedOpeningMultiLine, openingIndex);
- }
- }
-
- // Check closing ...
-
- const closingIndex = valueNode.sourceIndex + functionString.length - 2;
- const checkAfter = getCheckAfter(valueNode);
-
- if (expectation === 'always' && !containsNewline(checkAfter)) {
- if (context.fix) {
- hasFixed = true;
- fixAfterForAlways(valueNode, context.newline);
- } else {
- complain(messages.expectedClosing, closingIndex);
- }
- }
-
- if (isMultiLine && expectation === 'always-multi-line' && !containsNewline(checkAfter)) {
- if (context.fix) {
- hasFixed = true;
- fixAfterForAlways(valueNode, context.newline);
- } else {
- complain(messages.expectedClosingMultiLine, closingIndex);
- }
- }
-
- if (isMultiLine && expectation === 'never-multi-line' && checkAfter !== '') {
- if (context.fix) {
- hasFixed = true;
- fixAfterForNever(valueNode);
- } else {
- complain(messages.rejectedClosingMultiLine, closingIndex);
- }
- }
- });
-
- if (hasFixed) {
- setDeclarationValue(decl, parsedValue.toString());
- }
-
- function complain(message, offset) {
- report({
- ruleName,
- result,
- message,
- node: decl,
- index: declarationValueIndex(decl) + offset,
- });
- }
- });
- };
- }
-
- function getCheckBefore(valueNode) {
- let before = valueNode.before;
-
- for (const node of valueNode.nodes) {
- if (node.type === 'comment') {
- continue;
- }
-
- if (node.type === 'space') {
- before += node.value;
- continue;
- }
-
- break;
- }
-
- return before;
- }
-
- function getCheckAfter(valueNode) {
- let after = '';
-
- for (const node of valueNode.nodes.slice().reverse()) {
- if (node.type === 'comment') {
- continue;
- }
-
- if (node.type === 'space') {
- after = node.value + after;
- continue;
- }
-
- break;
- }
-
- after += valueNode.after;
-
- return after;
- }
-
- function fixBeforeForAlways(valueNode, newline) {
- let target;
-
- for (const node of valueNode.nodes) {
- if (node.type === 'comment') {
- continue;
- }
-
- if (node.type === 'space') {
- target = node;
- continue;
- }
-
- break;
- }
-
- if (target) {
- target.value = newline + target.value;
- } else {
- valueNode.before = newline + valueNode.before;
- }
- }
-
- function fixBeforeForNever(valueNode) {
- valueNode.before = '';
-
- for (const node of valueNode.nodes) {
- if (node.type === 'comment') {
- continue;
- }
-
- if (node.type === 'space') {
- node.value = '';
- continue;
- }
-
- break;
- }
- }
-
- function fixAfterForAlways(valueNode, newline) {
- valueNode.after = newline + valueNode.after;
- }
-
- function fixAfterForNever(valueNode) {
- valueNode.after = '';
-
- for (const node of valueNode.nodes.slice().reverse()) {
- if (node.type === 'comment') {
- continue;
- }
-
- if (node.type === 'space') {
- node.value = '';
- continue;
- }
-
- break;
- }
- }
-
- rule.ruleName = ruleName;
- rule.messages = messages;
- module.exports = rule;
|