Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
refactor(ember-order-imports): use source file location
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens committed Aug 8, 2019
1 parent 5a341f7 commit 9aba9c2
Show file tree
Hide file tree
Showing 23 changed files with 436 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@
This repo contains all of [Clark](https://github.com/ClarkSource)'s
[eslint](https://eslint.org/) configuration presets.

- [**`@clark/eslint-config`**](/packages/eslint-config): The base config that
all other configs extend from.
- [**`@clark/eslint-config`**](/packages/eslint-config):
The base config that all other configs extend from.
- [**`@clark/eslint-config-typescript`**](/packages/eslint-config-typescript):
Adds support for parsing TypeScript, as well as TypeScript-specific rules.
- [**`@clark/eslint-config-node`**](/packages/eslint-config-node): Adds
Node.js-specific rules and sets the environment.
- [**`@clark/eslint-config-node`**](/packages/eslint-config-node):
Adds Node.js-specific rules and sets the environment.
- [**`@clark/eslint-config-node-typescript`**](/packages/eslint-config-node-typescript):
Combines `eslint-config-node` and `eslint-config-typescript`.
- [**`@clark/eslint-config-node-order-imports`**](/packages/eslint-config-node-order-imports):
Sorts ES module imports for Node.js projects
Sorts ES module imports for Node.js projects.
- [**`@clark/eslint-config-browser`**](/packages/eslint-config-browser):
Extends from the base config and sets the browser environment.
- [**`@clark/eslint-config-ember`**](/packages/eslint-config-ember): Extends
from `eslint-config-browser` and adds Ember.js-specific rules to it.
- [**`@clark/eslint-config-ember`**](/packages/eslint-config-ember):
Extends from `eslint-config-browser` and adds Ember.js-specific rules to it.
- [**`@clark/eslint-config-ember-typescript`**](/packages/eslint-config-ember-typescript):
Combines `eslint-config-ember` and `eslint-config-typescript`.
- [**`@clark/eslint-config-ember-order-imports`**](/packages/eslint-config-ember-order-imports):
Sorts ES module imports for Ember.js projects
Sorts ES module imports for Ember.js projects.

There are also a few more plugins and utils in this repository:

- [**`@clark/eslint-plugin-import-helpers-with-package`**](/packages/eslint-plugin-import-helpers-with-package):
Extends [`eslint-plugin-import-helpers/order-imports`][order-imports] so that
it can infer the name of the package the file belongs to.
- [**`@clark/eslint-util-get-package`**](/packages/eslint-util-get-package):
Finds the `package.json` a source file belongs to.

[order-imports]: https://github.com/Tibfib/eslint-plugin-import-helpers/blob/master/docs/rules/order-imports.md
8 changes: 3 additions & 5 deletions packages/eslint-config-ember-order-imports/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const APP_NAME = require(`${process.cwd()}/package.json`).name;

module.exports = {
plugins: ['import-helpers'],
plugins: ['@clark/import-helpers-with-package'],
rules: {
/**
* This orders the the ES6 module imports.
Expand All @@ -12,7 +10,7 @@ module.exports = {
*
* @see https://github.com/skylines-project/skylines/blob/861a4b0d7025599c5546166253f342d1890590d4/ember/.eslintrc.js#L17-L33
*/
'import-helpers/order-imports': [
'@clark/import-helpers-with-package/order-imports': [
'error',
{
newlinesBetween: 'always',
Expand All @@ -35,7 +33,7 @@ module.exports = {
['/^ember-/'],
['module'],
['absolute'],
[`/^${APP_NAME}\\//`, `/^dummy\\//`],
['package', `/^dummy\\//`],
['parent', 'sibling', 'index']
],
alphabetize: { order: 'asc', ignoreCase: true }
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-ember-order-imports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"url": "https://github.com/ClarkSource/eslint-config/issues"
},
"dependencies": {
"eslint-plugin-import-helpers": "^1.0.2"
"@clark/eslint-plugin-import-helpers-with-package": "^1.0.8"
}
}
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 packages/eslint-plugin-import-helpers-with-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
!.eslintrc.js
8 changes: 8 additions & 0 deletions packages/eslint-plugin-import-helpers-with-package/.npmignore
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 packages/eslint-plugin-import-helpers-with-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `@clark/eslint-plugin-import-helpers-with-package`

[![BuildStatus](https://travis-ci.org/ClarkSource/eslint-config.svg)](https://travis-ci.org/ClarkSource/eslint-config)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![dependencies](https://david-dm.org/ClarkSource/eslint-config/status.svg?path=packages/eslint-plugin-import-helpers-with-package)](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-plugin-import-helpers-with-package)
[![devDependencies](https://david-dm.org/ClarkSource/eslint-config/dev-status.svg?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)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
[![CLARK Open Source](https://img.shields.io/badge/CLARK-Open%20Source-%232B6CDE.svg)](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
8 changes: 8 additions & 0 deletions packages/eslint-plugin-import-helpers-with-package/index.ts
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 packages/eslint-plugin-import-helpers-with-package/package.json
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"
}
}
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;
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
}
}
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;
}
4 changes: 4 additions & 0 deletions packages/eslint-util-get-package/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@clark/node-typescript'
};
3 changes: 3 additions & 0 deletions packages/eslint-util-get-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
*.js
!.eslintrc.js
8 changes: 8 additions & 0 deletions packages/eslint-util-get-package/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules/
types/

.eslintrc.js
.gitignore
tsconfig.json

*.ts
32 changes: 32 additions & 0 deletions packages/eslint-util-get-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# `@clark/eslint-util-get-package`

[![BuildStatus](https://travis-ci.org/ClarkSource/eslint-config.svg)](https://travis-ci.org/ClarkSource/eslint-config)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![dependencies](https://david-dm.org/ClarkSource/eslint-config/status.svg?path=packages/eslint-util-get-package)](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-util-get-package)
[![devDependencies](https://david-dm.org/ClarkSource/eslint-config/dev-status.svg?path=packages/eslint-util-get-package)](https://david-dm.org/ClarkSource/eslint-config?path=packages/eslint-util-get-package&type=dev)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
[![CLARK Open Source](https://img.shields.io/badge/CLARK-Open%20Source-%232B6CDE.svg)](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 packages/eslint-util-get-package/find-package-with-cache.ts
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;
}
Loading

0 comments on commit 9aba9c2

Please sign in to comment.