Skip to content

Commit

Permalink
Merge branch 'toTs' into toTypescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-zhang-at-salesforce committed May 7, 2024
2 parents c6516ad + 6663701 commit f332d96
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 300 deletions.
11 changes: 6 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
module.exports = {
displayName: 'Unit Tests',
setupFilesAfterEnv: ['jest-extended', 'jest-chain'],
preset: "ts-jest",
testMatch: [
'<rootDir>/test/plugin.js',
'<rootDir>/test/lib/rules/**/*.js',
'!**/test/lib/rules/shared.js'
'<rootDir>/test/plugin.ts',
'<rootDir>/test/lib/rules/**/*.ts',
'!**/test/lib/rules/shared.ts'
],
moduleFileExtensions: ['js', 'json'],
moduleFileExtensions: ['ts', 'js', 'json'],
testResultsProcessor: 'jest-sonar-reporter',
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/lib/'],
moduleDirectories: ['node_modules'],
collectCoverage: true,
collectCoverageFrom: ['lib/**/*.js'],
collectCoverageFrom: ['lib/**/*.ts'],
coverageDirectory: 'reports/coverage',
reporters: [
'default',
Expand Down
60 changes: 0 additions & 60 deletions lib/rules/enforce-foo-bar.js

This file was deleted.

50 changes: 50 additions & 0 deletions lib/rules/enforce-foo-bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { AST_NODE_TYPES, TSESLint } from '@typescript-eslint/utils';

type MessageIds = 'messageIdForFooMustBeBar';

const ruleEnforceFoorBar: TSESLint.RuleModule<MessageIds> = {
defaultOptions: [],
meta: {
type: 'problem',
messages: {
messageIdForFooMustBeBar: 'Value other than "bar" assigned to `const foo`. Unexpected value: {{ notBar }}.',
},
fixable: 'code',
schema: []
},
create: (context) => ({
VariableDeclarator: (node) => {
// Performs action in the function on every variable declarator
if (node.parent.type === AST_NODE_TYPES.VariableDeclaration) {
// Check if variable name is `foo`
if (node.id.type === AST_NODE_TYPES.Identifier && node.id.name === 'foo') {
// Check if value of variable is "bar"
if (
node.init?.type === AST_NODE_TYPES.Literal &&
node.init?.value !== 'bar'
) {
/*
* Report error to ESLint. Error message uses
* a message placeholder to include the incorrect value
* in the error message.
* Also includes a `fix(fixer)` function that replaces
* any values assigned to `const foo` with "bar".
*/
context.report({
node,
messageId: 'messageIdForFooMustBeBar',
data: {
notBar: node.init.value
},
fix(fixer) {
return fixer.replaceText(node.init!!, '"bar"');
}
});
}
}
}
}
})
}

export default ruleEnforceFoorBar;
Loading

0 comments on commit f332d96

Please sign in to comment.