123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * @fileoverview disallow `window/document` in `created/beforeCreate`
- * @author Xin Du <clark.duxin@gmail.com>
- */
- 'use strict'
- // ------------------------------------------------------------------------------
- // Requirements
- // ------------------------------------------------------------------------------
- const rule = require('../no-globals-in-created')
- const RuleTester = require('eslint').RuleTester
- const parserOptions = {
- ecmaVersion: 2018,
- sourceType: 'module'
- }
- // ------------------------------------------------------------------------------
- // Tests
- // ------------------------------------------------------------------------------
- const ruleTester = new RuleTester()
- ruleTester.run('no-globals-in-created', rule, {
- valid: [
- {
- filename: 'test.vue',
- code: `
- export default {
- created() {
- const path = this.$route.path
- },
- beforeCreate() {
- const path = this.$route.params.foo
- }
- }
- `,
- parserOptions
- }
- ],
- invalid: [
- {
- filename: 'test.vue',
- code: `
- export default {
- created() {
- const path = window.location.pathname
- },
- beforeCreate() {
- const foo = document.foo
- }
- }
- `,
- errors: [{
- message: 'Unexpected window in created.',
- type: 'MemberExpression'
- }, {
- message: 'Unexpected document in beforeCreate.',
- type: 'MemberExpression'
- }],
- parserOptions
- },
- {
- filename: 'test.vue',
- code: `
- export default {
- created() {
- document.foo = 'bar'
- },
- beforeCreate() {
- window.foo = 'bar'
- }
- }
- `,
- errors: [{
- message: 'Unexpected document in created.',
- type: 'MemberExpression'
- }, {
- message: 'Unexpected window in beforeCreate.',
- type: 'MemberExpression'
- }],
- parserOptions
- },
- {
- filename: 'test.vue',
- code: `
- export default {
- created() {
- return window.foo
- },
- beforeCreate() {
- return document.foo
- }
- }
- `,
- errors: [{
- message: 'Unexpected window in created.',
- type: 'MemberExpression'
- }, {
- message: 'Unexpected document in beforeCreate.',
- type: 'MemberExpression'
- }],
- parserOptions
- }
- ]
- })
|