Skip to content

Commit

Permalink
chore: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 20, 2021
1 parent 152112f commit 5af8c02
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": [
"@nuxtjs/eslint-config"
"@nuxtjs/eslint-config-typescript"
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
],
"scripts": {
"build": "unbuild",
"lint": "eslint --ext .mjs lib",
"lint": "eslint --ext .ts src",
"release": "yarn test && yarn build && standard-version && npm publish && git push --follow-tags",
"test": "yarn build && mocha ./test/**/*.test.mjs"
},
"devDependencies": {
"@nuxtjs/eslint-config": "latest",
"@nuxtjs/eslint-config-typescript": "latest",
"@types/node": "latest",
"import-meta-resolve": "^1.1.1",
"chai": "^4.3.4",
Expand Down
10 changes: 5 additions & 5 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { builtinModules } from 'module'

export const BUILTIN_MODULES = new Set(builtinModules)

export function normalizeSlash(str) {
export function normalizeSlash (str) {
return str.replace(/\\/g, '/')
}

export function pcall(fn, ...args) {
export function pcall (fn, ...args) {
try {
return Promise.resolve(fn(...args)).catch(err => perr(err))
} catch (err) {
return perr(err)
}
}

export function perr(_err) {
export function perr (_err) {
const err = new Error(_err)
err.code = _err.code
Error.captureStackTrace(err, pcall)
return Promise.reject(err)
}

export function isObject(val) {
export function isObject (val) {
return val !== null && typeof val === 'object'
}

export function matchAll(regex, string, addition) {
export function matchAll (regex, string, addition) {
const matches = []
for (const match of string.matchAll(regex)) {
matches.push({
Expand Down
9 changes: 4 additions & 5 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,22 @@ export interface DefaultExport extends ESMExport {
type: 'default'
}


export const ESM_STATIC_IMPORT_RE = /^(?<=\s*)import\s*(["'\s]*(?<imports>[\w*${}\n\r\t, /]+)from\s*)?["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*$/gm
export const DYNAMIC_IMPORT_RE = /import\s*\((?<expression>(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm

export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(function|let|const|var|class))\s+(?<name>[\w$_]+)/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}/g
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g

export function findStaticImports(code: string): StaticImport[] {
export function findStaticImports (code: string): StaticImport[] {
return matchAll(ESM_STATIC_IMPORT_RE, code, { type: 'static' })
}

export function findDynamicImports(code: string): DynamicImport[] {
export function findDynamicImports (code: string): DynamicImport[] {
return matchAll(DYNAMIC_IMPORT_RE, code, { type: 'dynamic' })
}

export function parseStaticImport(matched: StaticImport): ParsedStaticImport {
export function parseStaticImport (matched: StaticImport): ParsedStaticImport {
const cleanedImports = (matched.imports || '')
.replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, '')
.replace(/\s+/g, ' ')
Expand All @@ -87,7 +86,7 @@ export function parseStaticImport(matched: StaticImport): ParsedStaticImport {
} as ParsedStaticImport
}

export function findExports(code: string): ESMExport[] {
export function findExports (code: string): ESMExport[] {
const declaredExports = matchAll(EXPORT_DECAL_RE, code, { type: 'declaration' })

const namedExports = matchAll(EXPORT_NAMED_RE, code, { type: 'named' })
Expand Down
10 changes: 5 additions & 5 deletions src/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export interface CommonjsContext {
require: NodeRequire
}

export function createCommonJS(url: string): CommonjsContext {
export function createCommonJS (url: string): CommonjsContext {
const __filename = fileURLToPath(url)
const __dirname = dirname(__filename)

// Lazy require
let _nativeRequire
const getNativeRequire = () => _nativeRequire || (_nativeRequire = createRequire(url))
function require(id) { return getNativeRequire()(id) }
function require (id) { return getNativeRequire()(id) }
require.resolve = (id, options) => getNativeRequire().resolve(id, options)

return {
Expand All @@ -27,7 +27,7 @@ export function createCommonJS(url: string): CommonjsContext {
} as CommonjsContext
}

export function interopDefault(sourceModule: any): any {
export function interopDefault (sourceModule: any): any {
if (!isObject(sourceModule) || !('default' in sourceModule)) {
return sourceModule
}
Expand All @@ -39,7 +39,7 @@ export function interopDefault(sourceModule: any): any {
Object.defineProperty(newModule, key, {
enumerable: false,
configurable: false,
get() { return newModule }
get () { return newModule }
})
}
} catch (_err) { }
Expand All @@ -49,7 +49,7 @@ export function interopDefault(sourceModule: any): any {
Object.defineProperty(newModule, key, {
enumerable: true,
configurable: true,
get() { return sourceModule[key] }
get () { return sourceModule[key] }
})
}
} catch (_err) { }
Expand Down
13 changes: 6 additions & 7 deletions src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export interface EvaluateOptions extends ResolveOptions {

const EVAL_ESM_IMPORT_RE = /(?<=import .* from ['"])([^'"]+)(?=['"])|(?<=export .* from ['"])([^'"]+)(?=['"])|(?<=import\s*['"])([^'"]+)(?=['"])|(?<=import\s*\(['"])([^'"]+)(?=['"]\))/g

export async function loadModule(id: string, opts: EvaluateOptions = {}): Promise<any> {
export async function loadModule (id: string, opts: EvaluateOptions = {}): Promise<any> {
const url = await resolve(id, opts)
const code = await loadURL(url)
return evalModule(code, { ...opts, url })
}

export async function evalModule(code: string, opts: EvaluateOptions = {}): Promise<any> {
export async function evalModule (code: string, opts: EvaluateOptions = {}): Promise<any> {
const transformed = await transformModule(code, opts)
const dataURL = toDataURL(transformed)
return import(dataURL).catch((err) => {
Expand All @@ -23,22 +23,21 @@ export async function evalModule(code: string, opts: EvaluateOptions = {}): Prom
})
}

export async function transformModule(code: string, opts: EvaluateOptions): Promise<string> {
export function transformModule (code: string, opts: EvaluateOptions): Promise<string> {
// Convert JSON to module
if (opts.url && opts.url.endsWith('.json')) {
return 'export default ' + code
return Promise.resolve('export default ' + code)
}


// Rewrite import.meta.url
if (opts.url) {
code = code.replace(/import\.meta\.url/g, `'${opts.url}'`)
}

return code
return Promise.resolve(code)
}

export async function resolveImports(code: string, opts: EvaluateOptions): Promise<string> {
export async function resolveImports (code: string, opts: EvaluateOptions): Promise<string> {
const imports = Array.from(code.matchAll(EVAL_ESM_IMPORT_RE)).map(m => m[0])
if (!imports.length) {
return code
Expand Down
14 changes: 7 additions & 7 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ResolveOptions {
conditions?: string[]
}

function _tryModuleResolve(id: string, url: URL, conditions: any): any | null {
function _tryModuleResolve (id: string, url: URL, conditions: any): any | null {
try {
return moduleResolve(id, url, conditions)
} catch (err) {
Expand All @@ -26,7 +26,7 @@ function _tryModuleResolve(id: string, url: URL, conditions: any): any | null {
}
}

function _resolve(id: string, opts: ResolveOptions = {}): string {
function _resolve (id: string, opts: ResolveOptions = {}): string {
// Skip if already has a protocol
if (/(node|data|http|https):/.test(id)) {
return id
Expand Down Expand Up @@ -71,26 +71,26 @@ function _resolve(id: string, opts: ResolveOptions = {}): string {
/**
* @deprecated please use `resolve` instead of `resolveSync`
*/
export function resolveSync(id: string, opts: ResolveOptions): string {
export function resolveSync (id: string, opts: ResolveOptions): string {
return _resolve(id, opts)
}

export function resolve(id: string, opts: ResolveOptions): Promise<string> {
export function resolve (id: string, opts: ResolveOptions): Promise<string> {
return pcall(resolveSync, id, opts)
}

/**
* @deprecated please use `resolvePath` instead of `resolvePathSync`
*/
export function resolvePathSync(id: string, opts: ResolveOptions) {
export function resolvePathSync (id: string, opts: ResolveOptions) {
return fileURLToPath(resolveSync(id, opts))
}

export function resolvePath(id: string, opts: ResolveOptions) {
export function resolvePath (id: string, opts: ResolveOptions) {
return pcall(resolvePathSync, id, opts)
}

export function createResolve(defaults: ResolveOptions) {
export function createResolve (defaults: ResolveOptions) {
return (id, url) => {
return resolve(id, { url, ...defaults })
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { fileURLToPath as _fileURLToPath } from 'url'
import { promises as fsp } from 'fs'
import { normalizeSlash, BUILTIN_MODULES } from './_utils'

export function fileURLToPath(id: string): string {
export function fileURLToPath (id: string): string {
if (typeof id === 'string' && !id.startsWith('file://')) {
return normalizeSlash(id)
}
return normalizeSlash(_fileURLToPath(id))
}

export function normalizeid(id: string): string {
export function normalizeid (id: string): string {
if (typeof id !== 'string') {
// @ts-ignore
id = id.toString()
Expand All @@ -24,12 +24,12 @@ export function normalizeid(id: string): string {
return 'file://' + normalizeSlash(id)
}

export async function loadURL(url: string): Promise<string> {
export async function loadURL (url: string): Promise<string> {
const code = await fsp.readFile(fileURLToPath(url), 'utf-8')
return code
}

export function toDataURL(code: string): string {
export function toDataURL (code: string): string {
const base64 = Buffer.from(code).toString('base64')
return `data:text/javascript;base64,${base64}`
}
39 changes: 36 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,16 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@nuxtjs/eslint-config@latest":
"@nuxtjs/eslint-config-typescript@latest":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@nuxtjs/eslint-config-typescript/-/eslint-config-typescript-6.0.1.tgz#11e91a5e25aca6855ec7525080da694c4b3cd4d4"
integrity sha512-ZK/C2ZtXrc2FlQwssc3gqc3K9RAtGF+OLGXRDtDxyrlhgPwAC4bc9McBGqUokdbE+pup0OA38PylPOhbWHZkrg==
dependencies:
"@nuxtjs/eslint-config" "6.0.1"
"@typescript-eslint/eslint-plugin" "^4.25.0"
"@typescript-eslint/parser" "^4.25.0"

"@nuxtjs/eslint-config@6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@nuxtjs/eslint-config/-/eslint-config-6.0.1.tgz#305450a71e362c26f99b3a7130be960015a96c9b"
integrity sha512-NISrmMx4J2usVDVLG7WFwTdfQaznHf/b2aKTb1o0TCxxXdY30UHLqEH+3MqpjY+0+UPM06YswFmOBjOg5y2BXQ==
Expand Down Expand Up @@ -397,7 +406,21 @@
dependencies:
"@types/node" "*"

"@typescript-eslint/experimental-utils@^4.0.1":
"@typescript-eslint/eslint-plugin@^4.25.0":
version "4.33.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.33.0.tgz#c24dc7c8069c7706bc40d99f6fa87edcb2005276"
integrity sha512-aINiAxGVdOl1eJyVjaWn/YcVAq4Gi/Yo35qHGCnqbWVz61g39D0h23veY/MA0rFFGfxK7TySg2uwDeNv+JgVpg==
dependencies:
"@typescript-eslint/experimental-utils" "4.33.0"
"@typescript-eslint/scope-manager" "4.33.0"
debug "^4.3.1"
functional-red-black-tree "^1.0.1"
ignore "^5.1.8"
regexpp "^3.1.0"
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/experimental-utils@4.33.0", "@typescript-eslint/experimental-utils@^4.0.1":
version "4.33.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.33.0.tgz#6f2a786a4209fa2222989e9380b5331b2810f7fd"
integrity sha512-zeQjOoES5JFjTnAhI5QY7ZviczMzDptls15GFsI6jyUOq0kOf9+WonkhtlIhh0RgHRnqj5gdNxW5j1EvAyYg6Q==
Expand All @@ -409,6 +432,16 @@
eslint-scope "^5.1.1"
eslint-utils "^3.0.0"

"@typescript-eslint/parser@^4.25.0":
version "4.33.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.33.0.tgz#dfe797570d9694e560528d18eecad86c8c744899"
integrity sha512-ZohdsbXadjGBSK0/r+d87X0SBmKzOq4/S5nzK6SBgJspFo9/CUDJ7hjayuze+JK7CZQLDMroqytp7pOcFKTxZA==
dependencies:
"@typescript-eslint/scope-manager" "4.33.0"
"@typescript-eslint/types" "4.33.0"
"@typescript-eslint/typescript-estree" "4.33.0"
debug "^4.3.1"

"@typescript-eslint/scope-manager@4.33.0":
version "4.33.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.33.0.tgz#d38e49280d983e8772e29121cf8c6e9221f280a3"
Expand Down Expand Up @@ -1927,7 +1960,7 @@ ignore@^4.0.6:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc"
integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==

ignore@^5.1.1, ignore@^5.1.4:
ignore@^5.1.1, ignore@^5.1.4, ignore@^5.1.8:
version "5.1.8"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
Expand Down

0 comments on commit 5af8c02

Please sign in to comment.