-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* eslint * new plugin * delete files * change * Renamed folder + removed configs + removed fit * Update snapshots temporarily * add local rule to check dependencies * -- * commit before merging from master * Align dependencies + cleanup * More lint fixes * More fixes * add name of the undefined * change * make all the files run * split rule to different files * change structure and naming * update lock file * f * Upgrade to TSESLint 6.6.x + rename to undefined-dependency * add simple test * ignore node modules in lint * - * - * Update ci.yml * comment test * change deps * changes deps * empty commit * empty commit * change deps * update yarn * update deps * update deps * update deps * update yarn * update ci * change to yarn * add build --------- Co-authored-by: Guy Carmeli <[email protected]> Co-authored-by: Guy Carmeli <>
- Loading branch information
Showing
24 changed files
with
4,515 additions
and
4,117 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
dist/* | ||
dist/*, |
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 |
---|---|---|
|
@@ -16,15 +16,15 @@ jobs: | |
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
cache: npm | ||
cache-dependency-path: documentation/package-lock.json | ||
cache: yarn | ||
cache-dependency-path: documentation/yarn.lock | ||
|
||
- name: Install dependencies | ||
working-directory: documentation | ||
run: npm ci | ||
run: yarn install --frozen-lockfile | ||
- name: Build documentation | ||
working-directory: documentation | ||
run: npm run build | ||
run: yarn build | ||
|
||
- name: Setup EnhanceDocs | ||
uses: enhancedocs/[email protected] | ||
|
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 |
---|---|---|
|
@@ -117,5 +117,8 @@ buck-out/ | |
|
||
main.jsbundle.map | ||
index.android.bundle.map | ||
|
||
.jest_cache | ||
.eslintcache | ||
**/.docusaurus | ||
coverage/* |
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 |
---|---|---|
|
@@ -17,4 +17,5 @@ tsconfig.json | |
tsconfig.prod.json | ||
global.d.ts | ||
README.md | ||
babel.config.js | ||
babel.config.js | ||
eslint-local-rules.js |
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 |
---|---|---|
@@ -1 +1 @@ | ||
14 | ||
16.20.0 |
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,5 @@ | ||
{ | ||
"recommendations": [ | ||
"adpyke.codesnap" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"cSpell.words": [ | ||
"estree", | ||
"TSES", | ||
"unmagler" | ||
"Middlewares", | ||
"MVVM", | ||
"preconfigured", | ||
|
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,5 @@ | ||
const {undefinedDependencies} = require('./dist/eslint/undefinedDependencies/ruleConfiguration') | ||
|
||
module.exports = { | ||
'undefined-dependency': undefinedDependencies | ||
}; |
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,115 @@ | ||
import { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
import * as fs from 'fs'; | ||
import { parse } from '@typescript-eslint/parser'; | ||
import path= require('path') ; | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
|
||
export type MessageIds = 'undefinedDependency'; | ||
|
||
export function getSubGraphs(decorators: TSESTree.Decorator[]) { | ||
const args = (decorators[0].expression as TSESTree.CallExpression).arguments; | ||
if (args) { | ||
for (let i = 0; i < args.length; i++) { | ||
if (args[i].type === TSESTree.AST_NODE_TYPES.ObjectExpression) { | ||
const { properties } = (args[i] as TSESTree.ObjectExpression); | ||
if (properties) { | ||
for (let j = 0; j < properties.length; j++) { | ||
if (((properties[j] as TSESTree.Property).key as TSESTree.Identifier).name === 'subgraphs') { | ||
return ((properties[j] as TSESTree.Property).value as TSESTree.ArrayExpression) | ||
.elements.map((subGraph) => (subGraph as TSESTree.Identifier).name); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
export function getDependenciesFromSubgraphs( | ||
imports: TSESTree.ImportDeclaration[], | ||
subGraphs:string[], | ||
context:Readonly<TSESLint.RuleContext<MessageIds, []>>, | ||
) { | ||
const paths:Record<string, string>[] = []; | ||
const dependencies: string[] = []; | ||
imports.forEach((el) => { | ||
el.specifiers.forEach((specifier) => { | ||
if (subGraphs.includes(specifier.local.name)) { | ||
paths.push({ path: el.source.value, import: specifier.local.name }); | ||
} | ||
}); | ||
}); | ||
paths.forEach((el) => { | ||
const filePath = path.join(path.dirname(context.getFilename()), `${el['path']}.ts`); | ||
const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
const fileAST = parse( | ||
fileContent, | ||
{ | ||
ecmaVersion: 9, | ||
ecmaFeatures: { | ||
globalReturn: false, | ||
jsx: true, | ||
}, | ||
sourceType: 'module', | ||
comment: true, | ||
attachComment: true, | ||
tokens: true, | ||
loc: true, | ||
range: true, | ||
filePath, | ||
}, | ||
); | ||
dependencies.push(...mapFunctions( | ||
(fileAST.body[fileAST.body.length - 1] as TSESTree.ExportDefaultDeclaration) | ||
.declaration as TSESTree.ClassDeclaration, | ||
)); | ||
}); | ||
return dependencies; | ||
} | ||
export function mapFunctions(node: TSESTree.ClassDeclaration) { | ||
const { body } = node.body; | ||
const existingDependencies: string[] = []; | ||
body.forEach((el) => { | ||
if (el.type === TSESTree.AST_NODE_TYPES.MethodDefinition) { | ||
const decorators = (el)?.decorators; | ||
if (decorators) { | ||
if (decorators.map((decorator) => getDecoratorName(decorator)).includes('Provides')) { | ||
existingDependencies.push(((el as TSESTree.MethodDefinition).key as TSESTree.Identifier).name); | ||
} | ||
} | ||
} | ||
}); | ||
return existingDependencies; | ||
} | ||
export function checkDependencies(node: TSESTree.ClassDeclaration, existingDependencies: string[]) { | ||
const body = node?.body?.body; | ||
for (let j = 0; j < body.length; j++) { | ||
if (body[j].type === TSESTree.AST_NODE_TYPES.MethodDefinition | ||
&& ((body[j] as TSESTree.MethodDefinition).key as TSESTree.Identifier).name !== 'constructor') { | ||
const params = (body[j] as TSESTree.MethodDefinition).value?.params; | ||
if (params) { | ||
for (let i = 0; i < params.length; i++) { | ||
if (!existingDependencies.includes((params[i] as TSESTree.Identifier).name)) { | ||
return { | ||
value: false, | ||
param: (params[i] as TSESTree.Identifier).name, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return { value: true }; | ||
} | ||
export function getDecoratorName(decorator: TSESTree.Decorator) { | ||
return ((decorator?.expression as TSESTree.CallExpression)?.callee as TSESTree.Identifier)?.name; | ||
} | ||
|
||
export function getPropertyDeclarations(node:TSESTree.ClassDeclaration) { | ||
const classBody = node.body.body; | ||
const properties = classBody.map((method) => { | ||
return ((method as (TSESTree.PropertyDefinition | TSESTree.MethodDefinition)).key as TSESTree.Identifier).name; | ||
}); | ||
return properties; | ||
} |
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,45 @@ | ||
import { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
|
||
import { | ||
getSubGraphs, | ||
getDependenciesFromSubgraphs, | ||
mapFunctions, | ||
checkDependencies, | ||
getDecoratorName, | ||
getPropertyDeclarations, | ||
} from './ASTFunctions'; | ||
|
||
export function create(context: any) { | ||
const imports:TSESTree.ImportDeclaration[] = []; | ||
const dependencies:string[] = []; | ||
|
||
return { | ||
ImportDeclaration(node:TSESTree.ImportDeclaration) { | ||
imports.push(node); | ||
}, | ||
ClassDeclaration(node:TSESTree.ClassDeclaration) { | ||
const { decorators } = node; | ||
if (decorators) { | ||
const decoratorNames = decorators.map((decorator) => getDecoratorName(decorator)); | ||
if (decoratorNames.includes('Graph')) { | ||
const subGraphs = getSubGraphs(decorators); | ||
if (subGraphs.length > 0) { | ||
dependencies.push(...getDependenciesFromSubgraphs(imports, subGraphs, context)); | ||
} | ||
dependencies.push(...mapFunctions(node)); | ||
dependencies.push(...getPropertyDeclarations(node)); | ||
const check = checkDependencies(node, dependencies); | ||
if (!check?.value) { | ||
context.report({ | ||
node, | ||
messageId: 'undefinedDependency', | ||
data: { | ||
dependencyName: check.param, | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |
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,24 @@ | ||
export const invalidGraph = `import { Graph, ObjectGraph, Provides } from 'src'; | ||
@Graph() | ||
export default class SimpleGraph extends ObjectGraph { | ||
@Provides() | ||
instanceId(id:string): string { | ||
return id; | ||
} | ||
}`; | ||
|
||
export const invalidGraphWithSubgraph = `import { | ||
Graph, | ||
ObjectGraph, | ||
Provides, | ||
} from 'src'; | ||
import Subgraph from './subgraph'; | ||
@Graph({ subgraphs: [Subgraph] }) | ||
export default class SimpleGraphWithSubgraph extends ObjectGraph { | ||
@Provides() | ||
someClass(wrongDep:string): string { | ||
return wrongDep; | ||
} | ||
}`; |
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,24 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { RuleModule } from '@typescript-eslint/utils/ts-eslint'; | ||
import { create } from './createFunction'; | ||
|
||
const createRule = ESLintUtils.RuleCreator( | ||
(name) => `https://example.com/rule/${name}`, | ||
); | ||
|
||
export const undefinedDependencies: RuleModule<'undefinedDependency'> = createRule({ | ||
create, | ||
name: 'undefined-dependency', | ||
meta: { | ||
docs: { | ||
description: 'The dependency must be defined', | ||
recommended: 'strict', | ||
}, | ||
messages: { | ||
undefinedDependency: 'Dependency {{ dependencyName }} is undefined', | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
defaultOptions: [] as [], | ||
}); |
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,15 @@ | ||
import { uniqueId } from 'lodash'; | ||
import { Graph, ObjectGraph, Provides } from 'src'; | ||
|
||
@Graph() | ||
export default class Subgraph extends ObjectGraph { | ||
@Provides() | ||
unusedDependency(): string { | ||
throw Error('This dependency should not have been resolved since it is not required by anyone.'); | ||
} | ||
|
||
@Provides() | ||
instanceId(): string { | ||
return uniqueId('graph'); | ||
} | ||
} |
Oops, something went wrong.