Skip to content
Permalink
Browse files

fix: Fixed tslint notes

  • Loading branch information...
roman.vasilev
roman.vasilev committed Feb 19, 2019
1 parent df1145e commit cfd678eee956e0e7b273231c955971e7cf6bad63
Showing with 27 additions and 28 deletions.
  1. +1 −1 LICENSE
  2. +1 −0 package.json
  3. +0 −1 src/index.spec.ts
  4. +23 −26 src/index.ts
  5. +2 −0 tslint.json
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018
Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -19,6 +19,7 @@
"eslint": "eslint --ext ts src",
"eslint:fix": "eslint src --ext \"ts\" --fix",
"eslint:w": "sh Taskfile eslint_watch",
"tslint:fix": "tslint -p tsconfig.json --fix",
"lint:w": "concurrently -- \"npm run -s tsclint:w\" \"npm run -s eslint:w\"",
"semantic-release": "semantic-release",
"prepublishOnly": "sed -i -e 's/devDependencies/_devDependencies/g' package.json",
@@ -458,5 +458,4 @@ it.skip('export as namespace', async () => {
export = _;
export as namespace _;
`);
console.log("result", result);
});
@@ -2,10 +2,10 @@ import * as ts from 'typescript';
import { Entry } from './entry';
import fsReadFilePromise from 'fs-readfile-promise';
import * as resolve from 'resolve';
import * as path from 'path';
import path from 'path';
import flatten from 'lodash.flatten';
import debug from 'debug';
import rreaddir from 'recursive-readdir';
import recursiveReadDir from 'recursive-readdir';
import * as fs from 'fs';

const d = debug('esm-exports');
@@ -48,14 +48,14 @@ export async function main(target: string, options: WalkNodeOptions = {}): Promi
} else if (options.type === 'directory') {
directory = target;
try {
const files = await rreaddir(directory, [rreaddirIgnore]);
const files = await recursiveReadDir(directory, [rreaddirIgnore]);
return flatten(await Promise.all(files.map(filepath => main(filepath, { type: 'file', filepath }))));
} catch (err) {
return [];
}
} else if (options.type === 'module') {
try {
file = resolve.sync(target, resolveOptions as any);
file = resolve.sync(target, <any>resolveOptions);
} catch (err) {
return [];
}
@@ -69,7 +69,6 @@ export async function main(target: string, options: WalkNodeOptions = {}): Promi
// todo: remove hack
if (target === '@types/node') {
const entries = await main('node_modules/@types/node', { ...options, type: 'directory' });
// console.log("entries", entries.filter(x => x.module === '@types/node'));
options.result.push(...entries);
}
}
@@ -85,7 +84,7 @@ export async function main(target: string, options: WalkNodeOptions = {}): Promi
.filter(entry => !entry.name && entry.specifier)
.map(entry => {
try {
var filepath = resolve.sync(entry.specifier, { ...(resolveOptions as any), basedir: path.dirname(file) });
var filepath = resolve.sync(entry.specifier, { ...(<any>resolveOptions), basedir: path.dirname(file) });
} catch (err) {
return [];
}
@@ -102,27 +101,27 @@ export { main as esmExports };

function walkNode(node: ts.Node, options: WalkNodeOptions): any {
// console.log("node.kind", node.kind);
if ((isModuleExportsAssign(node) || isThisExportsAssign(node)) && (node as any).left.name && (node as any).left.name.kind === ts.SyntaxKind.Identifier) {
const name = (node as any).left.name.text;
if ((isModuleExportsAssign(node) || isThisExportsAssign(node)) && (<any>node).left.name && (<any>node).left.name.kind === ts.SyntaxKind.Identifier) {
const name = (<any>node).left.name.text;
options.result.push(new Entry({ ...options, name }));
} else if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
if (hasDeclareKeyword(node)) {
const moduleBlock = node.getChildren().find(c => c.kind === ts.SyntaxKind.ModuleBlock) as ts.Block | undefined;
const moduleBlock = <ts.Block | undefined>(node.getChildren().find(c => c.kind === ts.SyntaxKind.ModuleBlock));
if (!moduleBlock) {
return;
}
const declaredModule = (node as any).name.text;
const declaredModule = (<any>node).name.text;
moduleBlock.forEachChild(node => walkNode(node, { ...options, module: declaredModule }));
} else if (options.module) {
options.result.push(new Entry({ ...options, name: (node as any).name.text }));
options.result.push(new Entry({ ...options, name: (<any>node).name.text }));
}
} else if (node.kind === ts.SyntaxKind.ExportDeclaration) {
const specifier = ((node as ts.ExportDeclaration).moduleSpecifier as any);
const specifier = (<any>node).moduleSpecifier;
if (specifier) {
options.result.push(new Entry({ ...options, specifier: specifier.text }));
}
} else if (node.kind === ts.SyntaxKind.VariableDeclarationList && node.parent && hasExportModifier(node.parent)) {
(node as any).declarations.forEach(d => {
(<any>node).declarations.forEach(d => {
if (d.name.kind === ts.SyntaxKind.Identifier) {
options.result.push(new Entry({ ...options, name: d.name.text }));
} else if (d.name.kind === ts.SyntaxKind.ObjectBindingPattern) {
@@ -132,11 +131,11 @@ function walkNode(node: ts.Node, options: WalkNodeOptions): any {
}
});
} else if (node.kind === ts.SyntaxKind.ExportSpecifier) {
options.result.push(new Entry({ ...options, name: (node as any).name.text }));
options.result.push(new Entry({ ...options, name: (<any>node).name.text }));
} else if (node.kind === ts.SyntaxKind.ExportAssignment) {
const name = (node as any).expression.text;
const name = (<any>node).expression.text;
options.result.push(new Entry({ ...options, name, isDefault: true }));
if (options.module != null) {
if (options.module != undefined) {
let newModule = options.module;
if (options.isDeclarationFile) {
newModule = typedModule(newModule);
@@ -147,14 +146,14 @@ function walkNode(node: ts.Node, options: WalkNodeOptions): any {
}
});
}
} else if (isDeclaration(node) && (hasExportModifier(node) || options.module != null)) {
} else if (isDeclaration(node) && (hasExportModifier(node) || options.module != undefined)) {
const newModule = typedModule(options.module);
const isDefault = hasDefaultModifier(node);
options.result.push(new Entry({ ...options, name: (node as any).name.text, isDefault, module: newModule }));
options.result.push(new Entry({ ...options, name: (<any>node).name.text, isDefault, module: newModule }));
}
// else if (isDeclaration(node) && isInDeclaredModule(node)) {
// const isDefault = hasDefaultModifier(node);
// options.result.push(new Entry({ ...options, name: (node as any).name.text, isDefault, module: (node as any).parent.parent.name.text }));
// options.result.push(new Entry({ ...options, name: (<any>node).name.text, isDefault, module: (<any>node).parent.parent.name.text }));
// }
// todo: Check depth
node.forEachChild(node => walkNode(node, options));
@@ -173,10 +172,8 @@ function rreaddirIgnore(file: string, stats: fs.Stats): boolean {
if (resolveOptions.extensions.indexOf(ext) === -1) {
return true;
}
} else if (stats.isDirectory()) {
if (path.basename(file) === 'node_modules') {
return true;
}
} else if (stats.isDirectory() && path.basename(file) === 'node_modules') {
return true;
}
return false;
}
@@ -201,12 +198,12 @@ function isThisExportsAssign(node: any) {
function isInDeclaredModule(node: any) {
return node.parent && node.parent.kind === ts.SyntaxKind.ModuleBlock
&& node.parent.parent && node.parent.parent.kind === ts.SyntaxKind.ModuleDeclaration
&& node.parent.parent.modifiers && node.parent.parent.modifiers.find(m => m.kind === ts.SyntaxKind.DeclareKeyword) != null
&& node.parent.parent.name != null && node.parent.parent.name.text != null;
&& node.parent.parent.modifiers && node.parent.parent.modifiers.find(m => m.kind === ts.SyntaxKind.DeclareKeyword) != undefined
&& node.parent.parent.name != undefined && node.parent.parent.name.text != undefined;
}

function isModuleFromOptions(options: WalkNodeOptions) {
return options.module != null;
return options.module != undefined;
}

function getParentIf(node: ts.Node, syntaxKinds: ts.SyntaxKind[]) {
@@ -12,6 +12,7 @@
"node_modules/tslint-sonarts/lib/rules"
],
"rules": {
"triple-equals": [true, "allow-undefined-check", "allow-null-check"],
"no-use-before-declare": true,
"no-function-expression": false,
"member-access": false,
@@ -45,6 +46,7 @@
"no-reserved-keywords": false,
"newline-per-chained-call": false,
"no-increment-decrement": false,
"no-any": false,
".": false
}
}

0 comments on commit cfd678e

Please sign in to comment.
You can’t perform that action at this time.