-
-
Notifications
You must be signed in to change notification settings - Fork 907
build: add ESLint rule to enforce that last require
statement in JS examples is a relative path
#5379
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
Merged
Merged
build: add ESLint rule to enforce that last require
statement in JS examples is a relative path
#5379
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
126 changes: 126 additions & 0 deletions
126
lib/node_modules/@stdlib/_tools/eslint/rules/require-last-path-relative/README.md
This file contains hidden or 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,126 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# require-last-path-relative | ||
|
||
> [ESLint rule][eslint-rules] enforcing that the last `require` statement is a relative path. | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var rule = require( '@stdlib/_tools/eslint/rules/require-last-path-relative' ); | ||
``` | ||
|
||
#### rule | ||
|
||
[ESLint rule][eslint-rules] enforcing that the last `require` statement is a relative path. | ||
|
||
**Bad**: | ||
|
||
```javascript | ||
var zip = require( '@stdlib/utils/zip' ); | ||
var unzip = require( '@stdlib/utils/unzip' ); | ||
``` | ||
|
||
**Good**: | ||
|
||
<!-- eslint stdlib/require-file-extensions: "off" --> | ||
|
||
```javascript | ||
var zip = require( '@stdlib/utils/zip' ); | ||
var unzip = require( './../lib' ); | ||
``` | ||
|
||
The rule is mainly for `examples` files to help make explicit that the API being demonstrated belongs to the respective package. | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var Linter = require( 'eslint' ).Linter; | ||
var rule = require( '@stdlib/_tools/eslint/rules/require-last-path-relative' ); | ||
|
||
var linter = new Linter(); | ||
var result; | ||
|
||
var code = [ | ||
'var beep = require( \'@stdlib/boop/beep\' );', | ||
'var foo = require( \'@stdlib/bar/foo\' );' | ||
].join( '\n' ); | ||
|
||
linter.defineRule( 'require-last-path-relative', rule ); | ||
|
||
result = linter.verify( code, { | ||
'rules': { | ||
'require-last-path-relative-relative': 'error' | ||
} | ||
}); | ||
/* returns | ||
[ | ||
{ | ||
'ruleId': 'require-last-path-relative', | ||
'severity': 2, | ||
'message': 'Last `require` statement must be a relative path', | ||
'line': 2, | ||
'column': 11, | ||
'nodeType': 'CallExpression', | ||
'endLine': 2, | ||
'endColumn': 39 | ||
} | ||
] | ||
*/ | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/_tools/eslint/rules/require-last-path-relative/examples/index.js
This file contains hidden or 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,52 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var Linter = require( 'eslint' ).Linter; | ||
var rule = require( './../lib' ); | ||
|
||
var linter = new Linter(); | ||
|
||
var code = [ | ||
'var beep = require( \'@stdlib/boop/beep\' );', | ||
'var foo = require( \'@stdlib/bar/foo\' );' | ||
].join( '\n' ); | ||
|
||
linter.defineRule( 'require-last-path-relative', rule ); | ||
|
||
var result = linter.verify( code, { | ||
'rules': { | ||
'require-last-path-relative': 'error' | ||
} | ||
}); | ||
console.log( result ); | ||
/* => | ||
[ | ||
{ | ||
'ruleId': 'require-last-path-relative', | ||
'severity': 2, | ||
'message': 'Last `require` statement must be a relative path', | ||
'line': 2, | ||
'column': 11, | ||
'nodeType': 'CallExpression', | ||
'endLine': 2, | ||
'endColumn': 39 | ||
} | ||
] | ||
*/ |
39 changes: 39 additions & 0 deletions
39
lib/node_modules/@stdlib/_tools/eslint/rules/require-last-path-relative/lib/index.js
This file contains hidden or 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,39 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* ESLint rule to enforce that the last `require` statement is a relative path. | ||
* | ||
* @module @stdlib/_tools/eslint/rules/require-last-path-relative | ||
* | ||
* @example | ||
* var rule = require( '@stdlib/_tools/eslint/rules/require-last-path-relative' ); | ||
* | ||
* console.log( rule ); | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var main = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = main; |
124 changes: 124 additions & 0 deletions
124
lib/node_modules/@stdlib/_tools/eslint/rules/require-last-path-relative/lib/main.js
This file contains hidden or 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,124 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var startsWith = require( '@stdlib/string/starts-with' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Checks if a require path is relative. | ||
* | ||
* @private | ||
* @param {string} path - require path | ||
* @returns {boolean} boolean indicating if path is relative | ||
*/ | ||
function isRelativePath( path ) { | ||
return ( | ||
startsWith( path, './' ) || | ||
startsWith( path, '/' ) || | ||
Planeshifter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
startsWith( path, '../' ) | ||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* ESLint rule to enforce that the last `require` statement is a relative path. | ||
* | ||
* @param {Object} context - ESLint context | ||
* @returns {Object} validators | ||
*/ | ||
function main( context ) { | ||
var requires = []; | ||
|
||
/** | ||
* Validates `require` statements. | ||
* | ||
* @private | ||
* @param {ASTNode} node - node to examine | ||
*/ | ||
function validate( node ) { | ||
var requirePath; | ||
if ( | ||
node.callee.name === 'require' && | ||
node.arguments[ 0 ] && | ||
node.arguments[ 0 ].type === 'Literal' | ||
) { | ||
requirePath = node.arguments[ 0 ].value; | ||
requires.push({ | ||
'node': node, | ||
'path': requirePath, | ||
'isRelative': isRelativePath( requirePath ) | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Reports the error message. | ||
* | ||
* @private | ||
* @param {Object} lastRequire - last require statement | ||
*/ | ||
function report( lastRequire ) { | ||
context.report({ | ||
'node': lastRequire.node, | ||
'message': 'Last `require` statement in example files must be a relative path' | ||
}); | ||
} | ||
|
||
/** | ||
* Callback invoked upon program exit. | ||
* | ||
* @private | ||
*/ | ||
function finish() { | ||
var lastRequire; | ||
if ( requires.length > 0 ) { | ||
lastRequire = requires[ requires.length - 1 ]; | ||
if ( !lastRequire.isRelative ) { | ||
report( lastRequire ); | ||
} | ||
requires.length = 0; | ||
Planeshifter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return { | ||
'CallExpression': validate, | ||
'Program:exit': finish | ||
}; | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = { | ||
'meta': { | ||
'type': 'suggestion', | ||
'docs': { | ||
'description': 'enforce that the last `require` statement is a relative path' | ||
}, | ||
'schema': [] | ||
}, | ||
'create': main | ||
}; |
Oops, something went wrong.
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.
FTR, we cannot use
assert/is-relative-path
here, as this would incorrectly identify module identifiers as relative paths.