This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
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.
refactor(ember-order-imports): use source file location
- Loading branch information
1 parent
5a341f7
commit 9aba9c2
Showing
23 changed files
with
436 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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
4 changes: 4 additions & 0 deletions
4
packages/eslint-plugin-import-helpers-with-package/.eslintrc.js
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,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: '@clark/node-typescript' | ||
}; |
2 changes: 2 additions & 0 deletions
2
packages/eslint-plugin-import-helpers-with-package/.gitignore
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,2 @@ | ||
*.js | ||
!.eslintrc.js |
8 changes: 8 additions & 0 deletions
8
packages/eslint-plugin-import-helpers-with-package/.npmignore
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,8 @@ | ||
node_modules/ | ||
types/ | ||
|
||
.eslintrc.js | ||
.gitignore | ||
tsconfig.json | ||
|
||
*.ts |
13 changes: 13 additions & 0 deletions
13
packages/eslint-plugin-import-helpers-with-package/README.md
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,13 @@ | ||
# `@clark/eslint-plugin-import-helpers-with-package` | ||
|
||
[](https://travis-ci.org/ClarkSource/eslint-config) | ||
[](https://github.com/prettier/prettier) | ||
[](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-plugin-import-helpers-with-package) | ||
[](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-plugin-import-helpers-with-package&type=dev) | ||
[](https://lernajs.io/) | ||
[](https://www.clark.de/de/jobs) | ||
|
||
> Extends [`eslint-plugin-import-helpers/order-imports`][order-imports] so that | ||
> it can infer the name of the package the file belongs to. | ||
[order-imports]: https://github.com/Tibfib/eslint-plugin-import-helpers/blob/master/docs/rules/order-imports.md |
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,8 @@ | ||
import { rules as originalRules } from 'eslint-plugin-import-helpers'; | ||
|
||
import orderImports from './rules/order-imports'; | ||
|
||
export const rules = { | ||
...originalRules, | ||
'order-imports': orderImports | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/eslint-plugin-import-helpers-with-package/package.json
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,43 @@ | ||
{ | ||
"name": "@clark/eslint-plugin-import-helpers-with-package", | ||
"version": "1.8.0", | ||
"description": "Extends `import-helpers/order-imports` so that it can infer the name of the package the file belongs to", | ||
"keywords": [ | ||
"eslint", | ||
"eslint-plugin", | ||
"eslint-plugin-import", | ||
"order-imports", | ||
"eslint-plugin-import-helpers", | ||
"configurable", | ||
"import" | ||
], | ||
"author": "Jan Buschtöns <[email protected]>", | ||
"homepage": "https://github.com/ClarkSource/eslint-config/tree/master/packages/eslint-plugin-import-helpers-with-package", | ||
"license": "ISC", | ||
"main": "index.js", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ClarkSource/eslint-config.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ClarkSource/eslint-config/issues" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --ext ts .", | ||
"prepack": "tsc --build --force", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"dependencies": { | ||
"@clark/eslint-util-get-package": "^1.8.0", | ||
"eslint-plugin-import-helpers": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@clark/eslint-config-node-typescript": "^1.8.0", | ||
"@types/eslint": "^4.16.6", | ||
"eslint": "^6.1.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/eslint-plugin-import-helpers-with-package/rules/order-imports.ts
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,64 @@ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import getPackage from '@clark/eslint-util-get-package'; | ||
import { Rule } from 'eslint'; | ||
import { rules } from 'eslint-plugin-import-helpers'; | ||
|
||
const PACKAGE = 'package'; | ||
const MATCH_NONE = '/foo/'; | ||
|
||
export default { | ||
...rules['order-imports'], | ||
create(context) { | ||
const options = context.options[0] || {}; | ||
// eslint-disable-next-line prefer-destructuring | ||
const groups: (string | string[])[] = options.groups || []; | ||
|
||
let packageName: string | undefined; | ||
function getPackageName() { | ||
if (packageName) return packageName; | ||
|
||
const fileName = context.getFilename(); | ||
const packageJSON = getPackage(fileName); | ||
if (!packageJSON) { | ||
context.report({ | ||
message: `Could not find a valid 'package.json' for this file.`, | ||
node: context.getScope().block | ||
}); | ||
return MATCH_NONE; | ||
} | ||
if (!packageJSON.name) { | ||
context.report({ | ||
message: `Associated 'package.json' is missing a 'name' field.`, | ||
node: context.getScope().block | ||
}); | ||
return MATCH_NONE; | ||
} | ||
|
||
packageName = `/^${packageJSON.name.replace('/', '\\/')}/?/`; | ||
return packageName; | ||
} | ||
|
||
groups.forEach((block, i) => { | ||
if (block === PACKAGE) { | ||
groups[i] = getPackageName(); | ||
} else if (Array.isArray(block)) { | ||
block.forEach((group, j) => { | ||
if (group === PACKAGE) { | ||
block[j] = getPackageName(); | ||
} | ||
}); | ||
} else { | ||
context.report({ | ||
message: `Invalid 'groups' config: ${block}`, | ||
node: context.getScope().block | ||
}); | ||
} | ||
}); | ||
|
||
context.options[0] = options; | ||
options.groups = groups; | ||
|
||
return rules['order-imports'].create(context); | ||
} | ||
} as Rule.RuleModule; |
9 changes: 9 additions & 0 deletions
9
packages/eslint-plugin-import-helpers-with-package/tsconfig.json
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/eslint-plugin-import-helpers-with-package/types/eslint-plugin-import-helpers.d.ts
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,66 @@ | ||
declare module 'eslint-plugin-import-helpers' { | ||
import { Rule } from 'eslint'; | ||
import { Node } from 'estree'; | ||
|
||
interface OrderImportsRule extends Rule.RuleModule { | ||
meta: { | ||
type: 'suggestion'; | ||
docs: { | ||
url: 'https://github.com/Tibfib/eslint-plugin-import-helpers/blob/master/docs/rules/order-imports.md'; | ||
}; | ||
|
||
fixable: 'code'; | ||
schema: [ | ||
{ | ||
type: 'object'; | ||
properties: { | ||
groups: { | ||
type: 'array'; | ||
}; | ||
newlinesBetween: { | ||
enum: ['ignore', 'always', 'always-and-inside-groups', 'never']; | ||
}; | ||
alphabetize: { | ||
type: 'object'; | ||
properties: { | ||
order: { | ||
enum: ['ignore', 'asc', 'desc']; | ||
default: 'ignore'; | ||
}; | ||
ignoreCase: { | ||
type: 'boolean'; | ||
default: false; | ||
}; | ||
}; | ||
}; | ||
}; | ||
additionalProperties: false; | ||
} | ||
]; | ||
}; | ||
|
||
create(context: Rule.RuleContext): OrderImportsRuleListener; | ||
} | ||
|
||
interface OrderImportsRuleListener extends Rule.RuleListener { | ||
ImportDeclaration(node: Node): void; | ||
CallExpression(node: Node): void; | ||
'Program:exit'(): void; | ||
FunctionDeclaration(): void; | ||
FunctionExpression(): void; | ||
ArrowFunctionExpression(): void; | ||
BlockStatement(): void; | ||
ObjectExpression(): void; | ||
'FunctionDeclaration:exit'(): void; | ||
'FunctionExpression:exit'(): void; | ||
'ArrowFunctionExpression:exit'(): void; | ||
'BlockStatement:exit'(): void; | ||
'ObjectExpression:exit'(): void; | ||
} | ||
|
||
interface Rules { | ||
'order-imports': OrderImportsRule; | ||
} | ||
|
||
export const rules: Rules; | ||
} |
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,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: '@clark/node-typescript' | ||
}; |
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,3 @@ | ||
node_modules/ | ||
*.js | ||
!.eslintrc.js |
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,8 @@ | ||
node_modules/ | ||
types/ | ||
|
||
.eslintrc.js | ||
.gitignore | ||
tsconfig.json | ||
|
||
*.ts |
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,32 @@ | ||
# `@clark/eslint-util-get-package` | ||
|
||
[](https://travis-ci.org/ClarkSource/eslint-config) | ||
[](https://github.com/prettier/prettier) | ||
[](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-util-get-package) | ||
[](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-util-get-package&type=dev) | ||
[](https://lernajs.io/) | ||
[](https://www.clark.de/de/jobs) | ||
|
||
> Finds the `package.json` a source file belongs to. | ||
```ts | ||
import getPackage from '@clark/eslint-util-get-package'; | ||
|
||
export const rules = { | ||
'some-rule': { | ||
create(context) { | ||
const fileName = context.getFilename(); | ||
const packageJSON = getPackage(fileName); | ||
|
||
if (!packageJSON) | ||
throw new Error( | ||
`Could not find a 'package.json' that '${fileName}' belongs to.` | ||
); | ||
|
||
console.log(`'${fileName}' belongs to ${packageJSON.name}.`); | ||
|
||
// ... | ||
} | ||
} | ||
}; | ||
``` |
26 changes: 26 additions & 0 deletions
26
packages/eslint-util-get-package/find-package-with-cache.ts
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,26 @@ | ||
import { join } from 'path'; | ||
|
||
import { sync as findUpSync } from 'find-up'; | ||
|
||
import { findUpWithCache } from './find-up-with-cache'; | ||
|
||
const getPackagePath = (directory: string) => join(directory, 'package.json'); | ||
|
||
const matchPackage = (directory: string) => { | ||
const packagePath = getPackagePath(directory); | ||
return findUpSync.exists(directory) ? packagePath : undefined; | ||
}; | ||
|
||
export function findPackageWithCache(cwd: string) { | ||
const packagePath = findUpWithCache(matchPackage, { cwd }); | ||
if (!packagePath) return undefined; | ||
|
||
// `require` itself is cached | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const packageJSON = require(packagePath); | ||
|
||
if (!packageJSON && typeof packageJSON.name !== 'string') | ||
throw new TypeError(`'${packagePath}' is invalid.`); | ||
|
||
return packageJSON; | ||
} |
Oops, something went wrong.