Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/eslint-plugin-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
"name": "eslint-plugin-studio",
"private": true,
"version": "1.0.0",
"type": "module",
"description": "ESLint rules for Studio",
"main": "src/index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"eslint": "^9.0.0"
},
"devDependencies": {
"jest": "^30.0.0"
}
}
6 changes: 4 additions & 2 deletions packages/eslint-plugin-studio/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = {
import requireLockBeforeSave from './rules/require-lock-before-save.js';

export default {
rules: {
'require-lock-before-save': require( './rules/require-lock-before-save' ),
'require-lock-before-save': requireLockBeforeSave,
},
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
/** @type {import('eslint').Rule.RuleModule} */
export default {
meta: {
type: 'problem',
docs: {
Expand Down Expand Up @@ -73,19 +74,19 @@ module.exports = {
current.hasTryFinally = true;
}
},
FunctionDeclaration( node ) {
FunctionDeclaration() {
enterFunction();
},
'FunctionDeclaration:exit'( node ) {
exitFunction( node );
},
ArrowFunctionExpression( node ) {
ArrowFunctionExpression() {
enterFunction();
},
'ArrowFunctionExpression:exit'( node ) {
exitFunction( node );
},
FunctionExpression( node ) {
FunctionExpression() {
enterFunction();
},
'FunctionExpression:exit'( node ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
const { RuleTester } = require( 'eslint' );
const rule = require( '../src/rules/require-lock-before-save' );
import { RuleTester } from 'eslint';
import { describe, it } from 'vitest';
import rule from '../src/rules/require-lock-before-save';

const ruleTester = new RuleTester( {
parserOptions: {
languageOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
} );

ruleTester.run( 'require-lock-before-save', rule, {
valid: [
// Functions not calling save functions (allowed)
{
code: `
describe( 'require-lock-before-save', () => {
it( 'should enforce locking when calling saveUserData or saveAppdata', () => {
ruleTester.run( 'require-lock-before-save', rule, {
valid: [
// Functions not calling save functions (allowed)
{
code: `
async function updateUserData() {
await updateAppdata({ locale: 'en' });
}
`,
},
// saveUserData with lock (allowed)
{
code: `
},
// saveUserData with lock (allowed)
{
code: `
async function updateUserData() {
await lockAppdata();
try {
Expand All @@ -32,10 +35,10 @@ ruleTester.run( 'require-lock-before-save', rule, {
}
}
`,
},
// saveAppdata with lock (allowed)
{
code: `
},
// saveAppdata with lock (allowed)
{
code: `
const updateUserData = async () => {
await lockAppdata();
try {
Expand All @@ -47,34 +50,34 @@ ruleTester.run( 'require-lock-before-save', rule, {
}
};
`,
},
],
invalid: [
// saveUserData without lock (not allowed)
{
code: `
},
],
invalid: [
// saveUserData without lock (not allowed)
{
code: `
const updateUserData = async () => {
const data = await loadUserData();
data.snapshots.splice(0, 1);
await saveUserData(data);
};
`,
errors: [ { messageId: 'missingLock' } ],
},
// saveAppdata without lock (not allowed)
{
code: `
errors: [ { messageId: 'missingLock' } ],
},
// saveAppdata without lock (not allowed)
{
code: `
async function updateUserData() {
const data = await loadUserData();
data.sites[0].name = 'New Name';
await saveAppdata(data);
}
`,
errors: [ { messageId: 'missingLock' } ],
},
// Lock without try/finally block (not allowed)
{
code: `
errors: [ { messageId: 'missingLock' } ],
},
// Lock without try/finally block (not allowed)
{
code: `
async function updateUserData() {
await lockAppdata();
const data = await loadUserData();
Expand All @@ -83,11 +86,11 @@ ruleTester.run( 'require-lock-before-save', rule, {
await unlockAppdata();
}
`,
errors: [ { messageId: 'missingUnlock' } ],
},
// Lock without unlock (not allowed)
{
code: `
errors: [ { messageId: 'missingUnlock' } ],
},
// Lock without unlock (not allowed)
{
code: `
async function updateUserData() {
await lockAppdata();
try {
Expand All @@ -99,7 +102,9 @@ ruleTester.run( 'require-lock-before-save', rule, {
}
}
`,
errors: [ { messageId: 'missingUnlock' } ],
},
],
errors: [ { messageId: 'missingUnlock' } ],
},
],
} );
} );
} );
1 change: 1 addition & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig( {
'src/**/*.{test,spec}.{ts,tsx}',
'cli/**/*.{test,spec}.{ts,tsx}',
'common/**/*.{test,spec}.{ts,tsx}',
'packages/**/*.{test,spec}.{ts,tsx,js}',
],
exclude: [
'**/node_modules/**',
Expand Down