-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add support for interpolation in targets
- Loading branch information
1 parent
de2ddcb
commit b486d7f
Showing
6 changed files
with
557 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { resolve } from 'path'; | ||
import { getValue, interpolate } from './path-reader'; | ||
|
||
const PWD = process.cwd(); | ||
|
||
describe('getValue', () => { | ||
type Query = string; | ||
type ExpectedResult = string; | ||
type Assertion = [Query, ExpectedResult]; | ||
type AbsolutePath = string; | ||
type Test = [AbsolutePath, Assertion[]]; | ||
const tests: Test[] = [ | ||
[ | ||
resolve(PWD, './src/lib/module.js'), | ||
[ | ||
['...', 'src'], | ||
['..', 'lib'], | ||
['.', 'module.js'], | ||
['ancestors.0', 'lib'], | ||
['ancestors.1', 'src'], | ||
['dirs.0', 'src'], | ||
['dirs.1', 'lib'], | ||
['exts.0', 'js'], | ||
['exts.1', ''], | ||
['base', 'module.js'], | ||
['dir', 'src/lib'], | ||
['ext', 'js'], | ||
['name', 'module'], | ||
['rootDir', PWD], | ||
['invalid', ''], | ||
['unrecognised', ''] | ||
] | ||
], | ||
[ | ||
resolve(PWD, './src/lib/module.spec.js'), | ||
[ | ||
['...', 'src'], | ||
['..', 'lib'], | ||
['.', 'module.spec.js'], | ||
['ancestors.0', 'lib'], | ||
['ancestors.1', 'src'], | ||
['dirs.0', 'src'], | ||
['dirs.1', 'lib'], | ||
['exts.0', 'spec'], | ||
['exts.1', 'js'], | ||
['base', 'module.spec.js'], | ||
['dir', 'src/lib'], | ||
['ext', 'spec.js'], | ||
['name', 'module'], | ||
['rootDir', PWD], | ||
['invalid', ''], | ||
['unrecognised', ''] | ||
] | ||
], | ||
[ | ||
resolve(PWD, './.babelrc'), | ||
[ | ||
['...', ''], | ||
['..', ''], | ||
['.', '.babelrc'], | ||
['ancestors.0', ''], | ||
['ancestors.1', ''], | ||
['dirs.0', ''], | ||
['dirs.1', ''], | ||
['exts.0', 'babelrc'], | ||
['exts.1', ''], | ||
['base', '.babelrc'], | ||
['dir', ''], | ||
['ext', 'babelrc'], | ||
['name', ''], | ||
['rootDir', PWD], | ||
['invalid', ''], | ||
['unrecognised', ''] | ||
] | ||
], | ||
[ | ||
resolve(PWD, './.eslintrc.js'), | ||
[ | ||
['...', ''], | ||
['..', ''], | ||
['.', '.eslintrc.js'], | ||
['ancestors.0', ''], | ||
['ancestors.1', ''], | ||
['dirs.0', ''], | ||
['dirs.1', ''], | ||
['exts.0', 'eslintrc'], | ||
['exts.1', 'js'], | ||
['base', '.eslintrc.js'], | ||
['dir', ''], | ||
['ext', 'eslintrc.js'], | ||
['name', ''], | ||
['rootDir', PWD], | ||
['invalid', ''], | ||
['unrecognised', ''] | ||
] | ||
] | ||
]; | ||
|
||
tests.forEach(([absolutePath, assertions]) => { | ||
describe(`when path is '${absolutePath}'`, () => { | ||
assertions.forEach(([query, expected]) => { | ||
describe(`when query is '${query}'`, () => { | ||
it(`returns '${expected}'`, () => { | ||
expect(getValue(query, absolutePath)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('interpolate', () => { | ||
type Template = string; | ||
type ExpectedResult = string; | ||
type Assertion = [Template, ExpectedResult]; | ||
type AbsolutePath = string; | ||
type Test = [AbsolutePath, Assertion[]]; | ||
const tests: Test[] = [ | ||
[ | ||
resolve(PWD, './src/lib/module.spec.js'), | ||
[ | ||
[ | ||
'{rootDir}/test/{ancestors.0}/{name}.js', | ||
resolve(PWD, './test/lib/module.js') | ||
], | ||
['{rootDir}/test/{..}/{name}.js', resolve(PWD, './test/lib/module.js')] | ||
] | ||
] | ||
]; | ||
|
||
tests.forEach(([absolutePath, assertions]) => { | ||
describe(`when path is '${absolutePath}'`, () => { | ||
assertions.forEach(([template, expected]) => { | ||
describe(`when template is '${template}'`, () => { | ||
it(`returns '${expected}'`, () => { | ||
expect(interpolate(template, absolutePath)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { basename, dirname, relative, sep } from 'path'; | ||
import { getIn } from './get-in'; | ||
|
||
const getAncestors = (absPath: string) => | ||
getDir(absPath) | ||
.split(sep) | ||
.reverse(); | ||
const getAncestorsIndex = (query: string) => query.replace('ancestors.', ''); | ||
const getBase = (absPath: string) => basename(absPath); | ||
const getDir = (absPath: string) => relative(process.cwd(), dirname(absPath)); | ||
const getDirs = (absPath: string) => getDir(absPath).split(sep); | ||
const getDirsIndex = (query: string) => query.replace('dirs.', ''); | ||
const getExt = (absPath: string) => | ||
basename(absPath).slice(basename(absPath).indexOf('.') + 1); | ||
const getExts = (absPath: string) => | ||
getExt(absPath) | ||
.split('.') | ||
.filter(Boolean); | ||
const getExtsIndex = (query: string) => query.replace('exts.', ''); | ||
const getName = (absPath: string) => | ||
basename(absPath).slice(0, basename(absPath).indexOf('.')); | ||
const getRootDir = () => process.cwd(); | ||
|
||
export const getValue = (query: string, absPath: string) => { | ||
if (query === 'rootDir') { | ||
return getRootDir(); | ||
} | ||
if (query === 'dir') { | ||
return getDir(absPath); | ||
} | ||
if (query === 'base' || query === '.') { | ||
return getBase(absPath); | ||
} | ||
if (query === 'name') { | ||
return getName(absPath); | ||
} | ||
if (query === 'ext') { | ||
return getExt(absPath); | ||
} | ||
if (query.startsWith('dirs.')) { | ||
return getIn(getDirsIndex(query), getDirs(absPath), ''); | ||
} | ||
if (query.startsWith('exts.')) { | ||
return getIn(getExtsIndex(query), getExts(absPath), ''); | ||
} | ||
if (query.startsWith('ancestors.')) { | ||
return getIn(getAncestorsIndex(query), getAncestors(absPath), ''); | ||
} | ||
if (query.search(/^\.+$/) !== -1) { | ||
return getIn(query.length - 2, getAncestors(absPath), ''); | ||
} | ||
return ''; | ||
}; | ||
|
||
export const interpolate = (template: string, absPath: string) => | ||
template.replace(/{([^,}]+)}/g, (_, query) => getValue(query, absPath)); |
Oops, something went wrong.