From 95486e1b9cd28f52e98612b832fffbda9d4851ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Jos=C3=A9=20Carvalho=20de=20Mello?= Date: Sat, 13 Jun 2020 12:03:05 -0300 Subject: [PATCH 1/2] fix "is not a module" #2603 --- types/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index b9eb0d317a..cc6c88f36d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -3,6 +3,8 @@ // eslint-disable-next-line declare const hljs : HLJSApi; +export = hljs; + interface HLJSApi { highlight: (languageName: string, code: string, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult From 3f8ef6661a02b11cf41010e65fc725199feef653 Mon Sep 17 00:00:00 2001 From: Rodrigo Mello Date: Sun, 14 Jun 2020 11:47:54 -0300 Subject: [PATCH 2/2] add a type file for each sub module --- tools/build_node.js | 8 +- types/core.d.ts | 5 + types/highlight.d.ts | 5 + types/index.d.ts | 213 +----------------------------------- types/languages/basic.d.ts | 5 + types/types.ts | 214 +++++++++++++++++++++++++++++++++++++ 6 files changed, 237 insertions(+), 213 deletions(-) create mode 100644 types/core.d.ts create mode 100644 types/highlight.d.ts create mode 100644 types/languages/basic.d.ts create mode 100644 types/types.ts diff --git a/tools/build_node.js b/tools/build_node.js index ed30b5d675..2fc68c06f5 100644 --- a/tools/build_node.js +++ b/tools/build_node.js @@ -57,6 +57,7 @@ async function buildPackageJSON() { } return acc; }, []); + json.types = "./lib/index.d.ts"; await fs.writeFile(`${process.env.BUILD_DIR}/package.json`, JSON.stringify(json, null, ' ')); } @@ -75,11 +76,14 @@ async function buildNode(options) { mkdir("lib/languages"); mkdir("scss"); mkdir("styles"); - mkdir("types"); install("./LICENSE", "LICENSE"); install("./README.md","README.md"); - install("./types/index.d.ts","types/index.d.ts"); + install("./types/index.d.ts","lib/index.d.ts"); + install("./types/types.ts","lib/types.ts"); + install("./types/core.d.ts","lib/core.d.ts"); + install("./types/highlight.d.ts","lib/highlight.d.ts"); + install("./types/languages/basic.d.ts","lib/languages/basic.d.ts"); log("Writing styles."); const styles = await fs.readdir("./src/styles/"); diff --git a/types/core.d.ts b/types/core.d.ts new file mode 100644 index 0000000000..eba9f25c3c --- /dev/null +++ b/types/core.d.ts @@ -0,0 +1,5 @@ +import './types'; + +declare const hljs: HLJSApi; + +export = hljs; diff --git a/types/highlight.d.ts b/types/highlight.d.ts new file mode 100644 index 0000000000..eba9f25c3c --- /dev/null +++ b/types/highlight.d.ts @@ -0,0 +1,5 @@ +import './types'; + +declare const hljs: HLJSApi; + +export = hljs; diff --git a/types/index.d.ts b/types/index.d.ts index cc6c88f36d..eba9f25c3c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,214 +1,5 @@ -/* Public API */ +import './types'; -// eslint-disable-next-line -declare const hljs : HLJSApi; +declare const hljs: HLJSApi; export = hljs; - -interface HLJSApi { - highlight: (languageName: string, code: string, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult - highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult - fixMarkup: (html: string) => string - highlightBlock: (element: HTMLElement) => void - configure: (options: Partial) => void - initHighlighting: () => void - initHighlightingOnLoad: () => void - registerLanguage: (languageName: string, language: LanguageFn) => void - listLanguages: () => string[] - registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void - getLanguage: (languageName: string) => Language | undefined - requireLanguage: (languageName: string) => Language | never - autoDetection: (languageName: string) => boolean - inherit: (original: T, ...args: Record[]) => T - addPlugin: (plugin: HLJSPlugin) => void - debugMode: () => void - safeMode: () => void - versionString: string -} - -interface HLJSApi { - SHEBANG: (mode?: Partial & {binary?: string | RegExp}) => Mode - BACKSLASH_ESCAPE: Mode - QUOTE_STRING_MODE: Mode - APOS_STRING_MODE: Mode - PHRASAL_WORDS_MODE: Mode - COMMENT: (begin: string | RegExp, end: string | RegExp, modeOpts?: Mode | {}) => Mode - C_LINE_COMMENT_MODE: Mode - C_BLOCK_COMMENT_MODE: Mode - HASH_COMMENT_MODE: Mode - NUMBER_MODE: Mode - C_NUMBER_MODE: Mode - BINARY_NUMBER_MODE: Mode - CSS_NUMBER_MODE: Mode - REGEXP_MODE: Mode - TITLE_MODE: Mode - UNDERSCORE_TITLE_MODE: Mode - METHOD_GUARD: Mode - END_SAME_AS_BEGIN: (mode: Mode) => Mode - // build in regex - IDENT_RE: string - UNDERSCORE_IDENT_RE: string - NUMBER_RE: string - C_NUMBER_RE: string - BINARY_NUMBER_RE: string - RE_STARTERS_RE: string -} - -type LanguageFn = (hljs: HLJSApi) => Language - -// interface RawLanguage { -// name?: string -// aliases?: string[] -// rawDefinition?: () => Language -// } - -interface HighlightResult { - relevance : number - value : string - language? : string - emitter : Emitter - illegal : boolean - top? : Language | CompiledMode - illegalBy? : illegalData - sofar? : string - errorRaised? : Error - // * for auto-highlight - second_best? : Omit -} - -interface illegalData { - msg: string - context: string - mode: CompiledMode -} - -interface AutoHighlightResult extends HighlightResult { -} - -type PluginEvent = - 'before:highlight' - | 'after:highlight' - | 'before:highlightBlock' - | 'after:highlightBlock' - -type HLJSPlugin = { [K in PluginEvent]? : any } - -interface EmitterConstructor { - new (opts: any): Emitter -} - -interface HLJSOptions { - noHighlightRe: RegExp - languageDetectRe: RegExp - classPrefix: string - tabReplace?: string - useBR: boolean - languages?: string[] - __emitter: EmitterConstructor -} - -interface CallbackResponse { - data: Record - ignoreMatch: () => void -} - -/************ - PRIVATE API - ************/ - -/* for jsdoc annotations in the JS source files */ - -type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode} - -type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void -type HighlightedHTMLElement = HTMLElement & {result?: object, second_best?: object, parentNode: HTMLElement} -type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType} -type MatchType = "begin" | "end" | "illegal" - - interface Emitter { - addKeyword(text: string, kind: string): void - addText(text: string): void - toHTML(): string - finalize(): void - closeAllNodes(): void - openNode(kind: string): void - closeNode(): void - addSublanguage(emitter: Emitter, subLanguageName: string): void - } - -/* modes */ - - interface ModeCallbacks { - "on:end"?: Function, - "on:begin"?: Function, - } - -interface Mode extends ModeCallbacks, ModeDetails { - -} - -interface LanguageDetail { - name?: string - rawDefinition?: () => Language - aliases?: string[] - disableAutodetect?: boolean - contains: ("self"|Mode)[] - case_insensitive?: boolean - keywords?: Record | string - compiled?: boolean -} - -type Language = LanguageDetail & Partial - -interface CompiledLanguage extends LanguageDetail, CompiledMode { - compiled: true - contains: CompiledMode[] - keywords: Record -} - -type KeywordData = [string, number]; -type KeywordDict = Record - -type CompiledMode = Omit & - { - contains: CompiledMode[] - keywords: KeywordDict - data: Record - terminator_end: string - keywordPatternRe: RegExp - beginRe: RegExp - endRe: RegExp - illegalRe: RegExp - matcher: any - compiled: true - starts?: CompiledMode - parent?: CompiledMode - } - -interface ModeDetails { - begin?: RegExp | string - end?: RegExp | string - className?: string - contains?: ("self" | Mode)[] - endsParent?: boolean - endsWithParent?: boolean - endSameAsBegin?: boolean - skip?: boolean - excludeBegin?: boolean - excludeEnd?: boolean - returnBegin?: boolean - returnEnd?: boolean - __beforeBegin?: Function - parent?: Mode - starts?:Mode - lexemes?: string | RegExp - keywords?: Record | string - beginKeywords?: string - relevance?: number - illegal?: string | RegExp - variants?: Mode[] - cached_variants?: Mode[] - // parsed - subLanguage?: string | string[] - compiled?: boolean -} diff --git a/types/languages/basic.d.ts b/types/languages/basic.d.ts new file mode 100644 index 0000000000..55cdabd552 --- /dev/null +++ b/types/languages/basic.d.ts @@ -0,0 +1,5 @@ +import './types'; + +declare const basic: LanguageFn; + +export = basic; diff --git a/types/types.ts b/types/types.ts new file mode 100644 index 0000000000..8008564641 --- /dev/null +++ b/types/types.ts @@ -0,0 +1,214 @@ +/* Public API */ + +// @ts-ignore +interface HLJSApi { + highlight: (languageName: string, code: string, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult + highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult + fixMarkup: (html: string) => string + highlightBlock: (element: HTMLElement) => void + configure: (options: Partial) => void + initHighlighting: () => void + initHighlightingOnLoad: () => void + registerLanguage: (languageName: string, language: LanguageFn) => void + listLanguages: () => string[] + registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void + getLanguage: (languageName: string) => Language | undefined + requireLanguage: (languageName: string) => Language | never + autoDetection: (languageName: string) => boolean + inherit: (original: T, ...args: Record[]) => T + addPlugin: (plugin: HLJSPlugin) => void + debugMode: () => void + safeMode: () => void + versionString: string +} + +// -------------------------------- +// WHY 2 INTERFACES WITH SAME NAME? +// -------------------------------- + +// interface HLJSApi { +// SHEBANG: (mode?: Partial & {binary?: string | RegExp}) => Mode +// BACKSLASH_ESCAPE: Mode +// QUOTE_STRING_MODE: Mode +// APOS_STRING_MODE: Mode +// PHRASAL_WORDS_MODE: Mode +// COMMENT: (begin: string | RegExp, end: string | RegExp, modeOpts?: Mode | {}) => Mode +// C_LINE_COMMENT_MODE: Mode +// C_BLOCK_COMMENT_MODE: Mode +// HASH_COMMENT_MODE: Mode +// NUMBER_MODE: Mode +// C_NUMBER_MODE: Mode +// BINARY_NUMBER_MODE: Mode +// CSS_NUMBER_MODE: Mode +// REGEXP_MODE: Mode +// TITLE_MODE: Mode +// UNDERSCORE_TITLE_MODE: Mode +// METHOD_GUARD: Mode +// END_SAME_AS_BEGIN: (mode: Mode) => Mode +// // build in regex +// IDENT_RE: string +// UNDERSCORE_IDENT_RE: string +// NUMBER_RE: string +// C_NUMBER_RE: string +// BINARY_NUMBER_RE: string +// RE_STARTERS_RE: string +// } + +type LanguageFn = (hljs: HLJSApi) => Language + +// interface RawLanguage { +// name?: string +// aliases?: string[] +// rawDefinition?: () => Language +// } + +interface HighlightResult { + relevance : number + value : string + language? : string + emitter : Emitter + illegal : boolean + top? : Language | CompiledMode + illegalBy? : illegalData + sofar? : string + errorRaised? : Error + // * for auto-highlight + second_best? : Omit +} + +interface illegalData { + msg: string + context: string + mode: CompiledMode +} + +interface AutoHighlightResult extends HighlightResult { +} + +type PluginEvent = + 'before:highlight' + | 'after:highlight' + | 'before:highlightBlock' + | 'after:highlightBlock' + +type HLJSPlugin = { [K in PluginEvent]? : any } + +interface EmitterConstructor { + new (opts: any): Emitter +} + +interface HLJSOptions { + noHighlightRe: RegExp + languageDetectRe: RegExp + classPrefix: string + tabReplace?: string + useBR: boolean + languages?: string[] + __emitter: EmitterConstructor +} + +interface CallbackResponse { + data: Record + ignoreMatch: () => void +} + +/************ + PRIVATE API + ************/ + +/* for jsdoc annotations in the JS source files */ + +type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode} + +type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void +type HighlightedHTMLElement = HTMLElement & {result?: object, second_best?: object, parentNode: HTMLElement} +type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType} +type MatchType = "begin" | "end" | "illegal" + +interface Emitter { + addKeyword(text: string, kind: string): void + addText(text: string): void + toHTML(): string + finalize(): void + closeAllNodes(): void + openNode(kind: string): void + closeNode(): void + addSublanguage(emitter: Emitter, subLanguageName: string): void +} + +/* modes */ + +interface ModeCallbacks { + "on:end"?: Function, + "on:begin"?: Function, +} + +interface Mode extends ModeCallbacks, ModeDetails { + +} + +interface LanguageDetail { + name?: string + rawDefinition?: () => Language + aliases?: string[] + disableAutodetect?: boolean + contains: ("self"|Mode)[] + case_insensitive?: boolean + keywords?: Record | string + compiled?: boolean +} + +type Language = LanguageDetail & Partial + +interface CompiledLanguage extends LanguageDetail, CompiledMode { + compiled: true + contains: CompiledMode[] + keywords: Record +} + +type KeywordData = [string, number]; +type KeywordDict = Record + +type CompiledMode = Omit & + { + contains: CompiledMode[] + keywords: KeywordDict + data: Record + terminator_end: string + keywordPatternRe: RegExp + beginRe: RegExp + endRe: RegExp + illegalRe: RegExp + matcher: any + compiled: true + starts?: CompiledMode + parent?: CompiledMode + } + +interface ModeDetails { + begin?: RegExp | string + end?: RegExp | string + className?: string + contains?: ("self" | Mode)[] + endsParent?: boolean + endsWithParent?: boolean + endSameAsBegin?: boolean + skip?: boolean + excludeBegin?: boolean + excludeEnd?: boolean + returnBegin?: boolean + returnEnd?: boolean + __beforeBegin?: Function + parent?: Mode + starts?:Mode + lexemes?: string | RegExp + keywords?: Record | string + beginKeywords?: string + relevance?: number + illegal?: string | RegExp + variants?: Mode[] + cached_variants?: Mode[] + // parsed + subLanguage?: string | string[] + compiled?: boolean +}