Skip to content

Commit

Permalink
chore: add lint rule against implicit globals (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Apr 16, 2022
1 parent ef826a4 commit 7bca74c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ module.exports = {
// ES2022 will be released in June 2022
'prefer-object-has-own': 0,
},
overrides: [
{
files: ['src/**'],
rules: {
'explicit-globals': 'error',
},
},
],
}
38 changes: 38 additions & 0 deletions eslint/explicit-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
meta: {
type: 'suggestion',
messages: {
implicitGlobal: 'implicit global "{{name}}"',
},
},

create(context) {
return {
Program() {
const scope = context.getScope()

// `scope` is `GlobalScope` and `scope.variables` are the global variables
scope.variables.forEach(variable => {
// ignore `undefined`
if (variable.name === 'undefined') {
return
}
variable.references.forEach(ref => {
// Ignore types and global standard variables like `Object`
if (ref.resolved.constructor.name === 'ImplicitLibVariable') {
return
}

context.report({
node: ref.identifier,
messageId: 'implicitGlobal',
data: {
name: ref.identifier.name,
},
})
})
})
},
}
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"types": "./dist/types/index.d.ts",
"scripts": {
"build": "node build.js",
"lint": "kcd-scripts lint",
"lint": "kcd-scripts lint --rulesdir=eslint",
"setup": "npm install && npm run validate -s",
"test": "kcd-scripts test",
"test:debug": "kcd-scripts --inspect-brk test --runInBand",
Expand Down

0 comments on commit 7bca74c

Please sign in to comment.