-
Notifications
You must be signed in to change notification settings - Fork 1
/
CompilerTypes.swift
244 lines (194 loc) · 8.05 KB
/
CompilerTypes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// CompilerTypes.swift
// Sass
//
// Licensed under MIT (https://github.com/johnfairh/swift-sass/blob/main/LICENSE
//
// Much text here taken verbatim or only slightly editted from the embedded Sass
// protocol specification.
// Copyright (c) 2019, Google LLC
// Licensed under MIT (https://github.com/sass/embedded-protocol/blob/master/LICENSE)
//
import Foundation
// Sass compiler interface types, shared between embedded Sass and libsass.
/// How the Sass compiler should format the CSS it produces.
public enum CssStyle: Sendable {
/// Each selector and declaration is written on its own line.
case expanded
/// The entire stylesheet is written on a single line, with as few
/// characters as possible.
case compressed
/// CSS rules and declarations are indented to match the nesting of the
/// Sass source.
case nested
/// Each CSS rule is written on its own single line, along with all its
/// declarations.
case compact
}
/// The [syntax used for a stylesheet](https://sass-lang.com/documentation/syntax).
public enum Syntax: Sendable {
/// The CSS-superset `.scss` syntax.
case scss
/// The indented `.sass` syntax.
case indented, sass
/// Plain CSS syntax that doesn't support any special Sass features.
case css
}
/// The kind of source map to generate for the stylesheet, returned in `CompilerResults.sourceMap`.
public enum SourceMapStyle: Sendable {
/// Do not generate a source map.
case none
/// Generate a source map that references sources by URL only.
case separateSources
/// Generate a source map with embedded sources.
case embeddedSources
}
/// Thrown as an error after a failed compilation.
public struct CompilerError: Swift.Error, CustomStringConvertible, Sendable {
/// A message describing the reason for the failure.
public let message: String
/// Optionally, the section of stylesheet that triggered the failure.
public let span: Span?
/// The stack trace through the compiler input stylesheets that led to the failure.
public let stackTrace: String?
/// Any compiler diagnostics found before the error.
public let messages: [CompilerMessage]
/// The canonical URLs of all source files used before the compilation failed.
///
/// If the compilation fails while attempting to import a file after determining its canonical URL
/// then that file's URL is included in the list.
///
/// This includes the URL of the initial Sass file if it is known.
public let loadedURLs: [URL]
/// A rich multi-line user-readable description of this error, containing `message`, `span`,
/// and `stackTrace`, but not `messages`. This is provided by the underlying Sass compiler,
/// format controlled using `CompilerMessageStyle`.
public let description: String
/// :nodoc:
public init(message: String, span: Span?, stackTrace: String?, messages: [CompilerMessage], loadedURLs: [URL], description: String) {
self.message = message
self.span = span
self.stackTrace = stackTrace
self.messages = messages
self.loadedURLs = loadedURLs
self.description = description
}
}
/// A section of a stylesheet.
public struct Span: CustomStringConvertible, Sendable {
// MARK: Types
/// A single point in a stylesheet.
public struct Location: CustomStringConvertible, Sendable {
/// The 0-based byte offset of this location within the stylesheet.
public let offset: Int
/// The 1-based line number of this location within the stylesheet.
public let line: Int
/// The 1-based column number of this location within its line.
public let column: Int
/// A short description of the location.
public var description: String {
"\(line):\(column)"
}
/// :nodoc:
public init(offset: Int, line: Int, column: Int) {
self.offset = offset
self.line = line
self.column = column
}
}
// MARK: Properties
/// The text covered by the span, or `nil` if there is no
/// associated text.
public let text: String?
/// The URL of the stylesheet to which the span refers, or `nil` if it refers to
/// an inline compilation that doesn't specify a URL.
public let url: URL?
/// The location of the first character in the span.
public let start: Location
/// The location of the first character after this span, or `nil` to mean
/// the span is zero-length and points just before `start`.
public let end: Location?
/// Additional source text surrounding the span.
///
/// This usually contains the full lines the span begins and ends on if the
/// span itself doesn't cover the full lines.
public let context: String?
/// A short description of the span.
public var description: String {
var desc = url?.lastPathComponent ?? "[input]"
desc.append(" \(start)")
if let end = end {
desc.append("-\(end)")
}
return desc
}
/// :nodoc:
public init(text: String?, url: URL?, start: Location, end: Location?, context: String?) {
self.text = text
self.url = url
self.start = start
self.end = end
self.context = context
}
}
/// A diagnostic message generated by the Sass compiler that does not prevent the compilation
/// from succeeding.
///
/// Appropriate for display to end users who own the stylesheets.
public struct CompilerMessage: CustomStringConvertible, Sendable {
// MARK: Types
/// Kinds of diagnostic message.
public enum Kind: Sendable {
/// A warning for something other than a deprecated Sass feature. Often
/// emitted due to a stylesheet using the [`@warn` rule](https://sass-lang.com/documentation/at-rules/warn).
case warning
/// A warning indicating that the stylesheet is using a deprecated Sass
/// feature. The accompanying text does not include text like "deprecation warning".
case deprecation
/// Text from a [`@debug` rule](https://sass-lang.com/documentation/at-rules/debug).
case debug
}
// MARK: Properties
/// The kind of the message.
public let kind: Kind
/// The text of the message.
public let message: String
/// Optionally, the section of stylesheet that triggered the message.
public let span: Span?
/// The stack trace through the compiler input stylesheets that led to the message.
public let stackTrace: String?
/// A compiler-specific identifier for the message. For Dart Sass this holds the deprecation ID
/// for deprecation warning messages.
public let messageID: String?
/// A rich multi-line user-readable description of this error, containing the message, span,
/// and stacktrace. This is provided by the underlying Sass compiler, format controlled using
/// `CompilerMessageStyle`.
public let description: String
/// :nodoc:
public init(kind: Kind, message: String, span: Span?, stackTrace: String?, messageID: String?, description: String) {
self.kind = kind
self.message = message
self.span = span
self.stackTrace = stackTrace
self.messageID = messageID
self.description = description
}
}
/// The format used for `CompilerError.description` and `CompilerMessage.description`.
public enum CompilerMessageStyle: Sendable {
/// Plain text.
case plain
/// Colorized with terminal escape sequences.
case terminalColored
}
/// A control over warnings and debug messages produced by the compiler
public enum CompilerWarningLevel: Sendable {
/// All warnings and debug messages that can be generated are generated.
case all
/// Warnings from Sass files loaded by importers other than the importer used to load the main
/// Sass file are suppressed.
case noDependencyWarnings
/// All warnings and debug messages are suppressed. This may improve performance in certain
/// scenarios where many warning messages would otherwise be logged.
case none
}