Skip to content

Commit

Permalink
Fix ESLint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
renke committed Feb 2, 2019
1 parent 2e5801b commit 83422ce
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 187 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages/*/test
11 changes: 10 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
"rules": {
"@typescript-eslint/indent": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
"no-restricted-syntax": "off",
"@typescript-eslint/interface-name-prefix": "off",
"import/no-unresolved": "off",
"import/no-dynamic-require": "off",
"global-require": "off",
"no-use-before-define": ["error", "nofunc"],
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
"no-continue": "off",
"prefer-template": "off"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"*.ts": [
"npx import-sort-cli --write",
"prettier --write",
"eslint --fix",
"git add"
],
"*.{json,md}": [
Expand Down
6 changes: 6 additions & 0 deletions packages/atom-import-sort/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../.eslintrc",
"globals": {
"atom": false
}
}
4 changes: 2 additions & 2 deletions packages/atom-import-sort/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "loophole" {
export function allowUnsafeEval(f: () => any);
export function allowUnsafeNewFunction(f: () => any);
export function allowUnsafeEval(f: () => unknown);
export function allowUnsafeNewFunction(f: () => unknown);
}
22 changes: 12 additions & 10 deletions packages/atom-import-sort/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {dirname, extname} from "path";

// tslint:disable-next-line:no-implicit-dependencies
import {CompositeDisposable, TextEditor} from "atom";
import {CompositeDisposable, TextEditor} from "atom"; // eslint-disable-line
import sortImports, {ICodeChange} from "import-sort";
import {getConfig} from "import-sort-config";
import {allowUnsafeEval, allowUnsafeNewFunction} from "loophole";

// eslint-disable-next-line
export class Plugin {
public bufferWillSaveDisposables;

public editorObserverDisposable;

public config = {
Expand All @@ -20,8 +21,8 @@ export class Plugin {
},
};

public activate(state) {
(atom.config as any).observe(
public activate() {
atom.config.observe(
"atom-import-sort.sortOnSave",
(sortOnSave: boolean) => {
if (sortOnSave) {
Expand Down Expand Up @@ -69,6 +70,7 @@ export class Plugin {
}
}

// eslint-disable-next-line
private sortEditor(editor, notifyErrors = false) {
const scopeDescriptor = editor.getRootScopeDescriptor();

Expand Down Expand Up @@ -102,7 +104,7 @@ export class Plugin {
extension = ".ts";
}

directory = (atom.project as any).getPaths()[0];
[directory] = atom.project.getPaths();
}

if (!extension) {
Expand Down Expand Up @@ -143,17 +145,17 @@ export class Plugin {
const cursor = editor.getCursorBufferPosition();
const unsorted = editor.getText();

let changes: Array<ICodeChange>;
let changes: ICodeChange[];

allowUnsafeNewFunction(() => {
allowUnsafeEval(() => {
changes = sortImports(
({changes} = sortImports(
unsorted,
parser!,
style!,
parser,
style,
path,
rawConfig.options,
).changes;
));
});
});

Expand Down
6 changes: 6 additions & 0 deletions packages/import-sort-cli/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../.eslintrc",
"rules": {
"no-console": "off"
}
}
21 changes: 11 additions & 10 deletions packages/import-sort-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Usage: import-sort [OPTION]... [FILE/GLOB]...
.help()
.alias("help", "h");

const argv = yargs.argv;
const {argv} = yargs;

let filePatterns = argv._;

Expand Down Expand Up @@ -92,8 +92,8 @@ for (const filePath of filePaths) {
try {
sortResult = sortImports(
unsortedCode,
parser!,
style!,
parser,
style,
filePath,
rawConfig.options,
);
Expand All @@ -102,7 +102,7 @@ for (const filePath of filePaths) {
continue;
}

const {code: sortedCode, changes} = sortResult!;
const {code: sortedCode, changes} = sortResult;

const isDifferent = changes.length > 0;

Expand All @@ -126,21 +126,22 @@ function getAndCheckConfig(
): IResolvedConfig {
const resolvedConfig = getConfig(extension, fileDirectory);

throwIf(!resolvedConfig, `No configuration found for file type ${extension}`);
if (!resolvedConfig) {
throw new Error(`No configuration found for file type ${extension}`);
}

const rawParser = resolvedConfig!.config.parser;
const rawStyle = resolvedConfig!.config.style;
const rawParser = resolvedConfig.config.parser;
const rawStyle = resolvedConfig.config.style;

throwIf(!rawParser, `No parser defined for file type ${extension}`);
throwIf(!rawStyle, `No style defined for file type ${extension}`);

const parser = resolvedConfig!.parser;
const style = resolvedConfig!.style;
const {parser, style} = resolvedConfig;

throwIf(!parser, `Parser "${rawParser}" could not be resolved`);
throwIf(!style, `Style "${rawStyle}" could not be resolved`);

return resolvedConfig!;
return resolvedConfig;
}

function handleFilePathError(filePath, e) {
Expand Down
26 changes: 16 additions & 10 deletions packages/import-sort-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IConfigByGlobs {
export interface IConfig {
parser?: string;
style?: string;
options?: any;
options?: object;
}

export interface IResolvedConfig {
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getConfig(
const actualConfig = mergeConfigs([defaultConfig, packageConfig]);

if (!actualConfig) {
return;
return undefined;
}

const resolvedConfig = resolveConfig(actualConfig, directory);
Expand All @@ -60,7 +60,7 @@ function getConfigFromDirectory(
const packageConfigs = getAllConfigsFromDirectory(directory);

if (!packageConfigs) {
return;
return undefined;
}

return getConfigForExtension(packageConfigs, extension);
Expand All @@ -70,14 +70,16 @@ function getConfigForExtension(
configs: IConfigByGlobs,
extension: string,
): IConfig | undefined {
const foundConfigs: Array<IConfig | undefined> = Object.keys(configs).map(
const foundConfigs: (IConfig | undefined)[] = Object.keys(configs).map(
joinedGlobs => {
const globs = joinedGlobs.split(",").map(rawGlob => rawGlob.trim());
const config = configs[joinedGlobs];

if (globs.some(glob => minimatch(extension, glob))) {
return config;
}

return undefined;
},
);

Expand All @@ -97,22 +99,24 @@ function getAllConfigsFromDirectory(
const configsResult = configsLoader.searchSync(directory);

if (!configsResult) {
return;
return undefined;
}

return configsResult.config;
} catch (e) {
return;
// Do nothing…
}

return undefined;
}

function mergeConfigs(
rawConfigs: Array<IConfig | undefined>,
rawConfigs: (IConfig | undefined)[],
): IConfig | undefined {
const configs = rawConfigs.filter(rawConfig => !!rawConfig) as Array<IConfig>;
const configs = rawConfigs.filter(rawConfig => !!rawConfig) as IConfig[];

if (configs.length === 0) {
return;
return undefined;
}

return configs.reduce((previousConfig, currentConfig) => {
Expand All @@ -134,7 +138,7 @@ function mergeConfigs(
config.options = currentConfig.options;
}

return config!;
return config;
});
}

Expand Down Expand Up @@ -182,4 +186,6 @@ function resolveModule(module: string, directory?: string): string | undefined {
if (defaultPath) {
return defaultPath;
}

return undefined;
}
Loading

0 comments on commit 83422ce

Please sign in to comment.