Skip to content

Commit

Permalink
feat: add external modules refs corrector
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 4, 2020
1 parent 2507392 commit 6b16beb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/main/ts/replacer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IReplacer} from './interface'
import {IReplacer, IReplacerFactory} from './interface'

export const replaceExportMain: IReplacer = {
from: '\texport = main;',
Expand All @@ -20,3 +20,30 @@ export const replaceBrokenModulePrefix: IReplacer = (pattern => ({
return `${module}index' {`
}
}))(/(declare module '.+\/target\/es5\/)[^/]*\/src\/main\/index'.+/)

export const replaceModuleTypeRefs: IReplacer = {
from: /\/\/\/.+/,
to: ''
}

export const replaceEmptyLines: IReplacer = {
from: /^\s*[\r\n]/gm,
to: ''
}

export const replaceLocalModulesScope: IReplacerFactory = ({dtsData, prefix}) => {
const declaredModules = (dtsData.match(/declare module '.*'/g) || []).map(v => v.slice(16, -1))

return {
from: /import .+ from '(.+)'/g,
to: (line: string) => {
const re = /^(.+from ')([^']+)('.*)$/
const [, pre, module, post] = re.exec(line) || []
const name = declaredModules.includes(module)
? module
: module.replace(prefix + '/', '')

return `${pre}${name}${post}`
}
}
}
50 changes: 48 additions & 2 deletions src/test/ts/replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
replaceExportMain,
replaceImportMain,
replaceBrokenModulePrefix,
replaceModuleTypeRefs,
replaceEmptyLines,
replaceLocalModulesScope,
} from '../../main/ts/replacer'

describe('replacer', () => {
Expand All @@ -18,17 +21,22 @@ describe('replacer', () => {
if (typeof to === 'function') {
expect(to(input, '')).toBe(output)
}

if (typeof to === 'string') {
expect(to).toBe(output)
}
}

it('#replaceExportMain', () => {
assertReplacement(replaceExportMain, '\texport = main;', 'bar')
assertReplacement(replaceExportMain, '\texport = main;', '')
})

it('#replaceImportMain', () => {
assertReplacement(
replaceImportMain,
' import main = require(\'foo\');',
' export * from \'foo\';')
' export * from \'foo\';'
)
})

it('#replaceBrokenModulePrefix', () => {
Expand All @@ -37,4 +45,42 @@ describe('replacer', () => {
'declare module \'@qiwi/foo/target/es5//src/main/index\' {',
'declare module \'@qiwi/foo/target/es5/index\' {')
})

it('#replaceModuleTypeRefs', () => {
assertReplacement(
replaceModuleTypeRefs,
'/// <reference path="./common/common.d.ts" />',
''
)
})

it('#replaceEmptyLines', () => {
assertReplacement(
replaceEmptyLines,
`
`,
''
)
})

it('#replaceLocalModulesScope', () => {
const dtsData = `
declare module '@qiwi/decorator-utils/target/es5/utils' {
\t/** @module @qiwi/decorator-utils */
\timport { IInstance } from '@qiwi/decorator-utils/target/es5/interface';
\timport get from '@qiwi/decorator-utils/target/es5/lodash.get';
\timport set from '@qiwi/decorator-utils/target/es5/lodash.set';
\texport { get, set, };
\texport function getPrototypeMethods(instance: IInstance): PropertyDescriptorMap;
}
`
const prefix = '@qiwi/decorator-utils/target/es5'
const replacer = replaceLocalModulesScope({dtsData, prefix})
assertReplacement(
replacer,
`\timport get from '@qiwi/decorator-utils/target/es5/lodash.get';`,
`\timport get from 'lodash.get';`
)
})
})

0 comments on commit 6b16beb

Please sign in to comment.