-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
script-mime-types.ts
58 lines (50 loc) · 1.81 KB
/
script-mime-types.ts
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
import type { BuiltInParserName } from 'prettier';
import type { AttributeToken } from 'pug-lexer';
// NOTE: XML would be useful, but it's not a default parser.
// YAML is not official, but it costs nothing to support it right now.
const jsonSuffixRe: RegExp = /\+(json|yaml)$/i;
const wrappingQuotesRe: RegExp = /(^(["'`]))|((["'`])$)/g;
// Matches IANA media types to the required parser for them
// https://iana.org/assignments/media-types/media-types.xhtml
// Note: Don't need to put any suffixed types (+json, +xml) in here as it
// will be handled separately
// Why using a Map: https://github.com/prettier/plugin-pug/pull/248#discussion_r663854854
const scriptTypeToParserMap: Map<string, BuiltInParserName> = new Map([
['application/ecmascript', 'babel'],
['application/javascript', 'babel'],
['application/json', 'json'],
['text/ecmascript', 'babel'],
['text/javascript', 'babel'],
['text/markdown', 'markdown'],
['text/typescript', 'typescript'],
['module', 'babel'],
]);
/**
* Decides which parser to format script contents with.
*
* @param typeAttrToken Type token of the.
* @returns Parser name to parse contents with.
*/
export function getScriptParserName(
typeAttrToken?: AttributeToken,
): BuiltInParserName | undefined {
// Omission means Javascript
if (!typeAttrToken) {
return 'babel';
}
const typeRaw: string | boolean = typeAttrToken.val;
// If it's not a string, best not do anything
if (typeof typeRaw !== 'string') {
return;
}
const type: string = typeRaw.replaceAll(wrappingQuotesRe, '');
// Empty type is equivalent to omission
if (!type) {
return 'babel';
}
const suffixExec: RegExpExecArray | null = jsonSuffixRe.exec(type);
if (suffixExec) {
return suffixExec[1] as unknown as 'json' | 'yaml';
}
return scriptTypeToParserMap.get(type);
}