feat(schema): locate ID-typed value-constraint errors
Convert idValueConstraintErrors to idValueConstraintFindings so each a-props-correct.3 / e-props-correct.5 violation is attached to its declaring element or attribute node, letting an editor underline the offending declaration by line and column instead of the schema root. The error predicate and reason strings are unchanged: only where each error points differs. Adds a located-diagnostics test asserting the finding resolves to the declaring component's source range.
This commit is contained in:
parent
fa3552876a
commit
f5059de42e
@ -166,9 +166,7 @@ extension PureXML.Validation {
|
||||
|
||||
static var idValueConstraintsValid: Validation<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
|
||||
compileRule("ID-typed value constraints are valid") { document in
|
||||
PureXML.Schema.SchemaLocatedFinding.unlocated(
|
||||
PureXML.Schema.XSDParser.idValueConstraintErrors(document.schema, document.context),
|
||||
)
|
||||
PureXML.Schema.XSDParser.idValueConstraintFindings(document.schema, document.context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,20 +7,26 @@ extension PureXML.Schema.XSDParser {
|
||||
/// named user type derived from `xs:ID` is recognized, not only the built-in and
|
||||
/// inline forms. Only XSD-namespace declarations are checked; foreign content is
|
||||
/// skipped.
|
||||
static func idValueConstraintErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] {
|
||||
var errors: [String] = []
|
||||
collectIDValueConstraintErrors(schema, context, into: &errors)
|
||||
return errors
|
||||
/// Located findings (#169): each error is attached to the declaring `element`/
|
||||
/// `attribute` node, so an editor can underline it by line/column.
|
||||
static func idValueConstraintFindings(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [PureXML.Schema.SchemaLocatedFinding] {
|
||||
var findings: [PureXML.Schema.SchemaLocatedFinding] = []
|
||||
collectIDValueConstraintFindings(schema, context, into: &findings)
|
||||
return findings
|
||||
}
|
||||
|
||||
private static func collectIDValueConstraintErrors(_ node: XSDTree, _ context: PureXML.Schema.XSDContext, into errors: inout [String]) {
|
||||
private static func collectIDValueConstraintFindings(
|
||||
_ node: XSDTree,
|
||||
_ context: PureXML.Schema.XSDContext,
|
||||
into findings: inout [PureXML.Schema.SchemaLocatedFinding],
|
||||
) {
|
||||
let local = IDVNode.localName(node)
|
||||
if local == "appinfo" || local == "documentation" { return }
|
||||
if let error = idValueConstraintError(node, context) {
|
||||
errors.append(error)
|
||||
findings.append(PureXML.Schema.SchemaLocatedFinding(reason: error, node: node))
|
||||
}
|
||||
for child in IDVNode.elementChildren(node) {
|
||||
collectIDValueConstraintErrors(child, context, into: &errors)
|
||||
collectIDValueConstraintFindings(child, context, into: &findings)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -55,6 +55,23 @@ struct SchemaLocatedDiagnosticsTests {
|
||||
#expect(range?.start.column == 3)
|
||||
}
|
||||
|
||||
@Test("An ID-typed default/fixed value constraint is located on its declaring component")
|
||||
func test_idValueConstraintLocated() throws {
|
||||
let xsd = """
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="a" type="xs:ID" default="x"/>
|
||||
</xs:schema>
|
||||
"""
|
||||
let findings = try inconsistentFindings(in: xsd)
|
||||
let located = try #require(findings.first { $0.reason.contains("must not have a default or fixed value") })
|
||||
#expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
|
||||
|
||||
let (tree, _) = PureXML.readTree(xsd)
|
||||
let range = tree.sourceRange(at: located.codingPath)
|
||||
#expect(range?.start.line == 2)
|
||||
#expect(range?.start.column == 3)
|
||||
}
|
||||
|
||||
@Test("An xs:include targetNamespace mismatch is located on the include directive")
|
||||
func test_includeMismatchLocated() throws {
|
||||
let included = """
|
||||
|
||||
Loading…
Reference in New Issue
Block a user