Skip to content

Commit

Permalink
Merge pull request #122 from MichaelBitard/remove-2-characters-restri…
Browse files Browse the repository at this point in the history
…ction

Lift the 2 characters restriction
  • Loading branch information
brigand authored May 11, 2019
2 parents da8d2c7 + 4700c7a commit 0d2e172
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ The paths `"src/js"` and `"./src/js"` behave the same.

### Custom rootPathPrefix

If you don't like the `~` syntax you can use your own symbol (for example an `#` symbol or `\`). Using
If you don't like the `~` syntax you can use your own symbol (for example an `#` symbol or `\` or anything you want). Using
`@` is not recommended, as recent versions of NPM allow `@` in package names. `~` is the default since
it's very unlikely to conflict with anything (and wouldn't be expanded to HOME anyway).

This **must** be 1 or 2 characters. Any additional characters are ignored.

```javascript
//
// Waiting this change: https://github.com/entwicklerstube/babel-plugin-root-import/pull/97
Expand Down Expand Up @@ -203,6 +201,10 @@ Sometimes tooling might not be up to scratch, meaning you lose features such as

## Change Log

#### 6.2.0 - 2019-05-09

- Remove the 2 characters restriction

#### 6.1.0 - 2018-06-23

- Supports babel 7
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-root-import",
"version": "6.1.0",
"version": "6.2.0",
"description": "Babel Plugin to enable relative root-import",
"author": "Michael J. Zoidl <github@michaelzoidl.com>",
"license": "MIT",
Expand Down
23 changes: 2 additions & 21 deletions plugin/helper.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import slash from 'slash';
import path from 'path';

const root = slash(global.rootPath || process.cwd());

export const hasRootPathPrefixInString = (importPath, rootPathPrefix = '~') => {
let containsRootPathPrefix = false;

if (typeof importPath === 'string') {
if (importPath.substring(0, 1) === rootPathPrefix) {
containsRootPathPrefix = true;
}

const firstTwoCharactersOfString = importPath.substring(0, 2);
if (firstTwoCharactersOfString === `${rootPathPrefix}/`) {
containsRootPathPrefix = true;
}
}

return containsRootPathPrefix;
return !!(typeof importPath === 'string' && importPath.indexOf(rootPathPrefix) === 0);
};

export const transformRelativeToRootPath = (importPath, rootPathSuffix, rootPathPrefix, sourceFile = '') => {
let withoutRootPathPrefix = '';
if (hasRootPathPrefixInString(importPath, rootPathPrefix)) {
if (importPath.substring(0, 1) === '/') {
withoutRootPathPrefix = importPath.substring(1, importPath.length);
} else {
withoutRootPathPrefix = importPath.substring(2, importPath.length);
}
const withoutRootPathPrefix = importPath.replace(rootPathPrefix, '');

const absolutePath = path.resolve(`${rootPathSuffix ? rootPathSuffix : './'}/${withoutRootPathPrefix}`);
let sourcePath = sourceFile.substring(0, sourceFile.lastIndexOf('/'));
Expand Down
13 changes: 13 additions & 0 deletions test/helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe('helper#transformRelativeToRootPath', () => {
expect(result).to.not.equal(`${path.resolve('../shared')}/util/test/../test/test.js`);
});

it('works with long prefix and special characters', () => {
const rootPath = slash('./path');
const result = transformRelativeToRootPath('common$^plop/some/path', '', 'common$^plop', 'some/file.js');
expect(result).to.equal(rootPath);
});

it('throws error if no string is passed', () => {
expect(() => {
transformRelativeToRootPath();
Expand All @@ -49,6 +55,13 @@ describe('helper#hasRootPathPrefixInString', () => {
expect(withRootPathPrefix).to.be.true;
});

it('check if "common" is at the beginning of the string', () => {
const withoutRootPathPrefix = hasRootPathPrefixInString('some/path', 'common');
const withRootPathPrefix = hasRootPathPrefixInString('common/some/path', 'common');
expect(withoutRootPathPrefix).to.be.false;
expect(withRootPathPrefix).to.be.true;
});

it('returns false if no string is passed', () => {
const nothingPassed = hasRootPathPrefixInString();
const wrongTypePassed = hasRootPathPrefixInString([]);
Expand Down

0 comments on commit 0d2e172

Please sign in to comment.