|
// lib/rules/no-native-navigation.js |
|
module.exports = { |
|
meta: { |
|
type: 'problem', |
|
docs: { |
|
description: 'Forbid native navigation methods in Vue components', |
|
category: 'Best Practices', |
|
recommended: true, |
|
}, |
|
fixable: null, |
|
schema: [], // no options |
|
messages: { |
|
avoidNativeNavigation: 'Avoid using native navigation methods. Use Vue Router methods instead.', |
|
}, |
|
}, |
|
create(context) { |
|
// Check if the current file is a Vue component |
|
const isVueComponent = context.getFilename().endsWith('.vue'); |
|
|
|
// If not a Vue component, return an empty object to skip checks |
|
if (!isVueComponent) { |
|
return {}; |
|
} |
|
return { |
|
MemberExpression(node) { |
|
// Check for location.href |
|
if ( |
|
node.object.type === 'Identifier' && |
|
node.object.name === 'location' && |
|
node.property.type === 'Identifier' && |
|
node.property.name === 'href' |
|
) { |
|
context.report({ |
|
node, |
|
messageId: 'avoidNativeNavigation', |
|
}); |
|
} |
|
|
|
// Check for history.go, history.push, etc. |
|
if ( |
|
node.object.type === 'Identifier' && |
|
node.object.name === 'history' && |
|
['go', 'push', 'replace', 'back', 'forward'].includes(node.property.name) |
|
) { |
|
context.report({ |
|
node, |
|
messageId: 'avoidNativeNavigation', |
|
}); |
|
} |
|
}, |
|
CallExpression(node) { |
|
// Additional check for window.location methods |
|
if ( |
|
node.callee.type === 'MemberExpression' && |
|
node.callee.object.type === 'Identifier' && |
|
node.callee.object.name === 'location' && |
|
['assign', 'replace'].includes(node.callee.property.name) |
|
) { |
|
context.report({ |
|
node, |
|
messageId: 'avoidNativeNavigation', |
|
}); |
|
} |
|
} |
|
}; |
|
} |
|
}; |