Avoid withoutTrivia() where it’s not necessary

This commit is contained in:
Marcelo Fabri 2023-02-05 19:02:20 -08:00
parent 7eb479d546
commit a0caceb16f
17 changed files with 23 additions and 23 deletions

View File

@ -39,7 +39,7 @@ struct DiscouragedAssertRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderR
private extension DiscouragedAssertRule { private extension DiscouragedAssertRule {
final class Visitor: ViolationsSyntaxVisitor { final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
guard node.calledExpression.as(IdentifierExprSyntax.self)?.identifier.withoutTrivia().text == "assert", guard node.calledExpression.as(IdentifierExprSyntax.self)?.identifier.text == "assert",
let firstArg = node.argumentList.first, let firstArg = node.argumentList.first,
firstArg.label == nil, firstArg.label == nil,
let boolExpr = firstArg.expression.as(BooleanLiteralExprSyntax.self), let boolExpr = firstArg.expression.as(BooleanLiteralExprSyntax.self),

View File

@ -45,7 +45,7 @@ private extension FatalErrorMessageRule {
final class Visitor: ViolationsSyntaxVisitor { final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
guard let expression = node.calledExpression.as(IdentifierExprSyntax.self), guard let expression = node.calledExpression.as(IdentifierExprSyntax.self),
expression.identifier.withoutTrivia().text == "fatalError", expression.identifier.text == "fatalError",
node.argumentList.isEmptyOrEmptyString else { node.argumentList.isEmptyOrEmptyString else {
return return
} }

View File

@ -140,7 +140,7 @@ private extension LegacyConstructorRule {
final class Visitor: ViolationsSyntaxVisitor { final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self), if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
constructorsToCorrectedNames[identifierExpr.identifier.withoutTrivia().text] != nil { constructorsToCorrectedNames[identifierExpr.identifier.text] != nil {
violations.append(node.positionAfterSkippingLeadingTrivia) violations.append(node.positionAfterSkippingLeadingTrivia)
} }
} }
@ -158,7 +158,7 @@ private extension LegacyConstructorRule {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self), guard let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
case let identifier = identifierExpr.identifier.withoutTrivia().text, case let identifier = identifierExpr.identifier.text,
let correctedName = constructorsToCorrectedNames[identifier], let correctedName = constructorsToCorrectedNames[identifier],
let args = constructorsToArguments[identifier], let args = constructorsToArguments[identifier],
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else {

View File

@ -36,7 +36,7 @@ private extension LegacyRandomRule {
] ]
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
if let function = node.calledExpression.as(IdentifierExprSyntax.self)?.identifier.withoutTrivia().text, if let function = node.calledExpression.as(IdentifierExprSyntax.self)?.identifier.text,
Self.legacyRandomFunctions.contains(function) { Self.legacyRandomFunctions.contains(function) {
violations.append(node.positionAfterSkippingLeadingTrivia) violations.append(node.positionAfterSkippingLeadingTrivia)
} }

View File

@ -78,7 +78,7 @@ struct UnavailableConditionRule: ConfigurationProviderRule, SwiftSyntaxRule {
private final class UnavailableConditionRuleVisitor: ViolationsSyntaxVisitor { private final class UnavailableConditionRuleVisitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: IfStmtSyntax) { override func visitPost(_ node: IfStmtSyntax) {
guard node.body.statements.withoutTrivia().isEmpty else { guard node.body.statements.isEmpty else {
return return
} }

View File

@ -105,7 +105,7 @@ private extension UnavailableFunctionRule {
private extension FunctionDeclSyntax { private extension FunctionDeclSyntax {
var returnsNever: Bool { var returnsNever: Bool {
if let expr = signature.output?.returnType.as(SimpleTypeIdentifierSyntax.self) { if let expr = signature.output?.returnType.as(SimpleTypeIdentifierSyntax.self) {
return expr.name.withoutTrivia().text == "Never" return expr.name.text == "Never"
} }
return false return false
} }
@ -148,7 +148,7 @@ private extension CodeBlockSyntax? {
return false return false
} }
return terminatingFunctions.contains(identifierExpr.identifier.withoutTrivia().text) return terminatingFunctions.contains(identifierExpr.identifier.text)
} }
} }

View File

@ -68,7 +68,7 @@ private extension FunctionCallExprSyntax {
var isEnumerated: Bool { var isEnumerated: Bool {
guard let memberAccess = calledExpression.as(MemberAccessExprSyntax.self), guard let memberAccess = calledExpression.as(MemberAccessExprSyntax.self),
memberAccess.base != nil, memberAccess.base != nil,
memberAccess.name.withoutTrivia().text == "enumerated", memberAccess.name.text == "enumerated",
hasNoArguments else { hasNoArguments else {
return false return false
} }

View File

@ -85,7 +85,7 @@ private extension IdenticalOperandsRule {
final class Visitor: ViolationsSyntaxVisitor { final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: InfixOperatorExprSyntax) { override func visitPost(_ node: InfixOperatorExprSyntax) {
guard let operatorNode = node.operatorOperand.as(BinaryOperatorExprSyntax.self), guard let operatorNode = node.operatorOperand.as(BinaryOperatorExprSyntax.self),
IdenticalOperandsRule.operators.contains(operatorNode.operatorToken.withoutTrivia().text) else { IdenticalOperandsRule.operators.contains(operatorNode.operatorToken.text) else {
return return
} }

View File

@ -25,7 +25,7 @@ private extension QuickDiscouragedFocusedTestRule {
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self), if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
case let name = identifierExpr.identifier.withoutTrivia().text, case let name = identifierExpr.identifier.text,
QuickFocusedCallKind(rawValue: name) != nil { QuickFocusedCallKind(rawValue: name) != nil {
violations.append(node.positionAfterSkippingLeadingTrivia) violations.append(node.positionAfterSkippingLeadingTrivia)
} }

View File

@ -25,7 +25,7 @@ private extension QuickDiscouragedPendingTestRule {
override func visitPost(_ node: FunctionCallExprSyntax) { override func visitPost(_ node: FunctionCallExprSyntax) {
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self), if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
case let name = identifierExpr.identifier.withoutTrivia().text, case let name = identifierExpr.identifier.text,
QuickPendingCallKind(rawValue: name) != nil { QuickPendingCallKind(rawValue: name) != nil {
violations.append(node.positionAfterSkippingLeadingTrivia) violations.append(node.positionAfterSkippingLeadingTrivia)
} }

View File

@ -133,7 +133,7 @@ private extension LabeledStmtSyntax {
var violationPosition: AbsolutePosition? { var violationPosition: AbsolutePosition? {
let visitor = BreakAndContinueLabelCollector(viewMode: .sourceAccurate) let visitor = BreakAndContinueLabelCollector(viewMode: .sourceAccurate)
let labels = visitor.walk(tree: self, handler: \.labels) let labels = visitor.walk(tree: self, handler: \.labels)
guard !labels.contains(labelName.withoutTrivia().text) else { guard !labels.contains(labelName.text) else {
return nil return nil
} }
@ -145,13 +145,13 @@ private class BreakAndContinueLabelCollector: SyntaxVisitor {
private(set) var labels: Set<String> = [] private(set) var labels: Set<String> = []
override func visitPost(_ node: BreakStmtSyntax) { override func visitPost(_ node: BreakStmtSyntax) {
if let label = node.label?.withoutTrivia().text { if let label = node.label?.text {
labels.insert(label) labels.insert(label)
} }
} }
override func visitPost(_ node: ContinueStmtSyntax) { override func visitPost(_ node: ContinueStmtSyntax) {
if let label = node.label?.withoutTrivia().text { if let label = node.label?.text {
labels.insert(label) labels.insert(label)
} }
} }

View File

@ -143,7 +143,7 @@ private extension UnusedSetterValueRule {
return return
} }
let variableName = node.parameter?.name.withoutTrivia().text ?? "newValue" let variableName = node.parameter?.name.text ?? "newValue"
let visitor = NewValueUsageVisitor(variableName: variableName) let visitor = NewValueUsageVisitor(variableName: variableName)
if !visitor.walk(tree: node, handler: \.isVariableUsed) { if !visitor.walk(tree: node, handler: \.isVariableUsed) {
if (Syntax(node).closestVariableOrSubscript()?.modifiers).containsOverride, if (Syntax(node).closestVariableOrSubscript()?.modifiers).containsOverride,
@ -166,7 +166,7 @@ private extension UnusedSetterValueRule {
} }
override func visitPost(_ node: IdentifierExprSyntax) { override func visitPost(_ node: IdentifierExprSyntax) {
if node.identifier.withoutTrivia().text == variableName { if node.identifier.text == variableName {
isVariableUsed = true isVariableUsed = true
} }
} }

View File

@ -114,7 +114,7 @@ private extension VariableDeclSyntax {
return false return false
} }
return pattern.identifier.withoutTrivia().text.lowercased().hasSuffix("delegate") return pattern.identifier.text.lowercased().hasSuffix("delegate")
} }
} }
@ -147,7 +147,7 @@ private extension VariableDeclSyntax {
return false return false
} }
return ignoredAttributes.contains(typeIdentifier.name.withoutTrivia().text) return ignoredAttributes.contains(typeIdentifier.name.text)
} ?? false } ?? false
} }
} }

View File

@ -67,7 +67,7 @@ private extension EnumCaseAssociatedValuesLengthRule {
violationSeverity = .warning violationSeverity = .warning
} }
let reason = "Enum case \(node.identifier.withoutTrivia().text) should contain " let reason = "Enum case \(node.identifier.text) should contain "
+ "less than \(configuration.warning) associated values: " + "less than \(configuration.warning) associated values: "
+ "currently contains \(enumCaseAssociatedValueCount)" + "currently contains \(enumCaseAssociatedValueCount)"
violations.append( violations.append(

View File

@ -140,7 +140,7 @@ private enum NumberSeparatorViolation {
private extension NumberSeparatorValidator { private extension NumberSeparatorValidator {
func violation(token: TokenSyntax) -> NumberSeparatorViolation? { func violation(token: TokenSyntax) -> NumberSeparatorViolation? {
let content = token.withoutTrivia().text let content = token.text
guard isDecimal(number: content), guard isDecimal(number: content),
!isInValidRanges(number: content) !isInValidRanges(number: content)
else { else {

View File

@ -183,7 +183,7 @@ private class OperatorUsageWhitespaceVisitor: SyntaxVisitor {
let noSpacingAfter = operatorToken.trailingTrivia.isEmpty && nextToken.leadingTrivia.isEmpty let noSpacingAfter = operatorToken.trailingTrivia.isEmpty && nextToken.leadingTrivia.isEmpty
let noSpacing = noSpacingBefore || noSpacingAfter let noSpacing = noSpacingBefore || noSpacingAfter
let operatorText = operatorToken.withoutTrivia().text let operatorText = operatorToken.text
if noSpacing && allowedNoSpaceOperators.contains(operatorText) { if noSpacing && allowedNoSpaceOperators.contains(operatorText) {
return nil return nil
} }

View File

@ -62,7 +62,7 @@ private extension ShorthandOperatorRule {
guard node.operatorOperand.is(AssignmentExprSyntax.self), guard node.operatorOperand.is(AssignmentExprSyntax.self),
let rightExpr = node.rightOperand.as(InfixOperatorExprSyntax.self), let rightExpr = node.rightOperand.as(InfixOperatorExprSyntax.self),
let binaryOperatorExpr = rightExpr.operatorOperand.as(BinaryOperatorExprSyntax.self), let binaryOperatorExpr = rightExpr.operatorOperand.as(BinaryOperatorExprSyntax.self),
ShorthandOperatorRule.allOperators.contains(binaryOperatorExpr.operatorToken.withoutTrivia().text), ShorthandOperatorRule.allOperators.contains(binaryOperatorExpr.operatorToken.text),
node.leftOperand.withoutTrivia().description == rightExpr.leftOperand.withoutTrivia().description node.leftOperand.withoutTrivia().description == rightExpr.leftOperand.withoutTrivia().description
else { else {
return return