/
index.js
67 lines (58 loc) · 2.27 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
/**
* GENERAL UTILITIES
*/
export const has = arr => Array.isArray(arr) && arr.length > 0;
/**
* @example node?.decorators?.find(decorator('Component'))
*/
export const decorator = type => decorator => decorator?.expression?.expression?.getText() === type || decorator?.expression?.getText() === type;
export function isBareModuleSpecifier(specifier) {
return !!specifier?.replace(/'/g, '')[0].match(/[@a-zA-Z]/g);
}
export function resolveModuleOrPackageSpecifier(moduleDoc, context, name) {
const foundImport = context?.imports?.find(_import => _import.name === name);
/* item is imported from another file */
if(foundImport) {
if(foundImport.isBareModuleSpecifier) {
/* import is from 3rd party package */
return { package: foundImport.importPath }
} else {
/* import is imported from a local module */
return { module: new URL(foundImport.importPath, `file:///${moduleDoc.path}`).pathname }
}
} else {
/* item is in current module */
return { module: moduleDoc.path }
}
}
export const toKebabCase = str => {
return str.split('').map((letter, idx) => {
return letter.toUpperCase() === letter
? `${idx !== 0 ? '-' : ''}${letter.toLowerCase()}`
: letter;
}).join('');
}
/**
* TS seems to struggle sometimes with the `.getText()` method on JSDoc annotations, like `@deprecated` in ts v4.0.0 and `@override` in ts v4.3.2
* This is a bug in TS, but still annoying, so we add some safety rails here
*/
export const safe = (cb, returnType = '') => {
try {
return cb();
} catch {
return returnType;
}
}
export function withErrorHandling(name, cb) {
try {
cb()
} catch(e) {
let errorMessage = '';
const externalError = `Looks like you've hit an error in third party plugin: ${name}. Please try to create a minimal reproduction and inform the author of the ${name} plugin.`;
const coreError = `Looks like you've hit an error in the core library. Please try to create a minimal reproduction at https://custom-elements-manifest.netlify.com and create an issue at: https://github.com/open-wc/custom-elements-manifest/issues`;
if(name) {
errorMessage = name.startsWith('CORE') ? coreError : externalError;
}
throw new Error(`[${name ?? 'unnamed-plugin'}]: ${e}\n\n${errorMessage}\n`);
}
}