@@ -20,12 +20,12 @@ import * as l10n from '@vscode/l10n';
2020import { convertSimple2RegExpPattern } from '../utils/strings' ;
2121import { SingleYAMLDocument } from '../parser/yamlParser07' ;
2222import { JSONDocument } from '../parser/jsonParser07' ;
23- import { parse } from 'yaml' ;
2423import * as path from 'path' ;
2524import { getSchemaFromModeline } from './modelineUtil' ;
2625import { JSONSchemaDescriptionExt } from '../../requestTypes' ;
2726import { SchemaVersions } from '../yamlTypes' ;
2827
28+ import * as Json from 'jsonc-parser' ;
2929import Ajv , { DefinedError } from 'ajv' ;
3030import Ajv4 from 'ajv-draft-04' ;
3131import { getSchemaTitle } from '../utils/schemaUtils' ;
@@ -643,31 +643,33 @@ export class YAMLSchemaService extends JSONSchemaService {
643643 // If the YAML file starts with %YAML 1.x or contains a comment with a number the schema will
644644 // contain a number instead of being undefined, so we need to check for that too.
645645 if (
646- unresolvedJsonSchema . errors &&
647- ( unresolvedJsonSchema . schema === undefined || typeof unresolvedJsonSchema . schema === 'number' )
646+ unresolvedJsonSchema . errors ||
647+ unresolvedJsonSchema . schema === undefined ||
648+ typeof unresolvedJsonSchema . schema === 'number'
648649 ) {
649650 return requestService ( schemaUri ) . then (
650651 ( content ) => {
651652 if ( ! content ) {
652- const errorMessage = l10n . t ( 'json.schema.nocontent ' , toDisplayString ( schemaUri ) , unresolvedJsonSchema . errors ) ;
653+ const errorMessage = l10n . t ( 'json.schema.noContent ' , toDisplayString ( schemaUri ) , unresolvedJsonSchema . errors ) ;
653654 return new UnresolvedSchema ( < JSONSchema > { } , [ errorMessage ] ) ;
654655 }
655-
656- try {
657- const schemaContent = parse ( content ) ;
658- return new UnresolvedSchema ( schemaContent , [ ] ) ;
659- } catch ( yamlError ) {
660- const errorMessage = l10n . t ( 'json.schema.invalidFormat' , toDisplayString ( schemaUri ) , yamlError ) ;
661- return new UnresolvedSchema ( < JSONSchema > { } , [ errorMessage ] ) ;
656+ const jsonErrors : Json . ParseError [ ] = [ ] ;
657+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
658+ const schemaContent = Json . parse ( content , jsonErrors ) ;
659+ let errorMessage = '' ;
660+ if ( jsonErrors . length ) {
661+ const { offset } = jsonErrors [ 0 ] ;
662+ const { line, column } = getLineAndColumnFromOffset ( content , offset ) ;
663+ errorMessage = l10n . t ( 'json.schema.invalidFormat' , toDisplayString ( schemaUri ) , line , column ) ;
662664 }
665+ return new UnresolvedSchema ( < JSONSchema > { } , [ errorMessage ] ) ;
663666 } ,
664- // eslint-disable-next-line @typescript-eslint/no-explicit-any
665- ( error : any ) => {
667+ ( error : unknown ) => {
666668 let errorMessage = error . toString ( ) ;
667- const errorSplit = error . toString ( ) . split ( 'Error: ' ) ;
669+ const errorSplit = error . toString ( ) . split ( 'vscode/content ' ) ;
668670 if ( errorSplit . length > 1 ) {
669671 // more concise error message, URL and context are attached by caller anyways
670- errorMessage = errorSplit [ 1 ] ;
672+ errorMessage = l10n . t ( 'json.schema.noContent' , toDisplayString ( schemaUri ) ) ;
671673 }
672674 return new UnresolvedSchema ( < JSONSchema > { } , [ errorMessage ] ) ;
673675 }
@@ -731,3 +733,10 @@ function toDisplayString(url: string): string {
731733 }
732734 return url ;
733735}
736+
737+ function getLineAndColumnFromOffset ( text : string , offset : number ) : { line : number ; column : number } {
738+ const lines = text . slice ( 0 , offset ) . split ( / \r ? \n / ) ;
739+ const line = lines . length ; // 1-based line number
740+ const column = lines [ lines . length - 1 ] . length + 1 ; // 1-based column number
741+ return { line, column } ;
742+ }
0 commit comments