File: enforce-define.js

package info (click to toggle)
node-eslint-plugin-requirejs 4.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 916 kB
  • sloc: javascript: 4,677; perl: 48; makefile: 31; sh: 6
file content (157 lines) | stat: -rw-r--r-- 3,968 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * @file    Tests for `enforce-define` rule
 * @author  Casey Visco <cvisco@gmail.com>
 */

"use strict";

const testRule = require("../../rule-tester");
const fixtures = require("../../fixtures");
const rule = require("../../../lib/rules/enforce-define");

// -----------------------------------------------------------------------------
// Fixtures
// -----------------------------------------------------------------------------

const UNWRAPPED_FILE_NO_EXPRESSIONSTATEMENT = `
    var foo = 'foo';

    function bar() {
        return foo;
    }
`;

const UNWRAPPED_FILE = `
    var foo = 'foo';

    function bar() {
        return foo;
    }

    window.bar = bar;
`;

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const ERROR = {
    message: "File must be wrapped in a `define` call",
    type: "Program"
};

testRule("enforce-define", rule, {

    valid: [

        // Any sort of define should work
        fixtures.MULTIPLE_DEFINE,
        fixtures.OBJECT_DEFINE,
        fixtures.FUNCTION_DEFINE,
        fixtures.AMD_DEFINE,
        fixtures.AMD_EMPTY_DEFINE,
        fixtures.CJS_WITH_RETURN,
        fixtures.CJS_WITH_EXPORTS,
        fixtures.CJS_WITH_MODULE_EXPORTS,
        fixtures.CJS_WITH_FUNC_EXPR,
        fixtures.CJS_WITH_INVALID_EXPORTS,
        fixtures.NAMED_AMD_DEFINE,
        fixtures.NAMED_AMD_EMPTY_DEFINE,
        fixtures.NAMED_FUNCTION_DEFINE,
        fixtures.NAMED_OBJECT_DEFINE,
        fixtures.NAMED_CJS_DEFINE,
        fixtures.AMD_DEFINE_WITH_JS_EXT,
        fixtures.CJS_WITH_JS_EXT,
        fixtures.NAMED_AMD_DEFINE_WITH_JS_EXT,
        fixtures.NAMED_CJS_DEFINE_WITH_JS_EXT,

        // All of the invalid cases should work if we ignore the file
        {
            code: UNWRAPPED_FILE,
            filename: "main.js",
            options: ["main.js"]
        },
        {
            code: fixtures.NON_WRAPPED_EXPORTS,
            filename: "main.js",
            options: ["main.js"]
        },
        {
            code: fixtures.AMD_REQUIRE,
            filename: "main.js",
            options: ["main.js"]
        },
        {
            code: fixtures.AMD_REQUIREJS,
            filename: "main.js",
            options: ["main.js"]
        },

        // Ignore should work with full path
        {
            code: UNWRAPPED_FILE,
            filename: "path/to/main.js",
            options: [["main.js"]]
        },

        // Ignore should support multiple filenames
        {
            code: UNWRAPPED_FILE,
            filename: "main.js",
            options: [["main.js", "index.js"]]
        },
        {
            code: UNWRAPPED_FILE,
            filename: "index.js",
            options: [["main.js", "index.js"]]
        }
    ],

    invalid: [
        {
            code: UNWRAPPED_FILE,
            errors: [ERROR]
        },
        {
            code: UNWRAPPED_FILE_NO_EXPRESSIONSTATEMENT,
            errors: [ERROR]
        },
        {
            code: fixtures.NON_WRAPPED_EXPORTS,
            errors: [ERROR]
        },
        {
            code: fixtures.AMD_REQUIRE,
            errors: [ERROR]
        },
        {
            code: fixtures.AMD_REQUIREJS,
            errors: [ERROR]
        },
        {
            code: UNWRAPPED_FILE,
            filename: "foo.js",
            options: ["main.js"],
            errors: [ERROR]
        },
        {
            code: UNWRAPPED_FILE,
            filename: "foo.js",
            options: [["main.js", "index.js"]],
            errors: [ERROR]
        },
        {
            code: UNWRAPPED_FILE,
            filename: "path/to/foo.js",
            options: ["main.js"],
            errors: [ERROR]
        },
        {
            code: UNWRAPPED_FILE,
            filename: "path/to/main.js/foo.js",
            options: ["main.js"],
            errors: [ERROR]
        }
    ]

});