-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): change parsers from stamps to TypeScript classes (#4102
) Refs #3481 BREAKING CHANGE: parsers from apidom-reference package became a class and requires to be instantiated with new operator.
- Loading branch information
Showing
40 changed files
with
1,110 additions
and
981 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,54 @@ | ||
import stampit from 'stampit'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { NotImplementedError } from '@swagger-api/apidom-error'; | ||
|
||
import { Parser as IParser } from '../../types'; | ||
|
||
const Parser: stampit.Stamp<IParser> = stampit({ | ||
props: { | ||
name: '', | ||
/** | ||
* Whether to allow "empty" files. This includes zero-byte files. | ||
*/ | ||
allowEmpty: true, | ||
|
||
/** | ||
* Whether to generate source map during parsing. | ||
*/ | ||
sourceMap: false, | ||
/** | ||
* List of supported file extensions. | ||
*/ | ||
fileExtensions: [], | ||
/** | ||
* List of supported media types. | ||
*/ | ||
mediaTypes: [], | ||
}, | ||
init( | ||
this: IParser, | ||
{ | ||
allowEmpty = this.allowEmpty, | ||
sourceMap = this.sourceMap, | ||
fileExtensions = this.fileExtensions, | ||
mediaTypes = this.mediaTypes, | ||
} = {}, | ||
) { | ||
|
||
import File from '../../File'; | ||
|
||
export interface ParserOptions { | ||
readonly name: string; | ||
readonly allowEmpty?: boolean; | ||
readonly sourceMap?: boolean; | ||
readonly fileExtensions?: string[]; | ||
readonly mediaTypes?: string[]; | ||
} | ||
|
||
abstract class Parser { | ||
public readonly name: string; | ||
|
||
/** | ||
* Whether to allow "empty" files. This includes zero-byte files. | ||
*/ | ||
public allowEmpty: boolean; | ||
|
||
/** | ||
* Whether to generate source map during parsing. | ||
*/ | ||
public sourceMap: boolean; | ||
|
||
/** | ||
* List of supported file extensions. | ||
*/ | ||
public fileExtensions: string[]; | ||
|
||
/** | ||
* List of supported media types. | ||
*/ | ||
public mediaTypes: string[]; | ||
|
||
constructor({ | ||
name, | ||
allowEmpty = true, | ||
sourceMap = false, | ||
fileExtensions = [], | ||
mediaTypes = [], | ||
}: ParserOptions) { | ||
this.name = name; | ||
this.allowEmpty = allowEmpty; | ||
this.sourceMap = sourceMap; | ||
this.fileExtensions = fileExtensions; | ||
this.mediaTypes = mediaTypes; | ||
}, | ||
methods: { | ||
async canParse(): Promise<boolean> { | ||
throw new NotImplementedError('canParse method in Parser stamp is not yet implemented.'); | ||
}, | ||
async parse(): Promise<ParseResultElement> { | ||
throw new NotImplementedError('parse method in Parser stamp is not yet implemented.'); | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
abstract canParse(file: File): boolean | Promise<boolean>; | ||
abstract parse(file: File): ParseResultElement | Promise<ParseResultElement>; | ||
} | ||
|
||
export default Parser; |
78 changes: 41 additions & 37 deletions
78
packages/apidom-reference/src/parse/parsers/api-design-systems-json/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,51 @@ | ||
import stampit from 'stampit'; | ||
import { pick } from 'ramda'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { | ||
parse, | ||
mediaTypes, | ||
mediaTypes as ADSMediaTypes, | ||
detect, | ||
} from '@swagger-api/apidom-parser-adapter-api-design-systems-json'; | ||
|
||
import ParserError from '../../../errors/ParserError'; | ||
import { Parser as IParser } from '../../../types'; | ||
import Parser from '../Parser'; | ||
import Parser, { ParserOptions } from '../Parser'; | ||
import File from '../../../File'; | ||
|
||
const ApiDesignSystemsJsonParser: stampit.Stamp<IParser> = stampit(Parser, { | ||
props: { | ||
name: 'api-design-systems-json', | ||
fileExtensions: ['.json'], | ||
mediaTypes, | ||
}, | ||
methods: { | ||
async canParse(file: File): Promise<boolean> { | ||
const hasSupportedFileExtension = | ||
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension); | ||
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType); | ||
|
||
if (!hasSupportedFileExtension) return false; | ||
if (hasSupportedMediaType) return true; | ||
if (!hasSupportedMediaType) { | ||
return detect(file.toString()); | ||
} | ||
return false; | ||
}, | ||
async parse(file: File): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: any) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default ApiDesignSystemsJsonParser; | ||
export interface APIDesignSystemsJSONParserOptions extends Omit<ParserOptions, 'name'> {} | ||
|
||
class APIDesignSystemsJSONParser extends Parser { | ||
public syntacticAnalysis?: 'direct' | 'indirect'; | ||
|
||
public refractorOpts!: object; | ||
|
||
constructor(options?: APIDesignSystemsJSONParserOptions) { | ||
const { fileExtensions = ['.json'], mediaTypes = ADSMediaTypes, ...rest } = options ?? {}; | ||
|
||
super({ ...rest, name: 'api-design-systems-json', fileExtensions, mediaTypes }); | ||
} | ||
|
||
async canParse(file: File): Promise<boolean> { | ||
const hasSupportedFileExtension = | ||
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension); | ||
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType); | ||
|
||
if (!hasSupportedFileExtension) return false; | ||
if (hasSupportedMediaType) return true; | ||
if (!hasSupportedMediaType) { | ||
return detect(file.toString()); | ||
} | ||
return false; | ||
} | ||
|
||
async parse(file: File): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: unknown) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
} | ||
} | ||
|
||
export default APIDesignSystemsJSONParser; |
80 changes: 43 additions & 37 deletions
80
packages/apidom-reference/src/parse/parsers/api-design-systems-yaml/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,53 @@ | ||
import stampit from 'stampit'; | ||
import { pick } from 'ramda'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { | ||
parse, | ||
mediaTypes, | ||
mediaTypes as ADSMediaTypes, | ||
detect, | ||
} from '@swagger-api/apidom-parser-adapter-api-design-systems-yaml'; | ||
|
||
import ParserError from '../../../errors/ParserError'; | ||
import { Parser as IParser } from '../../../types'; | ||
import Parser from '../Parser'; | ||
import Parser, { ParserOptions } from '../Parser'; | ||
import File from '../../../File'; | ||
|
||
const ApiDesignSystemsYamlParser: stampit.Stamp<IParser> = stampit(Parser, { | ||
props: { | ||
name: 'api-design-systems-yaml', | ||
fileExtensions: ['.yaml', '.yml'], | ||
mediaTypes, | ||
}, | ||
methods: { | ||
async canParse(file: File): Promise<boolean> { | ||
const hasSupportedFileExtension = | ||
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension); | ||
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType); | ||
|
||
if (!hasSupportedFileExtension) return false; | ||
if (hasSupportedMediaType) return true; | ||
if (!hasSupportedMediaType) { | ||
return detect(file.toString()); | ||
} | ||
return false; | ||
}, | ||
async parse(file: File): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: any) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default ApiDesignSystemsYamlParser; | ||
export interface APIDesignSystemsYAMLParserOptions extends Omit<ParserOptions, 'name'> {} | ||
|
||
class APIDesignSystemsYAMLParser extends Parser { | ||
public refractorOpts!: object; | ||
|
||
constructor(options?: APIDesignSystemsYAMLParserOptions) { | ||
const { | ||
fileExtensions = ['.yaml', '.yml'], | ||
mediaTypes = ADSMediaTypes, | ||
...rest | ||
} = options ?? {}; | ||
|
||
super({ ...rest, name: 'api-design-systems-yaml', fileExtensions, mediaTypes }); | ||
} | ||
|
||
async canParse(file: File): Promise<boolean> { | ||
const hasSupportedFileExtension = | ||
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension); | ||
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType); | ||
|
||
if (!hasSupportedFileExtension) return false; | ||
if (hasSupportedMediaType) return true; | ||
if (!hasSupportedMediaType) { | ||
return detect(file.toString()); | ||
} | ||
return false; | ||
} | ||
|
||
async parse(file: File): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: unknown) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
} | ||
} | ||
|
||
export default APIDesignSystemsYAMLParser; |
Oops, something went wrong.