Skip to content

Commit c001b65

Browse files
committed
Deprecate placeholder nodes
1 parent 098b177 commit c001b65

10 files changed

+34
-29
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ extension Parser {
275275
}
276276

277277
if self.currentToken.isEditorPlaceholder {
278-
let placeholder = self.consumeAnyToken()
278+
let placeholder = self.parseAnyIdentifier()
279279
return RawDeclSyntax(
280-
RawEditorPlaceholderDeclSyntax(
280+
RawMissingDeclSyntax(
281281
attributes: attrs.attributes,
282282
modifiers: attrs.modifiers,
283283
placeholder: placeholder,

Sources/SwiftParser/Expressions.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,14 +1241,6 @@ extension Parser {
12411241
mutating func parseIdentifierExpression() -> RawExprSyntax {
12421242
let declName = self.parseDeclReferenceExpr(.compoundNames)
12431243
guard self.withLookahead({ $0.canParseAsGenericArgumentList() }) else {
1244-
if declName.baseName.tokenText.isEditorPlaceholder && declName.argumentNames == nil {
1245-
return RawExprSyntax(
1246-
RawEditorPlaceholderExprSyntax(
1247-
placeholder: declName.baseName,
1248-
arena: self.arena
1249-
)
1250-
)
1251-
}
12521244
return RawExprSyntax(declName)
12531245
}
12541246

Sources/SwiftParserDiagnostics/MissingNodesError.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,18 @@ extension ParseDiagnosticsGenerator {
381381

382382
// Walk all upcoming sibling to see if they are also missing to handle them in this diagnostic.
383383
// If this is the case, handle all of them in this diagnostic.
384-
var missingNodes = [Syntax(node)]
384+
var missingNodes: [Syntax] = [Syntax(node)]
385+
386+
// If the node is an `MissingDeclSyntax` and the placeholder is an editor placeholder
387+
// we should not add a diagnostic that it's missing.
388+
// Instead, we should emit a diagnostic about the fact that there is an editor placeholder in the source file.
389+
if let missing = missingNodes.first?.as(MissingDeclSyntax.self),
390+
missing.placeholder.isEditorPlaceholder,
391+
!missing.placeholder.isMissing
392+
{
393+
return .visitChildren
394+
}
395+
385396
if let parentWithTokens = ancestorWithMoreTokens, let index {
386397
let siblings = parentWithTokens.children(viewMode: .all)
387398
let siblingsAfter = siblings[siblings.index(after: index)...]

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
678678
if shouldSkip(node) {
679679
return .skipChildren
680680
}
681-
if node.statements.only?.item.is(EditorPlaceholderExprSyntax.self) == true {
681+
if let item = node.statements.only?.item.as(DeclReferenceExprSyntax.self), item.baseName.isEditorPlaceholder {
682682
// Only emit a single diagnostic about the editor placeholder and none for the missing '{' and '}'.
683683
addDiagnostic(node, .editorPlaceholderInSourceFile, handledNodes: [node.id])
684684
return .skipChildren

Sources/SwiftRefactor/ExpandEditorPlaceholder.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
135135
/// Expansion on `closure1` and `normalArg` is the same as `ExpandEditorPlaceholder`.
136136
public struct ExpandEditorPlaceholders: EditRefactoringProvider {
137137
public static func textRefactor(syntax token: TokenSyntax, in context: Void) -> [SourceEdit] {
138-
guard let placeholder = token.parent?.as(EditorPlaceholderExprSyntax.self),
138+
guard let placeholder = token.parent?.as(DeclReferenceExprSyntax.self),
139+
placeholder.baseName.isEditorPlaceholder,
139140
let arg = placeholder.parent?.as(LabeledExprSyntax.self),
140141
let argList = arg.parent?.as(LabeledExprListSyntax.self),
141142
let call = argList.parent?.as(FunctionCallExprSyntax.self)
@@ -190,8 +191,8 @@ extension FunctionTypeSyntax {
190191
placeholder = ExpandEditorPlaceholder.wrapInTypePlaceholder(ret, type: ret)
191192
}
192193

193-
let statementPlaceholder = EditorPlaceholderExprSyntax(
194-
placeholder: .identifier(placeholder)
194+
let statementPlaceholder = DeclReferenceExprSyntax(
195+
baseName: .identifier(placeholder)
195196
)
196197
let closureStatement = CodeBlockItemSyntax(
197198
item: .expr(ExprSyntax(statementPlaceholder))
@@ -234,8 +235,9 @@ extension FunctionCallExprSyntax {
234235
var includedArg = false
235236
var argsToExpand = 0
236237
for arg in arguments.reversed() {
237-
guard let expr = arg.expression.as(EditorPlaceholderExprSyntax.self),
238-
let data = EditorPlaceholderData(token: expr.placeholder),
238+
guard let expr = arg.expression.as(DeclReferenceExprSyntax.self),
239+
expr.baseName.isEditorPlaceholder,
240+
let data = EditorPlaceholderData(token: expr.baseName),
239241
case let .typed(_, type) = data,
240242
type.is(FunctionTypeSyntax.self)
241243
else {
@@ -253,7 +255,7 @@ extension FunctionCallExprSyntax {
253255

254256
var expandedArgs = [LabeledExprSyntax]()
255257
for arg in arguments.suffix(argsToExpand) {
256-
let edits = ExpandEditorPlaceholder.textRefactor(syntax: arg.expression.cast(EditorPlaceholderExprSyntax.self).placeholder)
258+
let edits = ExpandEditorPlaceholder.textRefactor(syntax: arg.expression.cast(DeclReferenceExprSyntax.self).baseName)
257259
guard edits.count == 1, let edit = edits.first, !edit.replacement.isEmpty else {
258260
return nil
259261
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ final class DeclarationTests: ParserTestCase {
22972297
1️⃣<#code#>
22982298
}
22992299
""",
2300-
substructure: MemberBlockItemSyntax(decl: EditorPlaceholderDeclSyntax(placeholder: .identifier("<#code#>"))),
2300+
substructure: MemberBlockItemSyntax(decl: MissingDeclSyntax(placeholder: .identifier("<#code#>"))),
23012301
diagnostics: [
23022302
DiagnosticSpec(message: "editor placeholder in source file")
23032303
]
@@ -2831,8 +2831,8 @@ final class DeclarationTests: ParserTestCase {
28312831
CodeBlockItemSyntax(
28322832
item: .expr(
28332833
ExprSyntax(
2834-
EditorPlaceholderExprSyntax(
2835-
placeholder: .identifier("<#function body#>")
2834+
DeclReferenceExprSyntax(
2835+
baseName: .identifier("<#function body#>")
28362836
)
28372837
)
28382838
)
@@ -2876,8 +2876,8 @@ final class DeclarationTests: ParserTestCase {
28762876
CodeBlockItemSyntax(
28772877
item: .expr(
28782878
ExprSyntax(
2879-
EditorPlaceholderExprSyntax(
2880-
placeholder: .identifier("<#function body#>")
2879+
DeclReferenceExprSyntax(
2880+
baseName: .identifier("<#function body#>")
28812881
)
28822882
)
28832883
)

Tests/SwiftParserTest/PatternTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ final class PatternTests: ParserTestCase {
180180
identifier: .identifier("<#name#>")
181181
),
182182
initializer: InitializerClauseSyntax(
183-
value: EditorPlaceholderExprSyntax(
184-
placeholder: .identifier("<#value#>")
183+
value: DeclReferenceExprSyntax(
184+
baseName: .identifier("<#value#>")
185185
)
186186
)
187187
)

Tests/SwiftParserTest/translated/TrailingClosuresTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ final class TrailingClosuresTests: ParserTestCase {
185185
leftBrace: .leftBraceToken(presence: .missing),
186186
statements: CodeBlockItemListSyntax([
187187
CodeBlockItemSyntax(
188-
item: .init(EditorPlaceholderExprSyntax(placeholder: .identifier("<#T##() -> Void#>")))
188+
item: .init(DeclReferenceExprSyntax(baseName: .identifier("<#T##() -> Void#>")))
189189
)
190190
]),
191191
rightBrace: .rightBraceToken(presence: .missing)

Tests/SwiftRefactorTest/ExpandEditorPlaceholderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fileprivate func assertRefactorPlaceholder(
103103
} else {
104104
var parser = Parser(placeholder)
105105
let expr = ExprSyntax.parse(from: &parser)
106-
token = try XCTUnwrap(expr.as(EditorPlaceholderExprSyntax.self)?.placeholder, file: file, line: line)
106+
token = try XCTUnwrap(expr.as(DeclReferenceExprSyntax.self)?.baseName, file: file, line: line)
107107
}
108108

109109
try assertRefactor(token, context: (), provider: ExpandEditorPlaceholder.self, expected: [SourceEdit.replace(token, with: expected)], file: file, line: line)

Tests/SwiftRefactorTest/ExpandEditorPlaceholdersTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fileprivate func assertRefactorPlaceholderCall(
112112
var parser = Parser(expr)
113113
let call = try XCTUnwrap(ExprSyntax.parse(from: &parser).as(FunctionCallExprSyntax.self), file: file, line: line)
114114
let arg = call.arguments[call.arguments.index(at: placeholder)]
115-
let token: TokenSyntax = try XCTUnwrap(arg.expression.as(EditorPlaceholderExprSyntax.self), file: file, line: line).placeholder
115+
let token: TokenSyntax = try XCTUnwrap(arg.expression.as(DeclReferenceExprSyntax.self), file: file, line: line).baseName
116116

117117
try assertRefactor(token, context: (), provider: ExpandEditorPlaceholders.self, expected: [SourceEdit.replace(call, with: expected)], file: file, line: line)
118118
}
@@ -127,7 +127,7 @@ fileprivate func assertRefactorPlaceholderToken(
127127
var parser = Parser(expr)
128128
let call = try XCTUnwrap(ExprSyntax.parse(from: &parser).as(FunctionCallExprSyntax.self), file: file, line: line)
129129
let arg = call.arguments[call.arguments.index(at: placeholder)]
130-
let token: TokenSyntax = try XCTUnwrap(arg.expression.as(EditorPlaceholderExprSyntax.self), file: file, line: line).placeholder
130+
let token: TokenSyntax = try XCTUnwrap(arg.expression.as(DeclReferenceExprSyntax.self), file: file, line: line).baseName
131131

132132
try assertRefactor(token, context: (), provider: ExpandEditorPlaceholders.self, expected: [SourceEdit.replace(token, with: expected)], file: file, line: line)
133133
}

0 commit comments

Comments
 (0)