Skip to content

Commit 4700c7a

Browse files
committed
Remove 2 characters restriction
1 parent da8d2c7 commit 4700c7a

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ The paths `"src/js"` and `"./src/js"` behave the same.
9494

9595
### Custom rootPathPrefix
9696

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

101-
This **must** be 1 or 2 characters. Any additional characters are ignored.
102-
103101
```javascript
104102
//
105103
// Waiting this change: https://github.com/entwicklerstube/babel-plugin-root-import/pull/97
@@ -203,6 +201,10 @@ Sometimes tooling might not be up to scratch, meaning you lose features such as
203201

204202
## Change Log
205203

204+
#### 6.2.0 - 2019-05-09
205+
206+
- Remove the 2 characters restriction
207+
206208
#### 6.1.0 - 2018-06-23
207209

208210
- Supports babel 7

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-root-import",
3-
"version": "6.1.0",
3+
"version": "6.2.0",
44
"description": "Babel Plugin to enable relative root-import",
55
"author": "Michael J. Zoidl <github@michaelzoidl.com>",
66
"license": "MIT",

plugin/helper.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
11
import slash from 'slash';
22
import path from 'path';
3-
43
const root = slash(global.rootPath || process.cwd());
54

65
export const hasRootPathPrefixInString = (importPath, rootPathPrefix = '~') => {
7-
let containsRootPathPrefix = false;
8-
9-
if (typeof importPath === 'string') {
10-
if (importPath.substring(0, 1) === rootPathPrefix) {
11-
containsRootPathPrefix = true;
12-
}
13-
14-
const firstTwoCharactersOfString = importPath.substring(0, 2);
15-
if (firstTwoCharactersOfString === `${rootPathPrefix}/`) {
16-
containsRootPathPrefix = true;
17-
}
18-
}
19-
20-
return containsRootPathPrefix;
6+
return !!(typeof importPath === 'string' && importPath.indexOf(rootPathPrefix) === 0);
217
};
228

239
export const transformRelativeToRootPath = (importPath, rootPathSuffix, rootPathPrefix, sourceFile = '') => {
24-
let withoutRootPathPrefix = '';
2510
if (hasRootPathPrefixInString(importPath, rootPathPrefix)) {
26-
if (importPath.substring(0, 1) === '/') {
27-
withoutRootPathPrefix = importPath.substring(1, importPath.length);
28-
} else {
29-
withoutRootPathPrefix = importPath.substring(2, importPath.length);
30-
}
11+
const withoutRootPathPrefix = importPath.replace(rootPathPrefix, '');
3112

3213
const absolutePath = path.resolve(`${rootPathSuffix ? rootPathSuffix : './'}/${withoutRootPathPrefix}`);
3314
let sourcePath = sourceFile.substring(0, sourceFile.lastIndexOf('/'));

test/helper.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ describe('helper#transformRelativeToRootPath', () => {
2929
expect(result).to.not.equal(`${path.resolve('../shared')}/util/test/../test/test.js`);
3030
});
3131

32+
it('works with long prefix and special characters', () => {
33+
const rootPath = slash('./path');
34+
const result = transformRelativeToRootPath('common$^plop/some/path', '', 'common$^plop', 'some/file.js');
35+
expect(result).to.equal(rootPath);
36+
});
37+
3238
it('throws error if no string is passed', () => {
3339
expect(() => {
3440
transformRelativeToRootPath();
@@ -49,6 +55,13 @@ describe('helper#hasRootPathPrefixInString', () => {
4955
expect(withRootPathPrefix).to.be.true;
5056
});
5157

58+
it('check if "common" is at the beginning of the string', () => {
59+
const withoutRootPathPrefix = hasRootPathPrefixInString('some/path', 'common');
60+
const withRootPathPrefix = hasRootPathPrefixInString('common/some/path', 'common');
61+
expect(withoutRootPathPrefix).to.be.false;
62+
expect(withRootPathPrefix).to.be.true;
63+
});
64+
5265
it('returns false if no string is passed', () => {
5366
const nothingPassed = hasRootPathPrefixInString();
5467
const wrongTypePassed = hasRootPathPrefixInString([]);

0 commit comments

Comments
 (0)