-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
optimize no-cycle rule using strongly connected components #2998
Draft
soryy708
wants to merge
1
commit into
import-js:main
Choose a base branch
from
soryy708:leverage-scc-in-no-cycle
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||
import calculateScc from '@rtsao/scc'; | ||||||
import { hashObject } from 'eslint-module-utils/hash'; | ||||||
import resolve from 'eslint-module-utils/resolve'; | ||||||
import ExportMapBuilder from './exportMap/builder'; | ||||||
import childContext from './exportMap/childContext'; | ||||||
|
||||||
let cache = new Map(); | ||||||
|
||||||
export default class StronglyConnectedComponentsBuilder { | ||||||
static clearCache() { | ||||||
cache = new Map(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
this might be less GC intensive? i don't feel strongly tho There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. |
||||||
} | ||||||
|
||||||
static get(source, context) { | ||||||
const path = resolve(source, context); | ||||||
if (path == null) { return null; } | ||||||
return StronglyConnectedComponentsBuilder.for(childContext(path, context)); | ||||||
} | ||||||
|
||||||
static for(context) { | ||||||
const cacheKey = context.cacheKey || hashObject(context).digest('hex'); | ||||||
if (cache.has(cacheKey)) { | ||||||
return cache.get(cacheKey); | ||||||
} | ||||||
const scc = StronglyConnectedComponentsBuilder.calculate(context); | ||||||
cache.set(cacheKey, scc); | ||||||
return scc; | ||||||
} | ||||||
|
||||||
static calculate(context) { | ||||||
const exportMap = ExportMapBuilder.for(context); | ||||||
const adjacencyList = this.exportMapToAdjacencyList(exportMap); | ||||||
const calculatedScc = calculateScc(adjacencyList); | ||||||
return StronglyConnectedComponentsBuilder.calculatedSccToPlainObject(calculatedScc); | ||||||
} | ||||||
|
||||||
/** @returns {Map<string, Set<string>>} for each dep, what are its direct deps */ | ||||||
static exportMapToAdjacencyList(initialExportMap) { | ||||||
const adjacencyList = new Map(); | ||||||
// BFS | ||||||
function visitNode(exportMap) { | ||||||
if (!exportMap) { | ||||||
return; | ||||||
} | ||||||
exportMap.imports.forEach((v, importedPath) => { | ||||||
const from = exportMap.path; | ||||||
const to = importedPath; | ||||||
|
||||||
if (!adjacencyList.has(from)) { | ||||||
adjacencyList.set(from, new Set()); | ||||||
ljharb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
if (adjacencyList.get(from).has(to)) { | ||||||
return; // prevent endless loop | ||||||
} | ||||||
adjacencyList.get(from).add(to); | ||||||
visitNode(v.getter()); | ||||||
}); | ||||||
} | ||||||
visitNode(initialExportMap); | ||||||
// Fill gaps | ||||||
adjacencyList.forEach((values) => { | ||||||
values.forEach((value) => { | ||||||
if (!adjacencyList.has(value)) { | ||||||
adjacencyList.set(value, new Set()); | ||||||
} | ||||||
}); | ||||||
}); | ||||||
return adjacencyList; | ||||||
} | ||||||
|
||||||
/** @returns {Record<string, number>} for each key, its SCC's index */ | ||||||
static calculatedSccToPlainObject(sccs) { | ||||||
const obj = {}; | ||||||
sccs.forEach((scc, index) => { | ||||||
scc.forEach((node) => { | ||||||
obj[node] = index; | ||||||
}); | ||||||
}); | ||||||
return obj; | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import StronglyConnectedComponentsBuilder from '../../src/scc'; | ||
import ExportMapBuilder from '../../src/exportMap/builder'; | ||
|
||
function exportMapFixtureBuilder(path, imports) { | ||
return { | ||
path, | ||
imports: new Map(imports.map((imp) => [imp.path, { getter: () => imp }])), | ||
}; | ||
} | ||
|
||
describe('Strongly Connected Components Builder', () => { | ||
afterEach(() => ExportMapBuilder.for.restore()); | ||
afterEach(() => StronglyConnectedComponentsBuilder.clearCache()); | ||
|
||
describe('When getting an SCC', () => { | ||
const source = ''; | ||
const context = { | ||
settings: {}, | ||
parserOptions: {}, | ||
parserPath: '', | ||
}; | ||
|
||
describe('Given two files', () => { | ||
describe('When they don\'t cycle', () => { | ||
it('Should return foreign SCCs', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [exportMapFixtureBuilder('bar.js', [])]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 0 }); | ||
}); | ||
}); | ||
|
||
describe('When they do cycle', () => { | ||
it('Should return same SCC', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('foo.js', []), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0 }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Given three files', () => { | ||
describe('When they form a line', () => { | ||
describe('When A -> B -> C', () => { | ||
it('Should return foreign SCCs', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('buzz.js', []), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 2, 'bar.js': 1, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
|
||
describe('When A -> B <-> C', () => { | ||
it('Should return 2 SCCs, A on its own', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('buzz.js', [ | ||
exportMapFixtureBuilder('bar.js', []), | ||
]), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 0, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
|
||
describe('When A <-> B -> C', () => { | ||
it('Should return 2 SCCs, C on its own', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('buzz.js', []), | ||
exportMapFixtureBuilder('foo.js', []), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 1, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
|
||
describe('When A <-> B <-> C', () => { | ||
it('Should return same SCC', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('foo.js', []), | ||
exportMapFixtureBuilder('buzz.js', [ | ||
exportMapFixtureBuilder('bar.js', []), | ||
]), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When they form a loop', () => { | ||
it('Should return same SCC', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('buzz.js', [ | ||
exportMapFixtureBuilder('foo.js', []), | ||
]), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
|
||
describe('When they form a Y', () => { | ||
it('Should return 3 distinct SCCs', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', []), | ||
exportMapFixtureBuilder('buzz.js', []), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 2, 'bar.js': 0, 'buzz.js': 1 }); | ||
}); | ||
}); | ||
|
||
describe('When they form a Mercedes', () => { | ||
it('Should return 1 SCC', () => { | ||
sinon.stub(ExportMapBuilder, 'for').returns( | ||
exportMapFixtureBuilder('foo.js', [ | ||
exportMapFixtureBuilder('bar.js', [ | ||
exportMapFixtureBuilder('foo.js', []), | ||
exportMapFixtureBuilder('buzz.js', []), | ||
]), | ||
exportMapFixtureBuilder('buzz.js', [ | ||
exportMapFixtureBuilder('foo.js', []), | ||
exportMapFixtureBuilder('bar.js', []), | ||
]), | ||
]), | ||
); | ||
const actual = StronglyConnectedComponentsBuilder.for(source, context); | ||
expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this row and line 114-117 move after line 119?
when skipErrorMessagePath is false, there is no need to do scc check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the scc check is how we know if there's cycles or not.