Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
fix a bug... that I still can't explained (#5)
Browse files Browse the repository at this point in the history
Major refactoring.
Fix false positive in tests.

Thanks to @yormi
  • Loading branch information
yormi authored and per2plex committed Apr 26, 2016
1 parent dce2be8 commit d6964b2
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 211 deletions.
52 changes: 0 additions & 52 deletions src/helper.js

This file was deleted.

107 changes: 77 additions & 30 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,90 @@
import {
normalizeSourceRoot, normalizeFilename,
checkAndRemovePrefix, transformPath
} from './helper'
dirname,
join,
relative,
isAbsolute
} from 'path'
import slash from 'slash'

export default function() {
export default function () {
return {
visitor: {
ImportDeclaration(path, state) {
const projectPathSuffix = typeof state.opts.projectPathSuffix === 'string' ?
state.opts.projectPathSuffix :
''
ImportDeclaration (path, state) {
/*
* config: {
* projectRoot: babel option sourceRoot or process.cwd as fallback
* prefix: importPathPrefix provided by the user in the plugin config
* suffix: projectPathSuffix provided by the user in the plugin config
* }
*/
const config = extractConfig(state)

const importPathPrefix = typeof state.opts.importPathPrefix === 'string' ?
state.opts.importPathPrefix + '/':
'~/'
// file currently visited
const sourcePath = state.file.opts.filename

const [sourceRoot, suffixedSourceRoot] = normalizeSourceRoot(
state.file.opts.sourceRoot, projectPathSuffix
)

const filename = normalizeFilename(
state.file.opts.filename,
sourceRoot,
suffixedSourceRoot
)
if (sourcePath === 'unknown') {
return
}

if (!filename) return
invariants(config)
unixifyPaths(config) // works on windows too according to slash's doc

// string in the import statement
const importPath = path.node.source.value
const importPathWithoutPrefix = checkAndRemovePrefix(
importPath, importPathPrefix
)

if (importPathWithoutPrefix) {
path.node.source.value = transformPath(
importPathWithoutPrefix, filename, sourceRoot
)

if (isImportPathPrefixed(importPath, config.prefix)) {
const absoluteImportPath = getAbsoluteImportPath(importPath, config)

const absoluteSourcePath = getAbsoluteSourcePath(config.projectRoot, sourcePath)
const relativeImportPath = relative(dirname(absoluteSourcePath), absoluteImportPath)

path.node.source.value = './' + slash(relativeImportPath)
}
}

}
}
}
}

function extractConfig (state) {
return {
projectRoot: state.file.opts.sourceRoot || process.cwd(),
prefix: state.opts.importPathPrefix || '~/',
suffix: state.opts.projectPathSuffix || ''
}
}

function unixifyPaths (config) {
config.projectRoot = slash(config.projectRoot)
config.suffix = slash(config.suffix)
}

function invariants (state) {
if (typeof state.suffix !== 'string') {
throw new Error('The projectPathSuffix provided is not a string')
}

if (typeof state.prefix !== 'string') {
throw new Error('The projectPathSuffix provided is not a string')
}
}

function isImportPathPrefixed (targetPath, prefix) {
return (targetPath.lastIndexOf(prefix, 0) === 0)
}

function getAbsoluteImportPath (importPath, config) {
const importPathWithoutPrefix = importPath.substring(config.prefix.length)
const suffixedProjectPath = join(config.projectRoot, config.suffix)
return join(suffixedProjectPath, importPathWithoutPrefix)
}

function getAbsoluteSourcePath (projectRoot, sourcePath) {
// Some babel wrappers supply an absolute path already
// so we need to check for that.
if (isAbsolute(sourcePath)) {
return sourcePath
} else {
return join(projectRoot, sourcePath)
}
}
118 changes: 0 additions & 118 deletions test/helper.js

This file was deleted.

0 comments on commit d6964b2

Please sign in to comment.