Skip to content

Commit 2aaa409

Browse files
committed
Deprecate placeholder nodes
1 parent b73118c commit 2aaa409

File tree

14 files changed

+47
-29
lines changed

14 files changed

+47
-29
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

+2
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ public let DECL_NODES: [Node] = [
554554
nameForDiagnostics: "editor placeholder",
555555
documentation: """
556556
An editor placeholder, e.g. `<#declaration#>` that is used in a position that expects a declaration.
557+
558+
- Warning: This ``EditorPlaceholderDeclSyntax`` node is not generated by the parser anymore. Placeholders are represented by a ``MissingDeclSyntax``.
557559
""",
558560
traits: [
559561
"WithAttributes",

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

+3
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ public let EXPR_NODES: [Node] = [
792792
kind: .editorPlaceholderExpr,
793793
base: .expr,
794794
nameForDiagnostics: "editor placeholder",
795+
documentation: """
796+
- Warning: This ``EditorPlaceholderExprSyntax`` node is not generated by the parser anymore. Placeholders are represented by a ``DeclReferenceExprSyntax``.
797+
""",
795798
children: [
796799
Child(
797800
name: "placeholder",

Release Notes/511.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
- Issue: https://github.com/apple/swift-syntax/issues/2267
3131
- Pull request: https://github.com/apple/swift-syntax/pull/2272
3232

33+
- `EditorPlaceholderDeclSyntax` & `EditorPlaceholderExprSyntax`:
34+
- Description: `EditorPlaceholderDeclSyntax` & `EditorPlaceholderExprSyntax` are now deprecated and not parsed anymore. They will now be parsed as identifiers.
35+
- Pull request: https://github.com/apple/swift-syntax/pull/2237
36+
3337
## API-Incompatible Changes
3438

3539
- Effect specifiers:

Sources/SwiftParser/Declarations.swift

+2-2
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

-8
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

+12-1
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

+1-1
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

+8-6
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
}

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesEF.swift

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// MARK: - EditorPlaceholderDeclSyntax
1616

1717
/// An editor placeholder, e.g. `<#declaration#>` that is used in a position that expects a declaration.
18+
///
19+
/// - Warning: This ``EditorPlaceholderDeclSyntax`` node is not generated by the parser anymore. Placeholders are represented by a ``MissingDeclSyntax``.
1820
///
1921
/// ### Children
2022
///
@@ -220,6 +222,8 @@ public struct EditorPlaceholderDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _
220222

221223
// MARK: - EditorPlaceholderExprSyntax
222224

225+
/// - Warning: This ``EditorPlaceholderExprSyntax`` node is not generated by the parser anymore. Placeholders are represented by a ``DeclReferenceExprSyntax``.
226+
///
223227
/// ### Children
224228
///
225229
/// - `placeholder`: `<identifier>`

Tests/SwiftParserTest/DeclarationTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ final class DeclarationTests: ParserTestCase {
23132313
1️⃣<#code#>
23142314
}
23152315
""",
2316-
substructure: MemberBlockItemSyntax(decl: EditorPlaceholderDeclSyntax(placeholder: .identifier("<#code#>"))),
2316+
substructure: MemberBlockItemSyntax(decl: MissingDeclSyntax(placeholder: .identifier("<#code#>"))),
23172317
diagnostics: [
23182318
DiagnosticSpec(message: "editor placeholder in source file")
23192319
]
@@ -2847,8 +2847,8 @@ final class DeclarationTests: ParserTestCase {
28472847
CodeBlockItemSyntax(
28482848
item: .expr(
28492849
ExprSyntax(
2850-
EditorPlaceholderExprSyntax(
2851-
placeholder: .identifier("<#function body#>")
2850+
DeclReferenceExprSyntax(
2851+
baseName: .identifier("<#function body#>")
28522852
)
28532853
)
28542854
)
@@ -2892,8 +2892,8 @@ final class DeclarationTests: ParserTestCase {
28922892
CodeBlockItemSyntax(
28932893
item: .expr(
28942894
ExprSyntax(
2895-
EditorPlaceholderExprSyntax(
2896-
placeholder: .identifier("<#function body#>")
2895+
DeclReferenceExprSyntax(
2896+
baseName: .identifier("<#function body#>")
28972897
)
28982898
)
28992899
)

Tests/SwiftParserTest/PatternTests.swift

+2-2
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

+1-1
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

+1-1
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

+2-2
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)