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

Performance increase for no-absolute-path #843

Merged
merged 2 commits into from
Jul 6, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ function constant(value) {
return () => value
}

function isAbsolute(name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of removing this, could you export it and then import it in no-absolute-path?

return name.indexOf('/') === 0
}

export function isBuiltIn(name, settings) {
const extras = (settings && settings['import/core-modules']) || []
return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1
Expand Down Expand Up @@ -50,7 +46,6 @@ function isRelativeToSibling(name) {
}

const typeTest = cond([
[isAbsolute, constant('absolute')],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also keep this here, needed for import/order AFAIK

[isBuiltIn, constant('builtin')],
[isExternalModule, constant('external')],
[isScoped, constant('external')],
Expand Down
13 changes: 8 additions & 5 deletions src/rules/no-absolute-path.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

function reportIfMissing(context, node, name) {
if (importType(name, context) === 'absolute') {
function reportIfAbsolute(context, node, name) {
if (isAbsolute(name)) {
context.report(node, 'Do not import modules using an absolute path')
}
}

function isAbsolute(name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is replaced with import { isAbsolute } from '../core/importType'

return name.indexOf('/') === 0
}

module.exports = {
meta: {
docs: {},
Expand All @@ -15,11 +18,11 @@ module.exports = {
create: function (context) {
return {
ImportDeclaration: function handleImports(node) {
reportIfMissing(context, node, node.source.value)
reportIfAbsolute(context, node, node.source.value)
},
CallExpression: function handleRequires(node) {
if (isStaticRequire(node)) {
reportIfMissing(context, node, node.arguments[0].value)
reportIfAbsolute(context, node, node.arguments[0].value)
}
},
}
Expand Down
6 changes: 0 additions & 6 deletions tests/src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { testContext } from '../utils'
describe('importType(name)', function () {
const context = testContext()

it("should return 'absolute' for paths starting with a /", function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then put these tests back 😁

expect(importType('/', context)).to.equal('absolute')
expect(importType('/path', context)).to.equal('absolute')
expect(importType('/some/path', context)).to.equal('absolute')
})

it("should return 'builtin' for node.js modules", function() {
expect(importType('fs', context)).to.equal('builtin')
expect(importType('path', context)).to.equal('builtin')
Expand Down