UNPKG

eslint-plugin-stylistic-compat

Version:

Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8

252 lines (248 loc) 11.1 kB
'use strict'; var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/slice'); var _replaceAllInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/replace-all'); var _trimStartInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/trim-start'); var _trimEndInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/trim-end'); var utils = require('../utils.js'); var vendor = require('../vendor.js'); require('@babel/runtime-corejs3/core-js/object/from-entries'); require('@babel/runtime-corejs3/core-js/instance/map'); require('@babel/runtime-corejs3/core-js/instance/filter'); require('@babel/runtime-corejs3/core-js/object/entries'); require('@babel/runtime-corejs3/core-js/set'); require('@babel/runtime-corejs3/core-js/instance/flags'); require('@babel/runtime-corejs3/core-js/instance/includes'); require('@babel/runtime-corejs3/core-js/instance/starts-with'); require('@babel/runtime-corejs3/core-js/array/from'); require('@babel/runtime-corejs3/core-js/array/is-array'); require('@babel/runtime-corejs3/core-js/instance/concat'); require('@babel/runtime-corejs3/core-js/object/keys'); require('@babel/runtime-corejs3/core-js/instance/reduce'); require('@babel/runtime-corejs3/core-js/object/assign'); require('@babel/runtime-corejs3/core-js/instance/keys'); require('@babel/runtime-corejs3/core-js/instance/find'); require('@babel/runtime-corejs3/core-js/instance/reverse'); require('@babel/runtime-corejs3/core-js/object/define-property'); require('@babel/runtime-corejs3/core-js/object/create'); require('@babel/runtime-corejs3/core-js/object/freeze'); require('@babel/runtime-corejs3/core-js/instance/last-index-of'); require('@babel/runtime-corejs3/core-js/object/define-properties'); require('@babel/runtime-corejs3/core-js/instance/index-of'); require('@babel/runtime-corejs3/core-js/instance/at'); require('@babel/runtime-corejs3/core-js/symbol'); require('@babel/runtime-corejs3/core-js/symbol/iterator'); require('@babel/runtime-corejs3/core-js/parse-int'); require('@babel/runtime-corejs3/core-js/parse-float'); require('@babel/runtime-corejs3/core-js/weak-map'); require('@babel/runtime-corejs3/core-js/global-this'); require('@babel/runtime-corejs3/core-js/instance/for-each'); require('eslint/use-at-your-own-risk'); require('eslint'); require('@babel/runtime-corejs3/core-js/object/get-own-property-descriptor'); require('@babel/runtime-corejs3/core-js/map'); require('@babel/runtime-corejs3/core-js/instance/some'); require('@babel/runtime-corejs3/core-js/instance/every'); require('@babel/runtime-corejs3/core-js/instance/sort'); require('@babel/runtime-corejs3/core-js/array/of'); require('@babel/runtime-corejs3/core-js/instance/entries'); require('@babel/runtime-corejs3/core-js/instance/find-index'); require('@babel/runtime-corejs3/core-js/instance/flat'); require('@babel/runtime-corejs3/core-js/instance/values'); require('@babel/runtime-corejs3/core-js/object/get-own-property-names'); require('@babel/runtime-corejs3/core-js/number/is-finite'); require('@babel/runtime-corejs3/core-js/number/is-nan'); require('@babel/runtime-corejs3/core-js/number/parse-float'); require('@babel/runtime-corejs3/core-js/number/parse-int'); require('@babel/runtime-corejs3/core-js/object/is'); require('@babel/runtime-corejs3/core-js/object/is-extensible'); require('@babel/runtime-corejs3/core-js/object/is-frozen'); require('@babel/runtime-corejs3/core-js/object/is-sealed'); require('@babel/runtime-corejs3/core-js/object/values'); require('@babel/runtime-corejs3/core-js/string/from-code-point'); require('@babel/runtime-corejs3/core-js/string/raw'); require('@babel/runtime-corejs3/core-js/instance/code-point-at'); require('@babel/runtime-corejs3/core-js/instance/ends-with'); require('@babel/runtime-corejs3/core-js/instance/pad-end'); require('@babel/runtime-corejs3/core-js/instance/pad-start'); require('@babel/runtime-corejs3/core-js/instance/trim'); require('@babel/runtime-corejs3/core-js/instance/trim-left'); require('@babel/runtime-corejs3/core-js/instance/trim-right'); require('@babel/runtime-corejs3/core-js/symbol/for'); require('@babel/runtime-corejs3/core-js/symbol/key-for'); require('@babel/runtime-corejs3/core-js/object/prevent-extensions'); require('@babel/runtime-corejs3/core-js/object/seal'); require('@babel/runtime-corejs3/core-js/object/get-prototype-of'); require('@babel/runtime-corejs3/core-js/symbol/replace'); require('@babel/runtime-corejs3/core-js/instance/bind'); require('@babel/runtime-corejs3/core-js/instance/splice'); require('@babel/runtime-corejs3/core-js/instance/repeat'); var functionCallSpacing = utils.createRule({ name: "function-call-spacing", package: "ts", meta: { type: "layout", docs: { description: "Require or disallow spacing between function identifiers and their invocations" }, fixable: "whitespace", schema: { anyOf: [{ type: "array", items: [{ type: "string", enum: ["never"] }], minItems: 0, maxItems: 1 }, { type: "array", items: [{ type: "string", enum: ["always"] }, { type: "object", properties: { allowNewlines: { type: "boolean" }, optionalChain: { type: "object", properties: { before: { type: "boolean" }, after: { type: "boolean" } }, additionalProperties: false } }, additionalProperties: false }], minItems: 0, maxItems: 2 }] }, messages: { unexpectedWhitespace: "Unexpected whitespace between function name and paren.", unexpectedNewline: "Unexpected newline between function name and paren.", missing: "Missing space between function name and paren." } }, defaultOptions: ["never", {}], create(context, [option, config]) { const sourceCode = context.sourceCode; const text = sourceCode.getText(); const { allowNewlines = false, optionalChain = { before: true, after: true } } = config; function checkSpacing(node, leftToken, rightToken) { const isOptionalCall = vendor.astUtilsExports.isOptionalCallExpression(node); const textBetweenTokens = _sliceInstanceProperty(text).call(text, leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, ""); const hasWhitespace = /\s/u.test(textBetweenTokens); const hasNewline = hasWhitespace && vendor.astUtilsExports.LINEBREAK_MATCHER.test(textBetweenTokens); if (option === "never") { if (hasWhitespace) { return context.report({ node, loc: { start: leftToken.loc.end, end: rightToken.loc.start }, messageId: "unexpectedWhitespace", fix(fixer) { if (sourceCode.commentsExistBetween(leftToken, rightToken)) return null; if (isOptionalCall) { return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], "?."); } return fixer.removeRange([leftToken.range[1], rightToken.range[0]]); } }); } } else if (isOptionalCall) { const { before: beforeOptionChain = true, after: afterOptionChain = true } = optionalChain; const hasPrefixSpace = /^\s/u.test(textBetweenTokens); const hasSuffixSpace = /\s$/u.test(textBetweenTokens); const hasCorrectPrefixSpace = beforeOptionChain ? hasPrefixSpace : !hasPrefixSpace; const hasCorrectSuffixSpace = afterOptionChain ? hasSuffixSpace : !hasSuffixSpace; const hasCorrectNewline = allowNewlines || !hasNewline; if (!hasCorrectPrefixSpace || !hasCorrectSuffixSpace || !hasCorrectNewline) { const messageId = !hasCorrectNewline ? "unexpectedNewline" : !beforeOptionChain && hasPrefixSpace || !afterOptionChain && hasSuffixSpace ? "unexpectedWhitespace" : "missing"; context.report({ node, loc: { start: leftToken.loc.end, end: rightToken.loc.start }, messageId, fix(fixer) { if (sourceCode.commentsExistBetween(leftToken, rightToken)) return null; let text2 = textBetweenTokens; if (!allowNewlines) { const GLOBAL_LINEBREAK_MATCHER = new RegExp(vendor.astUtilsExports.LINEBREAK_MATCHER.source, "g"); text2 = _replaceAllInstanceProperty(text2).call(text2, GLOBAL_LINEBREAK_MATCHER, " "); } if (!hasCorrectPrefixSpace) text2 = beforeOptionChain ? ` ${text2}` : _trimStartInstanceProperty(text2).call(text2); if (!hasCorrectSuffixSpace) text2 = afterOptionChain ? `${text2} ` : _trimEndInstanceProperty(text2).call(text2); return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], text2); } }); } } else { if (!hasWhitespace) { context.report({ node, loc: { start: leftToken.loc.end, end: rightToken.loc.start }, messageId: "missing", fix(fixer) { return fixer.insertTextBefore(rightToken, " "); } }); } else if (!allowNewlines && hasNewline) { context.report({ node, loc: { start: leftToken.loc.end, end: rightToken.loc.start }, messageId: "unexpectedNewline", fix(fixer) { if (sourceCode.commentsExistBetween(leftToken, rightToken)) return null; return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " "); } }); } } } return { "CallExpression, NewExpression": function (node) { var _node$typeArguments; const closingParenToken = sourceCode.getLastToken(node); const lastCalleeTokenWithoutPossibleParens = sourceCode.getLastToken((_node$typeArguments = node.typeArguments) != null ? _node$typeArguments : node.callee); const openingParenToken = sourceCode.getFirstTokenBetween(lastCalleeTokenWithoutPossibleParens, closingParenToken, vendor.astUtilsExports.isOpeningParenToken); if (!openingParenToken || openingParenToken.range[1] >= node.range[1]) { return; } const lastCalleeToken = sourceCode.getTokenBefore(openingParenToken, vendor.astUtilsExports.isNotOptionalChainPunctuator); checkSpacing(node, lastCalleeToken, openingParenToken); }, ImportExpression(node) { const leftToken = sourceCode.getFirstToken(node); const rightToken = sourceCode.getTokenAfter(leftToken); checkSpacing(node, leftToken, rightToken); } }; } }); module.exports = functionCallSpacing;