Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] use native JS instead of lodash #1419

Merged
merged 5 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"eslint-import-resolver-node": "^0.3.2",
"eslint-module-utils": "^2.4.0",
"has": "^1.0.3",
"lodash": "^4.17.11",
"minimatch": "^3.0.4",
"read-pkg-up": "^2.0.0",
"resolve": "^1.11.0"
Expand Down
27 changes: 11 additions & 16 deletions src/core/importType.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import cond from 'lodash/cond'
import coreModules from 'resolve/lib/core'
import { join } from 'path'

import resolve from 'eslint-module-utils/resolve'

function constant(value) {
return () => value
}

function baseModule(name) {
if (isScoped(name)) {
const [scope, pkg] = name.split('/')
Expand Down Expand Up @@ -76,17 +71,17 @@ function isRelativeToSibling(name) {
return /^\.[\\/]/.test(name)
}

const typeTest = cond([
[isAbsolute, constant('absolute')],
[isBuiltIn, constant('builtin')],
[isInternalModule, constant('internal')],
[isExternalModule, constant('external')],
[isScoped, constant('external')],
[isRelativeToParent, constant('parent')],
[isIndex, constant('index')],
[isRelativeToSibling, constant('sibling')],
[constant(true), constant('unknown')],
])
function typeTest(name, settings, path) {
if (isAbsolute(name, settings, path)) { return 'absolute' }
if (isBuiltIn(name, settings, path)) { return 'builtin' }
if (isInternalModule(name, settings, path)) { return 'internal' }
if (isExternalModule(name, settings, path)) { return 'external' }
if (isScoped(name, settings, path)) { return 'external' }
if (isRelativeToParent(name, settings, path)) { return 'parent' }
if (isIndex(name, settings, path)) { return 'index' }
if (isRelativeToSibling(name, settings, path)) { return 'sibling' }
return 'unknown'
}

export default function resolveImportType(name, context) {
return typeTest(name, context.settings, resolve(name, context))
Expand Down
9 changes: 4 additions & 5 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path'
import fs from 'fs'
import { isArray, isEmpty } from 'lodash'
import readPkgUp from 'read-pkg-up'
import minimatch from 'minimatch'
import resolve from 'eslint-module-utils/resolve'
Expand Down Expand Up @@ -31,15 +30,15 @@ function getDependencies(context, packageDir) {
peerDependencies: {},
}

if (!isEmpty(packageDir)) {
if (!isArray(packageDir)) {
if (packageDir && packageDir.length > 0) {
if (!Array.isArray(packageDir)) {
paths = [path.resolve(packageDir)]
} else {
paths = packageDir.map(dir => path.resolve(dir))
}
}

if (!isEmpty(paths)) {
if (paths.length > 0) {
// use rule config to find package.json
paths.forEach(dir => {
const _packageContent = extractDepFields(
Expand Down Expand Up @@ -70,7 +69,7 @@ function getDependencies(context, packageDir) {

return packageContent
} catch (e) {
if (!isEmpty(paths) && e.code === 'ENOENT') {
if (paths.length > 0 && e.code === 'ENOENT') {
context.report({
message: 'The package.json file could not be found.',
loc: { line: 0, column: 0 },
Expand Down
2 changes: 1 addition & 1 deletion tests/dep-time-travel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm install --no-save eslint@$ESLINT_VERSION --ignore-scripts || true
# completely remove the new typescript parser for ESLint < v5
if [[ "$ESLINT_VERSION" -lt "5" ]]; then
echo "Removing @typescript-eslint/parser..."
npm uninstall @typescript-eslint/parser
npm uninstall --no-save @typescript-eslint/parser
fi

# use these alternate typescript dependencies for ESLint < v4
Expand Down
12 changes: 7 additions & 5 deletions tests/src/rules/no-amd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

var ruleTester = new RuleTester()

Expand All @@ -25,11 +27,11 @@ ruleTester.run('no-amd', require('rules/no-amd'), {
'define("a")',
],

invalid: [
{ code: 'define([], function() {})', errors: [ { message: 'Expected imports instead of AMD define().' }] },
{ code: 'define(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD define().' }] },
invalid: semver.satisfies(eslintPkg.version, '< 4.0.0') ? [] : [
{ code: 'define([], function() {})', errors: [ { message: 'Expected imports instead of AMD define().' }] },
{ code: 'define(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD define().' }] },

{ code: 'require([], function() {})', errors: [ { message: 'Expected imports instead of AMD require().' }] },
{ code: 'require(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD require().' }] },
{ code: 'require([], function() {})', errors: [ { message: 'Expected imports instead of AMD require().' }] },
{ code: 'require(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD require().' }] },
],
})
10 changes: 7 additions & 3 deletions tests/src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

const EXPORT_MESSAGE = 'Expected "export" or "export default"'
, IMPORT_MESSAGE = 'Expected "import" instead of "require()"'
Expand Down Expand Up @@ -59,9 +61,11 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
invalid: [

// imports
{ code: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
...(semver.satisfies(eslintPkg.version, '< 4.0.0') ? [] : [
{ code: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
]),

// exports
{ code: 'exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
Expand Down