-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added @internal Js comments to internal API functions #2484
Open
nveenjain
wants to merge
1
commit into
graphql:main
Choose a base branch
from
nveenjain:enhancement/JSDocInternalRule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// @flow strict | ||
|
||
'use strict'; | ||
|
||
// @noflow | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const { HashMap } = require('./helper'); | ||
|
||
// Create a new Hashmap to store file names and declarations | ||
const mp = new HashMap(); | ||
|
||
// All the rules that are required by plugin | ||
const ExportNamedDeclaration = { | ||
enter(babelPath, state) { | ||
if (babelPath.node.declaration) { | ||
return handleDeclaration(babelPath, state); | ||
} | ||
|
||
if (babelPath.node.specifiers) { | ||
return handleNodeSpecifiers( | ||
babelPath.node.specifiers, | ||
state, | ||
babelPath.node.source ? babelPath.node.source.value : undefined, | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
const ExportAllDeclaration = { | ||
enter(babelPath, state) { | ||
mp.add( | ||
'*', | ||
state, | ||
babelPath.node.source ? babelPath.node.source.value : undefined, | ||
); | ||
}, | ||
}; | ||
|
||
module.exports = function() { | ||
return { | ||
visitor: { | ||
ExportNamedDeclaration, | ||
ExportAllDeclaration, | ||
ExportDefaultDeclaration: ExportNamedDeclaration, | ||
Program: { | ||
exit() { | ||
return writeToJSON(); | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
// Helper functions for the rules | ||
function handleDeclaration(babelPath, state) { | ||
switch (babelPath.node.declaration.type) { | ||
case 'VariableDeclaration': | ||
return handleVariableDeclarations( | ||
babelPath.node.declaration, | ||
state, | ||
babelPath.node.source ? babelPath.node.source.value : undefined, | ||
); | ||
case 'FunctionDeclaration': | ||
case 'ClassDeclaration': | ||
return handleFunctionDeclarations( | ||
babelPath.node.declaration, | ||
state, | ||
babelPath.node.source ? babelPath.node.source.value : undefined, | ||
); | ||
} | ||
} | ||
|
||
function handleNodeSpecifiers(specifiers, state, source) { | ||
return specifiers.forEach(specifier => { | ||
switch (specifier.type) { | ||
case 'ExportSpecifier': | ||
mp.add(specifier.local.name, state, source); | ||
break; | ||
case 'ExportNamespaceSpecifier': | ||
mp.add('*', state, source); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
function handleVariableDeclarations(variableDeclaration, state, source) { | ||
variableDeclaration.declarations.forEach(declaration => | ||
mp.add(declaration.id.name, state, source), | ||
); | ||
} | ||
|
||
function handleFunctionDeclarations(declaration, state, source) { | ||
return mp.add(declaration.id.name, state, source); | ||
} | ||
|
||
// To write final result to JSON file | ||
function writeToJSON() { | ||
if (!fs.existsSync(path.join(__dirname, '/map.json'))) { | ||
fs.writeFileSync(path.join(__dirname, '/map.json'), JSON.stringify({})); | ||
} | ||
const exportedValues = require(path.join(__dirname, '/map.json')); | ||
for (const key of mp.keys()) { | ||
exportedValues[key] = exportedValues[key] || []; | ||
|
||
exportedValues[key] = exportedValues[key].concat(Array.from(mp.get(key))); | ||
|
||
exportedValues[key] = Array.from(new Set(exportedValues[key])); | ||
} | ||
|
||
fs.writeFileSync( | ||
path.join(__dirname, '/map.json'), | ||
JSON.stringify(exportedValues), | ||
); | ||
} |
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,33 @@ | ||
// @flow strict | ||
|
||
'use strict'; | ||
const path = require('path'); | ||
|
||
class HashMap { | ||
constructor() { | ||
this._map = new Map(); | ||
} | ||
|
||
add(value, state, source) { | ||
let filepath = path.resolve(state.file.opts.filename); | ||
if (source) { | ||
const pathArray = state.file.opts.filename.split('/'); | ||
const directoryPath = pathArray.slice(0, pathArray.length - 1).join('/'); | ||
filepath = require.resolve(path.resolve(directoryPath, source)); | ||
} | ||
if (!this._map.has(filepath)) { | ||
this._map.set(filepath, new Set()); | ||
} | ||
this._map.get(filepath).add(value); | ||
} | ||
|
||
get(key) { | ||
return this._map.get(key); | ||
} | ||
|
||
keys() { | ||
return this._map.keys(); | ||
} | ||
} | ||
|
||
module.exports = { HashMap }; |
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,49 @@ | ||
// @flow strict | ||
|
||
// @noflow | ||
|
||
'use strict'; | ||
const path = require('path'); | ||
|
||
const listOfExports = require(path.join( | ||
process.cwd(), | ||
'/resources/babel-plugins/', | ||
'map.json', | ||
)); | ||
module.exports = { | ||
create(context) { | ||
function isExportedLocallyOnly(name) { | ||
if (!listOfExports[context.getFilename()]) { | ||
return true; | ||
} | ||
return !listOfExports[context.getFilename()].find( | ||
value => value === name || value === '*', | ||
); | ||
} | ||
|
||
const source = context.getSourceCode(); | ||
/** | ||
* | ||
*/ | ||
return { | ||
'ExportNamedDeclaration > :matches(FunctionDeclaration,ClassDeclaration)'( | ||
node, | ||
) { | ||
if (isExportedLocallyOnly(node.id.name)) { | ||
if (!source.getJSDocComment(node)) { | ||
return context.report({ | ||
node, | ||
message: 'Please enter JSDoC internal functions using @internal', | ||
}); | ||
} | ||
if (!source.getJSDocComment(node).value.includes('@internal')) { | ||
context.report({ | ||
node, | ||
message: 'Please annotate internal functions using @internal', | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,26 @@ | ||
// @noflow | ||
|
||
'use strict'; | ||
|
||
const babel = require('@babel/core'); | ||
const flowTypesPlugin = require('@babel/plugin-transform-flow-strip-types'); | ||
|
||
const extractExportPlugin = require('./babel-plugins/extract-exports'); | ||
|
||
const directoriesToScan = [ | ||
'/src', | ||
'/src/error', | ||
'/src/type', | ||
'/src/language', | ||
'/src/validation', | ||
'/src/utilities', | ||
'/src/execution', | ||
'/src/subscription', | ||
]; | ||
|
||
directoriesToScan.forEach(path => | ||
babel.transformFileSync(process.cwd() + path + '/index.js', { | ||
babelrc: false, | ||
plugins: [flowTypesPlugin, extractExportPlugin], | ||
}), | ||
); |
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
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.
@IvanGoncharov, I've ignored all the files under
__tests__
by ignoring them in the when we invoke eslint itself ( using--ignore-pattern
). But the command itself turned out to be larger in length... Should i break the lint commands in two parts or does this work?