describe('no use strict', function() {
  eslintTester.addRuleTest('dist/no-use-strict', {
    valid: [
      '',
      '           ',
      'var validVariable = true;',
      '/* comments */',
      'var foo = "use strict";',
      'var foo = function() { return "use strict"; };'
    ],

    invalid: [
      errorScenario('"use strict"'),
      errorScenario('"use strict"; var invalidVariable = true'),
      errorScenario('\'use strict\'; var invalidVariable = true'),
      errorScenario('/* Leading comments */ "use strict"; var invalidVariable = true'),
      errorScenario('(function () { "use strict"; console.log("a"); })()'),
      errorScenario('var foo = function(x) { "use strict"; return x; };')
    ]
  });
});
eslintTester.addRuleTest("lib/rules/no-unexpected-multiline", {
    valid: [
        "(x || y).aFunction()",
        "[a, b, c].forEach(doSomething)",
        "var a = b;\n(x || y).doSomething()",
        "var a = b\n;(x || y).doSomething()",
        "var a = b\nvoid (x || y).doSomething()",
        "var a = b;\n[1, 2, 3].forEach(console.log)",
        "var a = b\nvoid [1, 2, 3].forEach(console.log)",
        "\"abc\\\n(123)\"",
        "var a = (\n(123)\n)"
    ],
    invalid: [
        {
            code: "var a = b\n(x || y).doSomething()",
            line: 2,
            column: 1,
            errors: [{ message: "Unexpected newline between function and ( of function call." }]
        },
        {
            code: "var a = (a || b)\n(x || y).doSomething()",
            line: 2,
            column: 1,
            errors: [{ message: "Unexpected newline between function and ( of function call." }]
        },
        {
            code: "var a = (a || b)\n(x).doSomething()",
            line: 2,
            column: 1,
            errors: [{ message: "Unexpected newline between function and ( of function call." }]
        },
        {
			code: "var a = b\n[a, b, c].forEach(doSomething)",
            line: 2,
            column: 1,
            errors: [{ message: "Unexpected newline between object and [ of property access." }]
		},
        {
			code: "var a = b\n    (x || y).doSomething()",
            line: 2,
            column: 5,
            errors: [{ message: "Unexpected newline between function and ( of function call." }]
		},
        {
			code: "var a = b\n  [a, b, c].forEach(doSomething)",
            line: 2,
            column: 3,
            errors: [{ message: "Unexpected newline between object and [ of property access." }]
		}
    ]
});
Example #3
0
eslintTester.addRuleTest("lib/rules/brace-style", {
    valid: [
        "if (tag === 1) glyph.id = pbf.readVarint();\nelse if (tag === 2) glyph.bitmap = pbf.readBytes();",
        "function foo () { \nreturn; \n}",
        "function a(b,\nc,\nd) { }",
        "!function foo () { \nreturn;\n }",
        "!function a(b,\nc,\nd) { }",
        "if (foo) { \n bar(); \n}",
        "if (a) { \nb();\n } else { \nc();\n }",
        "while (foo) { \n bar();\n }",
        "for (;;) { \n bar(); \n}",
        "with (foo) { \n bar(); \n}",
        "switch (foo) { \n case \"bar\": break;\n }",
        "try { \n bar();\n } catch (e) {\n baz(); \n }",
        "do { \n bar();\n } while (true)",
        "for (foo in bar) { \n baz(); \n }",
        "if (a &&\n b &&\n c) { \n }",
        "switch(0) {\n}",
        { code: "if (foo) {\n}\nelse {\n}", args: ["2", "stroustrup"] },
        { code: "try { \n bar();\n }\ncatch (e) {\n baz(); \n }", args: ["2", "stroustrup"] },
        // allowSingleLine: true
        { code: "function foo () { return; }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "function foo () { a(); b(); return; }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "function a(b,c,d) { }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "!function foo () { return; }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "!function a(b,c,d) { }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "if (foo) {  bar(); }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "if (a) { b(); } else { c(); }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "while (foo) {  bar(); }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "for (;;) {  bar(); }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "with (foo) {  bar(); }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "switch (foo) {  case \"bar\": break; }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "try {  bar(); } catch (e) { baz();  }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "do {  bar(); } while (true)", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "for (foo in bar) {  baz();  }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "if (a && b && c) {  }", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "switch(0) {}", args: ["2", "1tbs", { allowSingleLine: true }] },
        { code: "if (foo) {}\nelse {}", args: ["2", "stroustrup", { allowSingleLine: true }] },
        { code: "try {  bar(); }\ncatch (e) { baz();  }", args: ["2", "stroustrup", { allowSingleLine: true }] },
        { code: "var foo = () => { return; }", ecmaFeatures: { arrowFunctions: true }, args: ["2", "stroustrup", { allowSingleLine: true }] }
    ],
    invalid: [
        { code: "var foo = () => { return; }", ecmaFeatures: { arrowFunctions: true }, errors: [{ message: BODY_MESSAGE, type: "ReturnStatement"}] },
        { code: "function foo() { return; }", errors: [{ message: BODY_MESSAGE, type: "ReturnStatement"}] },
        { code: "function foo() \n { \n return; }", errors: [{ message: OPEN_MESSAGE, type: "FunctionDeclaration"}] },
        { code: "!function foo() \n { \n return; }", errors: [{ message: OPEN_MESSAGE, type: "FunctionExpression"}] },
        { code: "if (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "IfStatement"}] },
        { code: "if (a) { \nb();\n } else \n { c(); }", errors: [{ message: OPEN_MESSAGE, type: "IfStatement"}] },
        { code: "while (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "WhileStatement"}] },
        { code: "for (;;) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "ForStatement"}] },
        { code: "with (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "WithStatement"}] },
        { code: "switch (foo) \n { \n case \"bar\": break; }", errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "switch (foo) \n { }", errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "try \n { \n bar(); \n } catch (e) {}", errors: [{ message: OPEN_MESSAGE, type: "TryStatement"}] },
        { code: "try { \n bar(); \n } catch (e) \n {}", errors: [{ message: OPEN_MESSAGE, type: "CatchClause"}] },
        { code: "do \n { \n bar(); \n} while (true)", errors: [{ message: OPEN_MESSAGE, type: "DoWhileStatement"}] },
        { code: "for (foo in bar) \n { \n baz(); \n }", errors: [{ message: OPEN_MESSAGE, type: "ForInStatement"}] },
        { code: "try { \n bar(); \n }\ncatch (e) {\n}", errors: [{ message: CLOSE_MESSAGE, type: "CatchClause"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement"}] },
        { code: "if (a) { \nb();\n } \n else { \nc();\n }", errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement" }]},
        { code: "try { \n bar(); \n }\ncatch (e) {\n} finally {\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "BlockStatement"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "CatchClause"}] },
        { code: "if (a) { \nb();\n } else { \nc();\n }", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "BlockStatement" }]},
        { code: "if (foo) {\nbaz();\n} else if (bar) {\nbaz();\n}\nelse {\nqux();\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "IfStatement" }] },
        { code: "if (foo) {\npoop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "IfStatement" }] },
        // allowSingleLine: true
        { code: "function foo() { return; \n}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: BODY_MESSAGE, type: "ReturnStatement"}] },
        { code: "function foo() { a(); b(); return; \n}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: BODY_MESSAGE, type: "ExpressionStatement"}] },
        { code: "function foo() { \n return; }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "ReturnStatement"}] },
        { code: "function foo() {\na();\nb();\nreturn; }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "ReturnStatement"}] },
        { code: "!function foo() { \n return; }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "ReturnStatement"}] },
        { code: "if (foo) \n { bar(); }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "IfStatement"}] },
        { code: "if (a) { b();\n } else { c(); }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: BODY_MESSAGE, type: "ExpressionStatement"}] },
        { code: "if (a) { b(); }\nelse { c(); }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement" }] },
        { code: "while (foo) { \n bar(); }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_SINGLE, type: "ExpressionStatement"}] },
        { code: "for (;;) { bar(); \n }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: BODY_MESSAGE, type: "ExpressionStatement"}] },
        { code: "with (foo) { bar(); \n }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: BODY_MESSAGE, type: "ExpressionStatement"}] },
        { code: "switch (foo) \n { \n case \"bar\": break; }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "switch (foo) \n { }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "try {  bar(); }\ncatch (e) { baz();  }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE, type: "CatchClause" }] },
        { code: "try \n { \n bar(); \n } catch (e) {}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "TryStatement"}] },
        { code: "try { \n bar(); \n } catch (e) \n {}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "CatchClause"}] },
        { code: "do \n { \n bar(); \n} while (true)", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "DoWhileStatement"}] },
        { code: "for (foo in bar) \n { \n baz(); \n }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: OPEN_MESSAGE, type: "ForInStatement"}] },
        { code: "try { \n bar(); \n }\ncatch (e) {\n}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE, type: "CatchClause"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement"}] },
        { code: "if (a) { \nb();\n } \n else { \nc();\n }", args: ["2", "1tbs", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement" }]},
        { code: "try { \n bar(); \n }\ncatch (e) {\n} finally {\n}", args: ["2", "stroustrup", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "BlockStatement"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", args: ["2", "stroustrup", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "CatchClause"}] },
        { code: "if (a) { \nb();\n } else { \nc();\n }", args: ["2", "stroustrup", { allowSingleLine: true }], errors: [{ message: CLOSE_MESSAGE_STROUSTRUP, type: "BlockStatement" }]}
    ]
});
Example #4
0
/**
 * @fileoverview Tests for no-debugger rule.
 * @author Nicholas C. Zakas
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-debugger", {
    valid: [
        "var test = { debugger: 1 }; test.debugger;"
    ],
    invalid: [
        { code: "debugger", errors: [{ message: "Unexpected 'debugger' statement.", type: "DebuggerStatement"}] }
    ]
});
Example #5
0
eslintTester.addRuleTest("lib/rules/func-style", {
    valid: [
        {
            code: "function foo(){}\n function bar(){}",
            args: [1, "declaration"]
        },
        {
            code: "foo.bar = function(){};",
            args: [1, "declaration"]
        },
        {
            code: "(function() { /* code */ }());",
            args: [1, "declaration"]
        },
        {
            code: "var module = (function() { return {}; }());",
            args: [1, "declaration"]
        },
        {
            code: "var object = { foo: function(){} };",
            args: [1, "declaration"]
        },
        {
            code: "Array.prototype.foo = function(){};",
            args: [1, "declaration"]
        },
        {
            code: "foo.bar = function(){};",
            args: [1, "expression"]
        },
        {
            code: "var foo = function(){};\n var bar = function(){};",
            args: [1, "expression"]
        }
    ],

    invalid: [
        {
            code: "var foo = function(){};",
            args: [1, "declaration"],
            errors: [
                {
                    message: "Expected a function declaration.",
                    type: "VariableDeclarator"
                }
            ]
        },
        {
            code: "function foo(){}",
            args: [1, "expression"],
            errors: [
                {
                    message: "Expected a function expression.",
                    type: "FunctionDeclaration"
                }
            ]
        }
    ]
});
Example #6
0
var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/eol-last", {

    valid: [
        "var a = 123;\n"
    ],

    invalid: [
        {
            code: "var a = 123;",
            errors: [{ message: "Unexpected end of file - newline needed.", type: "Program" }]
        },
        {
            code: "var a = 123;\n\n",
            errors: [{ message: "Multiple empty lines at the end of file.", type: "Program" }]
        },
        {
            code: "var a = 123;\n   \n",
            errors: [{ message: "Multiple empty lines at the end of file.", type: "Program" }]
        }
    ]
});
Example #7
0
eslintTester.addRuleTest("lib/rules/consistent-return", {

    valid: [
        "function foo() { return; }",
        "function foo() { if (true) return; else return; }",
        "function foo() { if (true) return true; else return false; }",
        "f(function() { return; })",
        "f(function() { if (true) return; else return; })",
        "f(function() { if (true) return true; else return false; })",
        "function foo() { function bar() { return true; } return; }",
        "function foo() { function bar() { return; } return false; }"
    ],

    invalid: [
        {
            code: "function foo() { if (true) return true; else return; }",
            errors: [
                {
                    message: "Expected a return value.",
                    type: "ReturnStatement"
                }
            ]
        },
        {
            code: "function foo() { if (true) return; else return false; }",
            errors: [
                {
                    message: "Expected no return value.",
                    type: "ReturnStatement"
                }
            ]
        },
        {
            code: "f(function () { if (true) return true; else return; })",
            errors: [
                {
                    message: "Expected a return value.",
                    type: "ReturnStatement"
                }
            ]
        },
        {
            code: "f(function () { if (true) return; else return false; })",
            errors: [
                {
                    message: "Expected no return value.",
                    type: "ReturnStatement"
                }
            ]
        }
    ]
});
Example #8
0
var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-empty", {
    valid: [
        "if (foo) { bar() }",
        "while (foo) { bar() }",
        "for (;foo;) { bar() }",
        "try { foo() } catch (ex) { foo() }",
        "switch(foo) {case 'foo': break;}",
        "(function() { }())",
        "function foo() { }",
        "try { foo() } catch (ex) {}",
        "try { foo() } finally {}"
    ],
    invalid: [
        { code: "try {} catch (ex) {}", errors: [{ message: "Empty block statement.", type: "BlockStatement"}] },
        { code: "try { foo() } catch (ex) {} finally {}", errors: [{ message: "Empty block statement.", type: "BlockStatement"}] },
        { code: "if (foo) {}", errors: [{ message: "Empty block statement.", type: "BlockStatement"}] },
        { code: "while (foo) {}", errors: [{ message: "Empty block statement.", type: "BlockStatement"}] },
        { code: "for (;foo;) {}", errors: [{ message: "Empty block statement.", type: "BlockStatement"}] },
        { code: "switch(foo) {}", errors: [{ message: "Empty switch statement.", type: "SwitchStatement"}] }
    ]
});
Example #9
0
/**
 * @fileoverview Tests for no-octal-escape rule.
 * @author Ian Christian Myers
 */

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslintTester = require("eslint-tester");

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

eslintTester.addRuleTest("lib/rules/no-octal-escape", {
    valid: [
        "var foo = \"\\851\";",
        "var foo = \"foo \\\\251 bar\";"
    ],
    invalid: [
        { code: "var foo = \"foo \\251 bar\";", errors: [{ message: "Don't use octal: '\\2'. Use '\\u....' instead.", type: "Literal"}] },
        { code: "var foo = \"\\751\";", errors: [{ message: "Don't use octal: '\\7'. Use '\\u....' instead.", type: "Literal"}] },
        { code: "var foo = \"\\3s51\";", errors: [{ message: "Don't use octal: '\\3'. Use '\\u....' instead.", type: "Literal"}] },
        { code: "var foo = \"\\\\\\751\";", errors: [{ message: "Don't use octal: '\\7'. Use '\\u....' instead.", type: "Literal"}] }
    ]
});
var eslintTester = new ESLintTester(linter);

var invalidTests = ['1', 'function(){}', '/f/', 'new Date()', 'foo'].map(
	function(item, index) {
		return {
			code: 'Liferay.Language.get(' + item + ')',
			errors: [ { message: 'You should only pass a single string literal to Liferay.Language.get()' } ]
		}
	}
);

eslintTester.addRuleTest(
	path.resolve(__dirname, '../', '../', 'lib/lint_rules/' + path.basename(__filename)),
	{
		valid: [
			'Liferay.Language.get("foo")',
			'Liferay.Language.get(\'foo\')'
		],

		invalid: invalidTests.concat([
			{
				code: 'Liferay.Language.get()',
				errors: [ { message: 'Liferay.Language.get() only accepts a single string as an argument' } ]
			},
			{
				code: 'Liferay.Language.get("foo", name)',
				errors: [ { message: 'Liferay.Language.get() only accepts a single string as an argument' } ]
			}
		])
	}
);
Example #11
0
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-func-assign", {
    valid: [
        "function foo() { var foo = bar; }",
        "function foo(foo) { foo = bar; }",
        "function foo() { var foo; foo = bar; }",
        "var foo = function() {}; foo = bar;",
        "var foo = function() { foo = bar; };"
    ],
    invalid: [
        { code: "function foo() {}; foo = bar;", errors: [{ message: "'foo' is a function.", type: "AssignmentExpression"}] },
        { code: "function foo() { foo = bar; }", errors: [{ message: "'foo' is a function.", type: "AssignmentExpression"}] },
        { code: "foo = bar; function foo() { };", errors: [{ message: "'foo' is a function.", type: "AssignmentExpression"}] }
    ]
});
eslintTester.addRuleTest('lib/rules/prop-types', {

  valid: [
    {
      code: [
        'var Hello = React.createClass({',
        '  propTypes: {',
        '    name: React.PropTypes.string.isRequired',
        '  },',
        '  render: function() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  propTypes: {',
        '    name: React.PropTypes.object.isRequired',
        '  },',
        '  render: function() {',
        '    return <div>Hello {this.props.name.firstname}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  render: function() {',
        '    return <div>Hello World</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  render: function() {',
        '    return <div>Hello World {this.props.children}</div>;',
        '  }',
        '});'
      ].join('\n'),
      args: [1, {
        ignore: ['children']
      }],
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  render: function() {',
        '    var props = this.props;',
        '    return <div>Hello World</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  render: function() {',
        '    var propName = "foo";',
        '    return <div>Hello World {this.props[propName]}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  propTypes: externalPropTypes,',
        '  render: function() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello World</div>;',
        '  }',
        '}'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.firstname} {this.props.lastname}</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  firstname: React.PropTypes.string',
        '};',
        'Hello.propTypes.lastname = React.PropTypes.string;'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      }
    }, {
      code: [
        'var Hello = React.createClass({',
        '  propTypes: {',
        '    name: React.PropTypes.object.isRequired',
        '  },',
        '  render: function() {',
        '    var user = {',
        '      name: this.props.name',
        '    };',
        '    return <div>Hello {user.name}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      }
    }, {
      code: [
        'class Hello {',
        '  render() {',
        '    return \'Hello\' + this.props.name;',
        '  }',
        '}'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  static get propTypes() {',
        '    return {',
        '      name: React.PropTypes.string',
        '    };',
        '  }',
        '  render() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '}'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        destructuring: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    var { firstname, ...other } = this.props;',
        '    return <div>Hello {firstname}</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  firstname: React.PropTypes.string',
        '};'
      ].join('\n'),
      parser: 'babel-eslint',
      ecmaFeatures: {
        classes: true,
        destructuring: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  static propTypes = {',
        '    name: React.PropTypes.string',
        '  }',
        '  render() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '}'
      ].join('\n'),
      parser: 'babel-eslint',
      ecmaFeatures: {
        classes: true,
        destructuring: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.firstname}</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  \'firstname\': React.PropTypes.string',
        '};'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      }
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    if (this.props.hasOwnProperty(\'firstname\')) {',
        '      return <div>Hello {this.props.firstname}</div>;',
        '    }',
        '    return <div>Hello</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  \'firstname\': React.PropTypes.string',
        '};'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      }
    }
  ],

  invalid: [
    {
      code: [
        'var Hello = React.createClass({',
        '  render: function() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      },
      errors: [{
        message: '\'name\' is missing in props validation',
        line: 3,
        column: 34,
        type: 'Identifier'
      }]
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '}'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      },
      errors: [{
        message: '\'name\' is missing in props validation for Hello',
        line: 3,
        column: 34,
        type: 'Identifier'
      }]
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.firstname} {this.props.lastname}</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  firstname: React.PropTypes.string',
        '};'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      },
      errors: [{
        message: '\'lastname\' is missing in props validation for Hello'
      }]
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  name: React.PropTypes.string',
        '};',
        'class HelloBis extends React.Component {',
        '  render() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '}'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        jsx: true
      },
      errors: [{
        message: '\'name\' is missing in props validation for HelloBis'
      }]
    }, {
      code: [
        'var Hello = React.createClass({',
        '  propTypes: {',
        '    name: React.PropTypes.string.isRequired',
        '  },',
        '  render: function() {',
        '    return <div>Hello {this.props.name} and {this.props.propWithoutTypeDefinition}</div>;',
        '  }',
        '});',
        'var Hello2 = React.createClass({',
        '  render: function() {',
        '    return <div>Hello {this.props.name}</div>;',
        '  }',
        '});'
      ].join('\n'),
      ecmaFeatures: {
        jsx: true
      },
      errors: [{
        message: '\'propWithoutTypeDefinition\' is missing in props validation'
      }, {
        message: '\'name\' is missing in props validation'
      }]
    }, {
      code: [
        'class Hello extends React.Component {',
        '  render() {',
        '    var { firstname, lastname } = this.props;',
        '    return <div>Hello</div>;',
        '  }',
        '}',
        'Hello.propTypes = {',
        '  firstname: React.PropTypes.string',
        '};'
      ].join('\n'),
      ecmaFeatures: {
        classes: true,
        destructuring: true,
        jsx: true
      },
      errors: [{
        message: '\'lastname\' is missing in props validation for Hello'
      }]
    }, {
      code: [
        'class Hello extends React.Component {',
        '  static propTypes: { ',
        '    firstname: React.PropTypes.string',
        '  }',
        '  render() {',
        '    return <div>Hello {this.props.firstname}</div>;',
        '  }',
        '}'
      ].join('\n'),
      parser: 'babel-eslint',
      ecmaFeatures: {
        classes: true,
        jsx: true
      },
      errors: [{
        message: '\'firstname\' is missing in props validation for Hello'
      }]
    }
  ]
});
Example #13
0
eslintTester.addRuleTest("lib/rules/block-scoped-var", {
    valid: [
        // See issue https://github.com/eslint/eslint/issues/2242
        { code: "function f() { } f(); var exports = { f: f };", ecmaFeatures: {modules: true} },
        { code: "var f = () => {}; f(); var exports = { f: f };", ecmaFeatures: {arrowFunctions: true, modules: true} },
        "!function f(){ f; }",
        "function f() { } f(); var exports = { f: f };",
        "function f() { var a, b; { a = true; } b = a; }",
        "var a; function f() { var b = a; }",
        "function f(a) { }",
        "!function(a) { };",
        "!function f(a) { };",
        "function f(a) { var b = a; }",
        "!function f(a) { var b = a; };",
        "function f() { var g = f; }",
        "function f() { } function g() { var f = g; }",
        "function f() { var hasOwnProperty; { hasOwnProperty; } }",
        "function f(){ a; b; var a, b; }",
        "function f(){ g(); function g(){} }",
        { code: "function myFunc(foo) {  \"use strict\";  var { bar } = foo;  bar.hello();}", ecmaFeatures: { destructuring: true } },
        { code: "function myFunc(foo) {  \"use strict\";  var [ bar ]  = foo;  bar.hello();}", ecmaFeatures: { destructuring: true } },
        { code: "function myFunc(...foo) {  return foo;}", ecmaFeatures: { restParams: true } },
        { code: "var f = () => { var g = f; }", ecmaFeatures: { arrowFunctions: true } },
        { code: "class Foo {}\nexport default Foo;", ecmaFeatures: { modules: true, classes: true } },
        { code: "new Date", globals: {Date: false} },
        { code: "new Date", globals: {} },
        { code: "var eslint = require('eslint');", globals: {require: false} },
        { code: "var fun = function({x}) {return x;};", ecmaFeatures: { destructuring: true } },
        { code: "var fun = function([,x]) {return x;};", ecmaFeatures: { destructuring: true } },
        "function f(a) { return a.b; }",
        "var a = { \"foo\": 3 };",
        "var a = { foo: 3 };",
        "var a = { foo: 3, bar: 5 };",
        "var a = { set foo(a){}, get bar(){} };",
        "function f(a) { return arguments[0]; }",
        "function f() { }; var a = f;",
        "var a = f; function f() { };",
        "function f(){ for(var i; i; i) i; }",
        "function f(){ for(var a=0, b=1; a; b) a, b; }",
        "function f(){ for(var a in {}) a; }",
        "function f(){ switch(2) { case 1: var b = 2; b; break; default: b; break;} b; }",
        "a:;",
        { code: "const React = require(\"react/addons\");const cx = React.addons.classSet;", globals: { require: false }, ecmaFeatures: { globalReturn: true, modules: true, blockBindings: true }},
        { code: "var v = 1;  function x() { return v; };", ecmaFeatures: { globalReturn: true }},
        { code: "import * as y from \"./other.js\"; y();", ecmaFeatures: { modules: true }},
        { code: "import y from \"./other.js\"; y();", ecmaFeatures: { modules: true }},
        { code: "import {x as y} from \"./other.js\"; y();", ecmaFeatures: { modules: true }},
        { code: "class Test { myFunction() { return true; }}", ecmaFeatures: { classes: true }},
        { code: "class Test { get flag() { return true; }}", ecmaFeatures: { classes: true }},
        { code: "var Test = class { myFunction() { return true; }}", ecmaFeatures: { classes: true }},
        { code: "var doStuff; let {x: y} = {x: 1}; doStuff(y);", ecmaFeatures: { blockBindings: true, destructuring: true }},
        { code: "function foo({x: y}) { return y; }", ecmaFeatures: { blockBindings: true, destructuring: true }}
    ],
    invalid: [
        { code: "!function f(){}; f", errors: [{ message: "\"f\" used outside of binding context." }] },
        { code: "var f = function foo() { }; foo(); var exports = { f: foo };", errors: [{ message: "\"foo\" used outside of binding context." }, { message: "\"foo\" used outside of binding context."}] },
        { code: "var f = () => { x; }", ecmaFeatures: { arrowFunctions: true }, errors: [{ message: "\"x\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f(){ x; }", errors: [{ message: "\"x\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f(){ x; { var x; } }", errors: [{ message: "\"x\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f(){ { var x; } x; }", errors: [{ message: "\"x\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f() { var a; { var b = 0; } a = b; }", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f() { try { var a = 0; } catch (e) { var b = a; } }", errors: [{ message: "\"a\" used outside of binding context.", type: "Identifier" }] },
        { code: "var eslint = require('eslint');", globals: {}, errors: [{ message: "\"require\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f(a) { return a[b]; }", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f() { return b.a; }", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        { code: "var a = { foo: bar };", errors: [{ message: "\"bar\" used outside of binding context.", type: "Identifier" }] },
        { code: "var a = { foo: foo };", errors: [{ message: "\"foo\" used outside of binding context.", type: "Identifier" }] },
        { code: "var a = { bar: 7, foo: bar };", errors: [{ message: "\"bar\" used outside of binding context.", type: "Identifier" }] },
        { code: "var a = arguments;", errors: [{ message: "\"arguments\" used outside of binding context.", type: "Identifier" }] },
        { code: "function x(){}; var a = arguments;", errors: [{ message: "\"arguments\" used outside of binding context.", type: "Identifier" }] },
        { code: "function z(b){}; var a = b;", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        { code: "function z(){var b;}; var a = b;", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        { code: "function f(){ try{}catch(e){} e }", errors: [{ message: "\"e\" used outside of binding context.", type: "Identifier" }] },
        { code: "a:b;", errors: [{ message: "\"b\" used outside of binding context.", type: "Identifier" }] },
        {
            code: "function a() { for(var b in {}) { var c = b; } c; }",
            errors: [{ message: "\"c\" used outside of binding context.", type: "Identifier" }]
        },
        {
            code: "function a() { for(var b of {}) { var c = b;} c; }",
            ecmaFeatures: { forOf: true },
            errors: [{ message: "\"c\" used outside of binding context.", type: "Identifier" }]
        }
    ]
});
Example #14
0
/**
 * @fileoverview Tests for no-caller rule.
 * @author Nicholas C. Zakas
 */

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-caller", {
    valid: [
        "var x = arguments.length",
        "var x = arguments",
        "var x = arguments[0]",
        "var x = arguments[caller]"
    ],
    invalid: [
        { code: "var x = arguments.callee", errors: [{ message: "Avoid arguments.callee.", type: "MemberExpression"}] },
        { code: "var x = arguments.caller", errors: [{ message: "Avoid arguments.caller.", type: "MemberExpression"}] }
    ]
});
eslintTester.addRuleTest("lib/rules/object-curly-spacing", {

    valid: [

        // always - object literals
        { code: "var obj = { foo: bar, baz: qux };", options: ["always"] },
        { code: "var obj = { foo: { bar: quxx }, baz: qux };", options: ["always"] },
        { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["always"] },

        // always - destructuring
        { code: "var { x } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var { x, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var { x,y } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var {\nx,y } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var {\nx,y\n} = z", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var { x = 10, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var { x: { z }, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var {\ny,\n} = x", options: ["always"], ecmaFeatures: { destructuring: true } },
        { code: "var { y, } = x", options: ["always"], ecmaFeatures: { destructuring: true } },

        // always - import / export
        { code: "import { door } from 'room'", options: ["always"], ecmaFeatures: { modules: true } },
        { code: "import {\ndoor } from 'room'", options: ["always"], ecmaFeatures: { modules: true } },
        { code: "export { door } from 'room'", options: ["always"], ecmaFeatures: { modules: true } },
        { code: "import { house, mouse } from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } },
        { code: "export { door }", options: ["always"], ecmaFeatures: { modules: true } },
        { code: "import 'room'", options: ["always"], ecmaFeatures: { modules: true } },

        // always - empty object
        { code: "var foo = {};", options: ["always"] },

        // always - objectsInObjects
        { code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};", options: ["always", {"objectsInObjects": false}] },

        // always - arraysInObjects
        { code: "var obj = { 'foo': [ 1, 2 ]};", options: ["always", {"arraysInObjects": false}] },

        // always - arraysInObjects, objectsInObjects
        { code: "var obj = { 'qux': [ 1, 2 ], 'foo': { 'bar': 1, 'baz': 2 }};", options: ["always", {"arraysInObjects": false, "objectsInObjects": false}] },

        // always - arraysInObjects, objectsInObjects (reverse)
        { code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }, 'qux': [ 1, 2 ]};", options: ["always", {"arraysInObjects": false, "objectsInObjects": false}] },

        // never
        { code: "var obj = {foo: bar,\nbaz: qux\n};", options: ["never"] },
        { code: "var obj = {\nfoo: bar,\nbaz: qux};", options: ["never"] },

        // never - object literals
        { code: "var obj = {foo: bar, baz: qux};", options: ["never"] },
        { code: "var obj = {foo: {bar: quxx}, baz: qux};", options: ["never"] },
        { code: "var obj = {foo: {\nbar: quxx}, baz: qux\n};", options: ["never"] },
        { code: "var obj = {foo: {\nbar: quxx\n}, baz: qux};", options: ["never"] },
        { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["never"] },

        // never - destructuring
        { code: "var {x} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {x, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {x,y} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {\nx,y\n} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {x = 10} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {x = 10, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {x: {z}, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {\nx: {z\n}, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {\ny,\n} = x", options: ["never"], ecmaFeatures: { destructuring: true } },
        { code: "var {y,} = x", options: ["never"], ecmaFeatures: { destructuring: true } },

        // never - import / export
        { code: "import {door} from 'room'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "export {door} from 'room'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "import {\ndoor} from 'room'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "export {\ndoor\n} from 'room'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "import {house,mouse} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "import {house, mouse} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "export {door}", options: ["never"], ecmaFeatures: { modules: true } },
        { code: "import 'room'", options: ["never"], ecmaFeatures: { modules: true } },

        // never - empty object
        { code: "var foo = {};", options: ["never"] },

        // never - objectsInObjects
        { code: "var obj = {'foo': {'bar': 1, 'baz': 2} };", options: ["never", {"objectsInObjects": true}]}

    ],

    invalid: [
        {
            code: "import {bar} from 'foo.js';",
            options: ["always"],
            ecmaFeatures: {
                modules: true
            },
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ImportDeclaration",
                    line: 1,
                    column: 8
                },
                {
                    message: "A space is required before '}'",
                    type: "ImportDeclaration",
                    line: 1,
                    column: 12
                }
            ]
        },
        {
            code: "export {bar};",
            options: ["always"],
            ecmaFeatures: {
                modules: true
            },
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ExportNamedDeclaration",
                    line: 1,
                    column: 8
                },
                {
                    message: "A space is required before '}'",
                    type: "ExportNamedDeclaration",
                    line: 1,
                    column: 12
                }
            ]
        },

        // always - arraysInObjects
        {
            code: "var obj = { 'foo': [ 1, 2 ] };",
            options: ["always", {"arraysInObjects": false}],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression"
                }
            ]
        },
        {
            code: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ] };",
            options: ["always", {"arraysInObjects": false}],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression"
                }
            ]
        },

        // always-objectsInObjects
        {
            code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 } };",
            options: ["always", {"objectsInObjects": false}],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 43
                }
            ]
        },
        {
            code: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 } };",
            options: ["always", {"objectsInObjects": false}],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 61
                }
            ]
        },

        // always-destructuring trailing comma
        {
            code: "var { a,} = x;",
            options: ["always"],
            ecmaFeatures: { destructuring: true },
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 9
                }
            ]
        },
        {
            code: "var {a, } = x;",
            options: ["never"],
            ecmaFeatures: { destructuring: true },
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 9
                }
            ]
        },

        // never-objectsInObjects
        {
            code: "var obj = {'foo': {'bar': 1, 'baz': 2}};",
            options: ["never", {"objectsInObjects": true}],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 39
                }
            ]
        },
        {
            code: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2}};",
            options: ["never", {"objectsInObjects": true}],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 55
                }
            ]
        },

        // always & never
        {
            code: "var obj = {foo: bar, baz: qux};",
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 11
                },
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 30
                }
            ]
        },
        {
            code: "var obj = {foo: bar, baz: qux };",
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 11
                }
            ]
        },
        {
            code: "var obj = { foo: bar, baz: qux};",
            options: ["always"],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 31
                }
            ]
        },
        {
            code: "var obj = { foo: bar, baz: qux };",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 11
                },
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 32
                }
            ]
        },
        {
            code: "var obj = {foo: bar, baz: qux };",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 31
                }
            ]
        },
        {
            code: "var obj = { foo: bar, baz: qux};",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 11
                }
            ]
        },
        {
            code: "var obj = { foo: { bar: quxx}, baz: qux};",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 11
                },
                {
                    message: "There should be no space after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 18
                }
            ]
        },
        {
            code: "var obj = {foo: {bar: quxx }, baz: qux };",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 28
                },
                {
                    message: "There should be no space before '}'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 40
                }
            ]
        },
        {
            code: "export const thing = {value: 1 };",
            ecmaFeatures: {
                modules: true,
                blockBindings: true
            },
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ObjectExpression",
                    line: 1,
                    column: 22
                }
            ]
        },

        // destructuring
        {
            code: "var {x, y} = y",
            ecmaFeatures: {destructuring: true},
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 5
                },
                {
                    message: "A space is required before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 10
                }
            ]
        },
        {
            code: "var { x, y} = y",
            ecmaFeatures: {destructuring: true},
            options: ["always"],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 11
                }
            ]
        },
        {
            code: "var { x, y } = y",
            ecmaFeatures: {destructuring: true},
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '{'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 5
                },
                {
                    message: "There should be no space before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 12
                }
            ]
        },
        {
            code: "var {x, y } = y",
            ecmaFeatures: {destructuring: true},
            options: ["never"],
            errors: [
                {
                    message: "There should be no space before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 11
                }
            ]
        },
        {
            code: "var { x=10} = y",
            ecmaFeatures: {destructuring: true},
            options: ["always"],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 11
                }
            ]
        },
        {
            code: "var {x=10 } = y",
            ecmaFeatures: {destructuring: true},
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '{'",
                    type: "ObjectPattern",
                    line: 1,
                    column: 5
                }
            ]
        },

        // never - arraysInObjects
        {
            code: "var obj = {'foo': [1, 2]};",
            options: ["never", {"arraysInObjects": true}],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression"
                }
            ]
        },
        {
            code: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux']};",
            options: ["never", {"arraysInObjects": true}],
            errors: [
                {
                    message: "A space is required before '}'",
                    type: "ObjectExpression"
                }
            ]
        }
    ]
});
eslintTester.addRuleTest("lib/rules/computed-property-spacing", {

    valid: [

        // default - never
        { code: "obj[foo]" },
        { code: "obj['foo']" },
        { code: "var x = {[b]: a}", ecmaFeatures: { objectLiteralComputedProperties: true } },

        // always
        { code: "obj[ foo ]", options: ["always"] },
        { code: "obj[\nfoo\n]", options: ["always"] },
        { code: "obj[ 'foo' ]", options: ["always"] },
        { code: "obj[ 'foo' + 'bar' ]", options: ["always"] },
        { code: "obj[ obj2[ foo ] ]", options: ["always"] },
        { code: "obj.map(function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] },
        { code: "obj[ 'map' ](function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] },
        { code: "obj[ 'for' + 'Each' ](function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["always"] },
        { code: "obj[ obj2[ foo ] ]", options: ["always"] },
        { code: "var foo = obj[ 1 ]", options: ["always"] },
        { code: "var foo = obj[ 'foo' ];", options: ["always"] },
        { code: "var foo = obj[ [1, 1] ];", options: ["always"] },

        // always - objectLiteralComputedProperties
        { code: "var x = {[ \"a\" ]: a}", options: ["always"], ecmaFeatures: { "objectLiteralComputedProperties": true } },
        { code: "var y = {[ x ]: a}", options: ["always"], ecmaFeatures: { "objectLiteralComputedProperties": true } },
        { code: "var x = {[ \"a\" ]() {}}", options: ["always"], ecmaFeatures: { objectLiteralComputedProperties: true, objectLiteralShorthandMethods: true } },
        { code: "var y = {[ x ]() {}}", options: ["always"], ecmaFeatures: { objectLiteralComputedProperties: true, objectLiteralShorthandMethods: true } },

        // always - unrelated cases
        { code: "var foo = {};", options: ["always"] },
        { code: "var foo = [];", options: ["always"] },

        // never
        { code: "obj[foo]", options: ["never"] },
        { code: "obj['foo']", options: ["never"] },
        { code: "obj['foo' + 'bar']", options: ["never"] },
        { code: "obj['foo'+'bar']", options: ["never"] },
        { code: "obj[obj2[foo]]", options: ["never"] },
        { code: "obj.map(function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] },
        { code: "obj['map'](function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] },
        { code: "obj['for' + 'Each'](function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] },
        { code: "obj['for' + 'Each'](function (item) { return [\n1,\n2,\n3,\n4\n]; })", options: ["never"] },
        { code: "obj[\nfoo]", options: ["never"] },
        { code: "obj[foo\n]", options: ["never"] },
        { code: "var foo = obj[1]", options: ["never"] },
        { code: "var foo = obj['foo'];", options: ["never"] },
        { code: "var foo = obj[[ 1, 1 ]];", options: ["never"] },

        // never - objectLiteralComputedProperties
        { code: "var x = {[\"a\"]: a}", options: ["never"], ecmaFeatures: { objectLiteralComputedProperties: true } },
        { code: "var y = {[x]: a}", options: ["never"], ecmaFeatures: { objectLiteralComputedProperties: true } },
        { code: "var x = {[\"a\"]() {}}", options: ["never"], ecmaFeatures: { objectLiteralComputedProperties: true, objectLiteralShorthandMethods: true } },
        { code: "var y = {[x]() {}}", options: ["never"], ecmaFeatures: { objectLiteralComputedProperties: true, objectLiteralShorthandMethods: true } },

        // never - unrelated cases
        { code: "var foo = {};", options: ["never"] },
        { code: "var foo = [];", options: ["never"] }
    ],

    invalid: [
        {
            code: "var foo = obj[ 1];",
            options: ["always"],
            errors: [
                {
                    message: "A space is required before ']'",
                    type: "MemberExpression",
                    column: 17,
                    line: 1
                }
            ]
        },
        {
            code: "var foo = obj[1 ];",
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '['",
                    type: "MemberExpression",
                    column: 14,
                    line: 1
                }
            ]
        },
        {
            code: "var foo = obj[ 1];",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "MemberExpression",
                    column: 14,
                    line: 1
                }
            ]
        },
        {
            code: "var foo = obj[1 ];",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space before ']'",
                    type: "MemberExpression"
                }
            ]
        },
        {
            code: "var foo = obj[ 1];",
            options: ["always"],
            errors: [
                {
                    message: "A space is required before ']'",
                    type: "MemberExpression",
                    column: 17,
                    line: 1
                }
            ]
        },
        {
            code: "var foo = obj[1 ];",
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '['",
                    type: "MemberExpression",
                    column: 14,
                    line: 1
                }
            ]
        },
        {
            code: "obj[ foo ]",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "MemberExpression",
                    column: 4,
                    line: 1
                },
                {
                    message: "There should be no space before ']'",
                    type: "MemberExpression",
                    column: 10,
                    line: 1
                }
            ]
        },
        {
            code: "obj[foo ]",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space before ']'",
                    type: "MemberExpression",
                    column: 9,
                    line: 1
                }
            ]
        },
        {
            code: "obj[ foo]",
            options: ["never"],
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "MemberExpression",
                    column: 4,
                    line: 1
                }
            ]
        },
        {
            code: "var foo = obj[1]",
            options: ["always"],
            errors: [
                {
                    message: "A space is required after '['",
                    type: "MemberExpression",
                    column: 14,
                    line: 1
                },
                {
                    message: "A space is required before ']'",
                    type: "MemberExpression",
                    column: 16,
                    line: 1
                }
            ]
        },

        // always - objectLiteralComputedProperties
        {
            code: "var x = {[a]: b}",
            options: ["always"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "A space is required after '['",
                    type: "Property",
                    column: 10,
                    line: 1
                },
                {
                    message: "A space is required before ']'",
                    type: "Property",
                    column: 12,
                    line: 1
                }
            ]
        },
        {
            code: "var x = {[a ]: b}",
            options: ["always"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "A space is required after '['",
                    type: "Property",
                    column: 10,
                    line: 1
                }
            ]
        },
        {
            code: "var x = {[ a]: b}",
            options: ["always"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "A space is required before ']'",
                    type: "Property",
                    column: 13,
                    line: 1
                }
            ]
        },

        // never - objectLiteralComputedProperties
        {
            code: "var x = {[ a ]: b}",
            options: ["never"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "Property",
                    column: 10,
                    line: 1
                },
                {
                    message: "There should be no space before ']'",
                    type: "Property",
                    column: 14,
                    line: 1
                }
            ]
        },
        {
            code: "var x = {[a ]: b}",
            options: ["never"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "There should be no space before ']'",
                    type: "Property",
                    column: 13,
                    line: 1
                }
            ]
        },
        {
            code: "var x = {[ a]: b}",
            options: ["never"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "Property",
                    column: 10,
                    line: 1
                }
            ]
        },
        {
            code: "var x = {[ a\n]: b}",
            options: ["never"],
            ecmaFeatures: { "objectLiteralComputedProperties": true },
            errors: [
                {
                    message: "There should be no space after '['",
                    type: "Property",
                    column: 10,
                    line: 1
                }
            ]
        }

    ]
});
Example #17
0
eslintTester.addRuleTest("lib/rules/camelcase", {
    valid: [
        "firstName = \"Nicholas\"",
        "FIRST_NAME = \"Nicholas\"",
        "__myPrivateVariable = \"Patrick\"",
        "myPrivateVariable_ = \"Patrick\"",
        "function doSomething(){}",
        "do_something()",
        "foo.do_something()",
        "var foo = bar.baz_boom;",
        "var foo = bar.baz_boom.something;",
        "foo.boom_pow.qux = bar.baz_boom.something;",
        "if (bar.baz_boom) {}",
        "var obj = { key: foo.bar_baz };",
        "var arr = [foo.bar_baz];",
        "[foo.bar_baz]",
        "var arr = [foo.bar_baz.qux];",
        "[foo.bar_baz.nesting]",
        "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }",
        {
            code: "var o = {key: 1}",
            args: [2, {properties: "always"}]
        },
        {
            code: "var o = {bar_baz: 1}",
            args: [2, {properties: "never"}]
        },
        {
            code: "obj.a_b = 2;",
            args: [2, {properties: "never"}]
        },
        {
            code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;",
            args: [2, {properties: "never"}]
        },
        {
            code: "obj.foo_bar = function(){};",
            args: [2, {properties: "never"}]
        }
    ],
    invalid: [
        {
            code: "first_name = \"Nicholas\"",
            errors: [
                {
                    message: "Identifier 'first_name' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "__private_first_name = \"Patrick\"",
            errors: [
                {
                    message: "Identifier '__private_first_name' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "function foo_bar(){}",
            errors: [
                {
                    message: "Identifier 'foo_bar' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "obj.foo_bar = function(){};",
            errors: [
                {
                    message: "Identifier 'foo_bar' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "bar_baz.foo = function(){};",
            errors: [
                {
                    message: "Identifier 'bar_baz' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "[foo_bar.baz]",
            errors: [
                {
                    message: "Identifier 'foo_bar' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }",
            errors: [
                {
                    message: "Identifier 'foo_bar' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "foo.bar_baz = boom.bam_pow",
            errors: [
                {
                    message: "Identifier 'bar_baz' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "var foo = { bar_baz: boom.bam_pow }",
            errors: [
                {
                    message: "Identifier 'bar_baz' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "foo.qux.boom_pow = { bar: boom.bam_pow }",
            errors: [
                {
                    message: "Identifier 'boom_pow' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "var o = {bar_baz: 1}",
            args: [2, {properties: "always"}],
            errors: [
                {
                    message: "Identifier 'bar_baz' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "obj.a_b = 2;",
            args: [2, {properties: "always"}],
            errors: [
                {
                    message: "Identifier 'a_b' is not in camel case.",
                    type: "Identifier"
                }
            ]
        },
        {
            code: "obj.a_b = 2;",
            args: [2, {properties: "always"}],
            errors: [
                {
                    message: "Identifier 'a_b' is not in camel case.",
                    type: "Identifier"
                }
            ]
        }

    ]
});
Example #18
0
/**
 * @fileoverview Tests for new-cap rule.
 * @author Nicholas C. Zakas
 */

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslintTester = require("eslint-tester");

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

eslintTester.addRuleTest("lib/rules/new-cap", {
    valid: [
        "var x = new Constructor();",
        "var x = new a.b.Constructor();",
        "var x = new function(){};"
    ],
    invalid: [
        { code: "var x = new c();", errors: [{ message: "A constructor name should start with an uppercase letter.", type: "NewExpression"}] }
    ]
});
Example #19
0
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslint = require("../../../lib/eslint"),
    ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-comma-dangle", {
    valid: [
        "var foo = { bar: \"baz\" }",
        "var foo = [ \"baz\" ]",
        "[,,]",
        "[,]",
        "[]"
    ],
    invalid: [
        { code: "var foo = { bar: \"baz\", }", errors: [{ message: "Trailing comma.", type: "Property"}] },
        { code: "foo({ bar: \"baz\", qux: \"quux\", });", errors: [{ message: "Trailing comma.", type: "Property"}] },
        { code: "var foo = [ \"baz\", ]", errors: [{ message: "Trailing comma.", type: "Literal"}] },
        { code: "var foo = { bar: \"bar\"\n\n, }", errors: [{ message: "Trailing comma.", line: 3}] }
    ]
});
// Tests
//------------------------------------------------------------------------------

var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-native-jquery", {

    valid: [
        "Backbone.View.extend({});",
        "Backbone.View.extend({ render: function() { var a = this.$('.item').offset(); } });",
        "Backbone.View.extend({ test: function() { return $.isArray(a); } });",
        "Backbone.Model.extend({ initialize: function() { var a = $('.item').offset(); } });",
        "var a = 6 * 7;",
        "var a = $('.item').offset();"
    ],

    invalid: [
        {
            code: "Backbone.View.extend({ render: function() { var a = $('.item').offset(); } });",
            errors: [ { message: "Use this.$ instead of $ in views" } ]
        },
        {
            code: "var a = Backbone.View.extend({ render: function() { var a = $('.item').offset(); } }); var b = Backbone.View.extend({ render: function() { var a = this.$('.item').offset() } });",
            errors: [ { message: "Use this.$ instead of $ in views" } ]
        },
        {
            code: "Backbone.View.extend({ initialize: function() { Backbone.View.apply(this, arguments); var a = $('.item').offset(); } });",
            errors: [ { message: "Use this.$ instead of $ in views" } ]
        }
    ]
});
Example #21
0
eslintTester.addRuleTest("lib/rules/comma-style", {

    valid: [
        "var foo = 1, bar = 3;",
        "var foo = {'a': 1, 'b': 2};",
        "var foo = [1, 2];",
        "var foo = [, 2];",
        "var foo = [1, ];",
        "var foo = ['apples', \n 'oranges'];",
        "var foo = {'a': 1, \n 'b': 2, \n'c': 3};",
        "var foo = {'a': 1, \n 'b': 2, 'c':\n 3};",
        "var foo = {'a': 1, \n 'b': 2, 'c': [{'d': 1}, \n {'e': 2}, \n {'f': 3}]};",
        {code: "var foo = [1, \n2, \n3];"},
        {code: "function foo(){var a=[1,\n 2]}"},
        {code: "function foo(){return {'a': 1,\n'b': 2}}"},
        {code: "var foo = \n1, \nbar = \n2;"},
        {code: "var foo = \n1, \nbar = [1,\n2,\n3]"},
        {code: "var foo = ['apples'\n,'oranges'];", args: ["2", "first"]},
        {code: "var foo = 1, bar = 2;", args: ["2", "first"]},
        {code: "var foo = 1 \n ,bar = 2;", args: ["2", "first"]},
        {code: "var foo = {'a': 1 \n ,'b': 2 \n,'c': 3};", args: ["2", "first"]},
        {code: "var foo = [1 \n ,2 \n, 3];", args: ["2", "first"]},
        {code: "function foo(){return {'a': 1\n,'b': 2}}", args: ["2", "first"]},
        {code: "function foo(){var a=[1\n, 2]}", args: ["2", "first"]},
        {
            code: "var a = 'a',\no = 'o';",
            args: [2, "first", {exceptions: {VariableDeclaration: true}}]
        },
        {
            code: "var arr = ['a',\n'o'];",
            args: [2, "first", {exceptions: {ArrayExpression: true}}]
        },
        {
            code: "var obj = {a: 'a',\nb: 'b'};",
            args: [2, "first", {exceptions: {ObjectExpression: true}}]
        },
        {
            code: "var a = 'a',\no = 'o',\narr = [1,\n2];",
            args: [2, "first", {exceptions: {VariableDeclaration: true, ArrayExpression: true}}]
        },
        {
            code: "var ar ={fst:1,\nsnd: [1,\n2]};",
            args: [2, "first", {exceptions: {ArrayExpression: true, ObjectExpression: true}}]
        },
        {
            code: "var ar ={fst:1,\nsnd: [1,\n2]};",
            args: [2, "first", {exceptions: {ArrayExpression: true, ObjectExpression: true}}]
        },
        {
            code: "var a = 'a',\nar ={fst:1,\nsnd: [1,\n2]};",
            args: [2, "first",
              {exceptions: {
                    ArrayExpression: true,
                    ObjectExpression: true,
                    VariableDeclaration: true
              }}]
        }
    ],

    invalid: [
        {
            code: "var foo = 1\n,\nbar = 2;",
            errors: [{
                message: BAD_LN_BRK_MSG,
                type: "VariableDeclarator"
            }]
        },
        {
            code: "var foo = 1\n,bar = 2;",
            errors: [{
                message: LAST_MSG,
                type: "VariableDeclarator"
            }]
        },
        {
            code: "f([1,2\n,3]);",
            errors: [{
                message: LAST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "f([1,2\n,]);",
            errors: [{
                message: LAST_MSG,
                type: "Punctuator"
            }]
        },
        {
            code: "f([,2\n,3]);",
            errors: [{
                message: LAST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var foo = ['apples'\n, 'oranges'];",
            errors: [{
                message: LAST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var foo = 1,\nbar = 2;",
            args: [2, "first"],
            errors: [{
                message: FIRST_MSG,
                type: "VariableDeclarator"
            }]
        },
        {
            code: "f([1,\n2,3]);",
            args: [2, "first"],
            errors: [{
                message: FIRST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var foo = ['apples', \n 'oranges'];",
            args: [2, "first"],
            errors: [{
                message: FIRST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var foo = {'a': 1, \n 'b': 2\n ,'c': 3};",
            args: [2, "first"],
            errors: [{
                message: FIRST_MSG,
                type: "Property"
            }]
        },
        {
            code: "var foo = {'a': 1, \n 'b': 2\n ,'c': 3};",
            args: [2, "first"],
            errors: [{
                message: FIRST_MSG,
                type: "Property"
            }]
        },
        {
            code: "var a = 'a',\no = 'o',\narr = [1,\n2];",
            args: [2, "first", {exceptions: {VariableDeclaration: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var a = 'a',\nobj = {a: 'a',\nb: 'b'};",
            args: [2, "first", {exceptions: {VariableDeclaration: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "Property"
            }]
        },
        {
            code: "var a = 'a',\nobj = {a: 'a',\nb: 'b'};",
            args: [2, "first", {exceptions: {ObjectExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "VariableDeclarator"
            }]
        },
        {
            code: "var a = 'a',\narr = [1,\n2];",
            args: [2, "first", {exceptions: {ArrayExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "VariableDeclarator"
            }]
        },
        {
            code: "var ar =[1,\n{a: 'a',\nb: 'b'}];",
            args: [2, "first", {exceptions: {ArrayExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "Property"
            }]
        },
        {
            code: "var ar =[1,\n{a: 'a',\nb: 'b'}];",
            args: [2, "first", {exceptions: {ObjectExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "ObjectExpression"
            }]
        },
        {
            code: "var ar ={fst:1,\nsnd: [1,\n2]};",
            args: [2, "first", {exceptions: {ObjectExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "Literal"
            }]
        },
        {
            code: "var ar ={fst:1,\nsnd: [1,\n2]};",
            args: [2, "first", {exceptions: {ArrayExpression: true}}],
            errors: [{
                message: FIRST_MSG,
                type: "Property"
            }]
        }
    ]
});
Example #22
0
eslintTester.addRuleTest("lib/rules/no-extend-native", {
    valid: [
        "x.prototype.p = 0",
        "x.prototype['p'] = 0",
        "Math.prototype.p = 0",
        "Object.p = 0",
        "Object.toString.bind = 0",
        "Object['toString'].bind = 0",
        "Object.defineProperty(x, 'p', {value: 0})",
        "global.Object.prototype.toString = 0",
        "this.Object.prototype.toString = 0",
        "with(Object) { prototype.p = 0; }",
        "o = Object; o.prototype.toString = 0",
        "eval('Object.prototype.toString = 0')"
    ],
    invalid: [{
        code: "Object.prototype.p = 0",
        errors: [{
            message: "Object prototype is read only, properties should not be added.",
            type: "AssignmentExpression"
        }]
    }, {
        code: "Function.prototype['p'] = 0",
        errors: [{
            message: "Function prototype is read only, properties should not be added.",
            type: "AssignmentExpression"
        }]
    }, {
        code: "String['prototype'].p = 0",
        errors: [{
            message: "String prototype is read only, properties should not be added.",
            type: "AssignmentExpression"
        }]
    }, {
        code: "Number['prototype']['p'] = 0",
        errors: [{
            message: "Number prototype is read only, properties should not be added.",
            type: "AssignmentExpression"
        }]
    }, {
        code: "Object.defineProperty(Array.prototype, 'p', {value: 0})",
        errors: [{
            message: "Array prototype is read only, properties should not be added.",
            type: "CallExpression"
        }]
    }]
});
 */

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var eslintTester = require("eslint-tester");

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

eslintTester.addRuleTest("lib/rules/no-array-constructor", {
    valid: [
        "new Array(x)",
        "Array(x)",
        "new Array(9)",
        "Array(9)",
        "new foo.Array()",
        "foo.Array()",
        "new Array.foo",
        "Array.foo()"
    ],
    invalid: [
        { code: "new Array()", errors: [{ message: "The array literal notation [] is preferrable.", type: "NewExpression"}] },
        { code: "new Array", errors: [{ message: "The array literal notation [] is preferrable.", type: "NewExpression"}] },
        { code: "new Array(x, y)", errors: [{ message: "The array literal notation [] is preferrable.", type: "NewExpression"}] },
        { code: "new Array(0, 1, 2)", errors: [{ message: "The array literal notation [] is preferrable.", type: "NewExpression"}] }
    ]
});
Example #24
0
eslintTester.addRuleTest("lib/rules/no-shadow", {
    valid: [
        "var a=3; function b(x) { a++; return x + a; }; setTimeout(function() { b(a); }, 0);",
        "(function() { var doSomething = function doSomething() {}; doSomething() }())",
        "var arguments;\nfunction bar() { }",
        { code: "var a=3; var b = (x) => { a++; return x + a; }; setTimeout(() => { b(a); }, 0);", ecmaFeatures: { arrowFunctions: true } },
        { code: "class A {}", ecmaFeatures: {classes: true} },
        { code: "class A { constructor() { var a; } }", ecmaFeatures: {classes: true} },
        { code: "(function() { var A = class A {}; })()", ecmaFeatures: {classes: true} },
        { code: "{ var a; } let a;", ecmaFeatures: {blockBindings: true} }, // this case reports `no-redeclare`, not shadowing.
        { code: "{ let a; } let a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ let a; } var a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ let a; } function a() {}", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ const a = 0; } const a = 1;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ const a = 0; } var a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ const a = 0; } function a() {}", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { let a; } let a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { let a; } var a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { let a; } function a() {}", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { var a; } let a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { var a; } var a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { var a; } function a() {}", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo(a) { } let a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo(a) { } var a;", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "function foo(a) { } function a() {}", options: [{hoist: "never"}], ecmaFeatures: {blockBindings: true} },
        { code: "{ let a; } let a;", ecmaFeatures: {blockBindings: true} },
        { code: "{ let a; } var a;", ecmaFeatures: {blockBindings: true} },
        { code: "{ const a = 0; } const a = 1;", ecmaFeatures: {blockBindings: true} },
        { code: "{ const a = 0; } var a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { let a; } let a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { let a; } var a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { var a; } let a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo() { var a; } var a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo(a) { } let a;", ecmaFeatures: {blockBindings: true} },
        { code: "function foo(a) { } var a;", ecmaFeatures: {blockBindings: true} }
    ],
    invalid: [
        {
            code: "function a(x) { var b = function c() { var x = 'foo'; }; }",
            errors: [{
                message: "x is already declared in the upper scope.",
                type: "Identifier",
                line: 1,
                column: 44
            }]
        },
        {
            code: "var a = (x) => { var b = () => { var x = 'foo'; }; }",
            ecmaFeatures: {
                arrowFunctions: true
            },
            errors: [{
                message: "x is already declared in the upper scope.",
                type: "Identifier",
                line: 1,
                column: 38
            }]
        },
        {
            code: "function a(x) { var b = function () { var x = 'foo'; }; }",
            errors: [{
                message: "x is already declared in the upper scope.",
                type: "Identifier",
                line: 1,
                column: 43
            }]
        },
        {
            code: "var x = 1; function a(x) { return ++x; }",
            errors: [{
                message: "x is already declared in the upper scope.",
                type: "Identifier",
                line: 1,
                column: 23
            }]
        },
        {
            code: "var a=3; function b() { var a=10; }",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier" }]
        },
        {
            code: "var a=3; function b() { var a=10; }; setTimeout(function() { b(); }, 0);",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier" }]
        },
        {
            code: "var a=3; function b() { var a=10; var b=0; }; setTimeout(function() { b(); }, 0);",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier" }, { message: "b is already declared in the upper scope.", type: "Identifier" }]
        },
        {
            code: "var x = 1; { let x = 2; }",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "x is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "let x = 1; { const x = 2; }",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "x is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ let a; } function a() {}",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ const a = 0; } function a() {}",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { let a; } function a() {}",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { var a; } function a() {}",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo(a) { } function a() {}",
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ let a; } let a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ let a; } var a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ let a; } function a() {}",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ const a = 0; } const a = 1;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ const a = 0; } var a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "{ const a = 0; } function a() {}",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { let a; } let a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { let a; } var a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { let a; } function a() {}",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { var a; } let a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { var a; } var a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo() { var a; } function a() {}",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo(a) { } let a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo(a) { } var a;",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "function foo(a) { } function a() {}",
            options: [{hoist: "all"}],
            ecmaFeatures: {blockBindings: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function a() { function a(){} })()",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function a() { class a{} })()",
            ecmaFeatures: {classes: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function a() { (function a(){}); })()",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function a() { (class a{}); })()",
            ecmaFeatures: {classes: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = function(a) {}; })()",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = function() { function a() {} }; })()",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = function() { class a{} }; })()",
            ecmaFeatures: {classes: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = function() { (function a() {}); }; })()",
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = function() { (class a{}); }; })()",
            ecmaFeatures: {classes: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function() { var a = class { constructor() { class a {} } }; })()",
            ecmaFeatures: {classes: true},
            errors: [{ message: "a is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "class A { constructor() { var A; } }",
            ecmaFeatures: {classes: true},
            errors: [{ message: "A is already declared in the upper scope.", type: "Identifier"}]
        },
        {
            code: "(function a() { function a(){ function a(){} } })()",
            errors: [
                { message: "a is already declared in the upper scope.", type: "Identifier", line: 1, column: 26},
                { message: "a is already declared in the upper scope.", type: "Identifier", line: 1, column: 40}
            ]
        }
    ]
});
Example #25
0
eslintTester.addRuleTest("lib/rules/space-after-keywords", {
    valid: [
        { code: "switch (a){ default: break; }", args: [1] },
        { code: "if (a) {}", args: [1] },
        { code: "if (a) {} else {}", args: [1] },
        { code: "for (;;){}", args: [1] },
        { code: "while (true) {}", args: [1]},
        { code: "do {} while (0)", args: [1]},
        { code: "do ;while (0)", args: [1]},
        { code: "do ;while ((0))", args: [1]},
        { code: "do (a);while ((0))", args: [1]},
        { code: "do (a)\nwhile ((0))", args: [1]},
        { code: "do ((a))\nwhile ((0))", args: [1]},
        { code: "do; while(0)", args: [1, "never"]},
        { code: "try {} catch (e) {}", args: [1]},
        { code: "with (a) {}", args: [1]},
        { code: "if(a) {}", args: [1, "never"]},
        { code: "if(a){}else{}", args: [1, "never"]},
        { code: "if(a){}else if(b){}else{}", args: [1, "never"]},
        { code: "try {}finally {}", args: [1]},
        { code: "try{} finally{}", args: [1, "never"]},
        { code: "(function(){})", args: [1] },
        { code: "(function(){})", args: [1] }
    ],
    invalid: [
        { code: "if (a) {} else if(b){}", args: [1], errors: [{ message: "Keyword \"if\" must be followed by whitespace.", type: "IfStatement" }] },
        { code: "if (a) {} else{}", args: [1], errors: [{ message: "Keyword \"else\" must be followed by whitespace." }] },
        { code: "switch(a){ default: break; }", errors: [{ message: "Keyword \"switch\" must be followed by whitespace.", type: "SwitchStatement" }] },
        { code: "if(a){}", errors: [{ message: "Keyword \"if\" must be followed by whitespace.", type: "IfStatement" }] },
        { code: "do{} while (0)", args: [1], errors: [{ message: "Keyword \"do\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do ;while(0)", args: [1], errors: [{ message: "Keyword \"while\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do ;while((0))", args: [1], errors: [{ message: "Keyword \"while\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do (a);while((0))", args: [1], errors: [{ message: "Keyword \"while\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do (a)\nwhile((0))", args: [1], errors: [{ message: "Keyword \"while\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do ((a))\nwhile((0))", args: [1], errors: [{ message: "Keyword \"while\" must be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "do;while (0)", args: [1, "never"], errors: [{ message: "Keyword \"while\" must not be followed by whitespace.", type: "DoWhileStatement" }]},
        { code: "if (a) {}", args: [1, "never"], errors: [{ message: "Keyword \"if\" must not be followed by whitespace.", type: "IfStatement" }]},
        { code: "if(a){}else {}", args: [1, "never"], errors: [{ message: "Keyword \"else\" must not be followed by whitespace." }]},
        { code: "if(a){}else if(b){}else {}", args: [1, "never"], errors: [{ message: "Keyword \"else\" must not be followed by whitespace." }]},
        { code: "try{}finally {}", args: [1], errors: [{ message: "Keyword \"try\" must be followed by whitespace." }]},
        { code: "try {}finally{}", args: [1], errors: [{ message: "Keyword \"finally\" must be followed by whitespace." }]},
        { code: "try{}finally {}", args: [1, "never"], errors: [{ message: "Keyword \"finally\" must not be followed by whitespace." }]},
        { code: "try {}finally{}", args: [1, "never"], errors: [{ message: "Keyword \"try\" must not be followed by whitespace." }]}
    ]
});
Example #26
0
eslintTester.addRuleTest("lib/rules/lines-around-comment", {

    valid: [

        // default rules
        { code: "bar()\n/** block block block\n * block \n */\n\nvar a = 1;" },
        { code: "bar()\n\n/** block block block\n * block \n */\n\nvar a = 1;" },
        { code: "bar()\n// line line line \nvar a = 1;" },
        { code: "bar()\n\n// line line line\nvar a = 1;" },
        { code: "bar()\n// line line line\n\nvar a = 1;" },

        // line comments
        {
            code: "bar()\n// line line line\n\nvar a = 1;",
            options: [{ afterLineComment: true }]
        },
        {
            code: "foo()\n\n// line line line\nvar a = 1;",
            options: [{ beforeLineComment: true }]
        },
        {
            code: "foo()\n\n// line line line\n\nvar a = 1;",
            options: [{ beforeLineComment: true, afterLineComment: true }]
        },
        {
            code: "foo()\n\n// line line line\n// line line\n\nvar a = 1;",
            options: [{ beforeLineComment: true, afterLineComment: true }]
        },
        {
            code: "// line line line\n// line line",
            options: [{ beforeLineComment: true, afterLineComment: true }]
        },

        // block comments
        {
            code: "bar()\n\n/** A Block comment with a an empty line after\n *\n */\nvar a = 1;",
            options: [{ afterBlockComment: false, beforeBlockComment: true }]
        },
        {
            code: "bar()\n/** block block block\n * block \n */\nvar a = 1;",
            options: [{ afterBlockComment: false }]
        },
        {
            code: "/** \nblock \nblock block\n */\n/* block \n block \n */",
            options: [{ afterBlockComment: true, beforeBlockComment: true }]
        },
        {
            code: "bar()\n\n/** block block block\n * block \n */\n\nvar a = 1;",
            options: [{ afterBlockComment: true, beforeBlockComment: true }]
        },

        // inline comments (should not ever warn)
        {
            code: "foo() // An inline comment with a an empty line after\nvar a = 1;",
            options: [{ afterLineComment: true, beforeLineComment: true }]
        },
        {
            code: "foo();\nbar() /* An inline block comment with a an empty line after\n *\n */\nvar a = 1;",
            options: [{ beforeBlockComment: true }]
        },

        // mixed comment (some block & some line)
        {
            code: "bar()\n/** block block block\n * block \n */\n//line line line\nvar a = 1;",
            options: [{ afterBlockComment: true }]
        },
        {
            code: "bar()\n/** block block block\n * block \n */\n//line line line\nvar a = 1;",
            options: [{ beforeLineComment: true }]
        }

    ],

    invalid: [

        // default rules
        {
            code: "bar()\n/** block block block\n * block \n */\nvar a = 1;",
            errors: [{ message: afterMessage, type: "Block" }]
        },

        // line comments
        {
            code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
            options: [{ afterLineComment: true }],
            errors: [{ message: afterMessage, type: "Line" }]
        },
        {
            code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
            options: [{ beforeLineComment: true, afterLineComment: false }],
            errors: [{ message: beforeMessage, type: "Line" }]
        },
        {
            code: "// A line comment with no empty line after\nvar a = 1;",
            options: [{ beforeLineComment: true, afterLineComment: true }],
            errors: [{ message: afterMessage, type: "Line", line: 1, column: 0 }]
        },
        {
            code: "baz()\n// A line comment with no empty line after\nvar a = 1;",
            options: [{ beforeLineComment: true, afterLineComment: true }],
            errors: [{ message: beforeMessage, type: "Line", line: 2 }, { message: afterMessage, type: "Line", line: 2 }]
        },

        // block comments
        {
            code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
            options: [{ afterBlockComment: true, beforeBlockComment: true }],
            errors: [{ message: beforeMessage, type: "Block", line: 2 }, { message: afterMessage, type: "Block", line: 2 }]
        },
        {
            code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
            options: [{ afterBlockComment: true, beforeBlockComment: false }],
            errors: [{ message: afterMessage, type: "Block", line: 2 }]
        },
        {
            code: "bar()\n/**\n * block block block\n */\nvar a = 1;",
            options: [{ afterBlockComment: false, beforeBlockComment: true }],
            errors: [{ message: beforeMessage, type: "Block", line: 2 }]
        }
    ]

});
Example #27
0
eslintTester.addRuleTest("lib/rules/brace-style", {
    valid: [
        "function foo () { return; }",
        "function a(b,\nc,\nd) { }",
        "!function foo () { return; }",
        "!function a(b,\nc,\nd) { }",
        "if (foo) { \n bar(); }",
        "if (a) { b(); } else { c(); }",
        "while (foo) { \n bar(); }",
        "for (;;) { \n bar(); }",
        "with (foo) { \n bar(); }",
        "switch (foo) { \n case \"bar\": break; }",
        "try { \n bar();\n } catch (e) {\n baz(); \n }",
        "do { \n bar();\n } while (true)",
        "for (foo in bar) { \n baz(); \n }",
        "if (a &&\n b &&\n c) { \n }",
        "switch(0) {}",
        { code: "if (foo) {\n}\nelse {\n}", args: ["2", "stroustrup"] },
        { code: "try { \n bar();\n }\ncatch (e) {\n baz(); \n }", args: ["2", "stroustrup"] }
    ],
    invalid: [
        { code: "function foo() \n { \n return; }", errors: [{ message: OPEN_MESSAGE, type: "FunctionDeclaration"}] },
        { code: "!function foo() \n { \n return; }", errors: [{ message: OPEN_MESSAGE, type: "FunctionExpression"}] },
        { code: "if (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "IfStatement"}] },
        { code: "if (a) { b(); } else \n { c(); }", errors: [{ message: OPEN_MESSAGE, type: "IfStatement"}] },
        { code: "while (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "WhileStatement"}] },
        { code: "for (;;) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "ForStatement"}] },
        { code: "with (foo) \n { \n bar(); }", errors: [{ message: OPEN_MESSAGE, type: "WithStatement"}] },
        { code: "switch (foo) \n { \n case \"bar\": break; }", errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "switch (foo) \n { }", errors: [{ message: OPEN_MESSAGE, type: "SwitchStatement"}] },
        { code: "try \n { \n bar(); \n } catch (e) {}", errors: [{ message: OPEN_MESSAGE, type: "TryStatement"}] },
        { code: "try { \n bar(); \n } catch (e) \n {}", errors: [{ message: OPEN_MESSAGE, type: "CatchClause"}] },
        { code: "do \n { \n bar(); \n} while (true)", errors: [{ message: OPEN_MESSAGE, type: "DoWhileStatement"}] },
        { code: "for (foo in bar) \n { \n baz(); \n }", errors: [{ message: OPEN_MESSAGE, type: "ForInStatement"}] },
        { code: "try { \n bar(); \n }\ncatch (e) {\n}", errors: [{ message: CLOSE_MESSAGE, type: "CatchClause"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement"}] },
        { code: "if (a) { b(); } \n else { c(); }", errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement" }]},
        { code: "try { \n bar(); \n }\ncatch (e) {\n} finally {\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement"}] },
        { code: "try { \n bar(); \n } catch (e) {\n}\n finally {\n}", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE, type: "CatchClause"}] },
        { code: "if (a) { b(); } else { c(); }", args: ["2", "stroustrup"], errors: [{ message: CLOSE_MESSAGE, type: "BlockStatement" }]}
    ]
});
Example #28
0
eslintTester.addRuleTest("lib/rules/no-multi-spaces", {

    valid: [
        "var a = 1;",
        "var a=1;",
        "var a = 1, b = 2;",
        "var arr = [1, 2];",
        "var arr = [ (1), (2) ];",
        "var obj = {'a': 1, 'b': (2)};",
        "a, b",
        "a >>> b",
        "a ^ b",
        "(a) | (b)",
        "a & b",
        "a << b",
        "a !== b",
        "a >>>= b",
        "if (a & b) { }",
        "function foo(a,b) {}",
        "function foo(a, b) {}",
        "if ( a === 3 && b === 4) {}",
        "if ( a === 3||b === 4 ) {}",
        "if ( a <= 4) {}",
        "var foo = bar === 1 ? 2: 3",
        "[1, , 3]",
        "[1, ]",
        "[ (  1  ) , (  2  ) ]",
        "a = 1, b = 2;",
        "(function(a, b){})",
        "x.in = 0;"
    ],

    invalid: [
        {
            code: "var a =  1",
            errors: [{
                message: "Multiple spaces found after '='.",
                type: "Punctuator"
            }]
        },
        {
            code: "var a = 1,  b = 2;",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "a <<  b",
            errors: [{
                message: "Multiple spaces found after '<<'.",
                type: "Punctuator"
            }]
        },
        {
            code: "var arr = {'a': 1,  'b': 2};",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "if (a &  b) { }",
            errors: [{
                message: "Multiple spaces found after '&'.",
                type: "Punctuator"
            }]
        },
        {
            code: "if ( a === 3  &&  b === 4) {}",
            errors: [{
                message: "Multiple spaces found before '&&'.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after '&&'.",
                type: "Punctuator"
            }]
        },
        {
            code: "var foo = bar === 1 ?  2:  3",
            errors: [{
                message: "Multiple spaces found after '?'.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ':'.",
                type: "Punctuator"
            }]
        },
        {
            code: "var a = [1,  2,  3,  4]",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "var arr = [1,  2];",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "[  , 1,  , 3,  ,  ]",
            errors: [{
                message: "Multiple spaces found before ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "a >>>  b",
            errors: [{
                message: "Multiple spaces found after '>>>'.",
                type: "Punctuator"
            }]
        },
        {
            code: "a = 1,  b =  2;",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }, {
                message: "Multiple spaces found after '='.",
                type: "Punctuator"
            }]
        },
        {
            code: "(function(a,  b){})",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        },
        {
            code: "function foo(a,  b){}",
            errors: [{
                message: "Multiple spaces found after ','.",
                type: "Punctuator"
            }]
        }
    ]
});
Example #29
0
var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/no-illegal-private-usage", {

  valid: [
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { members: { __known: null, foo: function(){ this.__known = true; } } });'
    },
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { members: { __known: null, foo: function(){ qx.foo.Bar.__known = true; } } });'
    },
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { constructor: function(){ this.__known = true }, members: { __known: null } });'
    },
  ],

  invalid: [
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { members: { foo: function(){ this.__notDeclared = false } } });',
      errors: [ { message: "Do not use private \'__notDeclared\' of foreign class" } ]
    },
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { members: { foo: function(){ Foo.__notDeclared = false } } });',
      errors: [ { message: "Do not use private \'__notDeclared\' of foreign class" } ]
    },
    {
      code: 'qx.Bootstrap.define("qx.foo.Bar", { members: { foo: function(){ qx.foo.Bar.__notDeclared = false } } });',
      errors: [ { message: "Do not use private \'__notDeclared\' of foreign class" } ]
    }
  ]

});
Example #30
0
eslintTester.addRuleTest("lib/rules/no-implicit-coercion", {
    valid: [
        {code: "Boolean(foo)"},
        {code: "foo.indexOf(1) !== -1"},
        {code: "Number(foo)"},
        {code: "String(foo)"},
        {code: "!foo"},
        {code: "~foo"},
        {code: "-foo"},
        {code: "2 * foo"},
        {code: "0 + foo"},
        {code: "~foo.bar()"},

        {code: "!!foo", options: [{boolean: false}]},
        {code: "~foo.indexOf(1)", options: [{boolean: false}]},
        {code: "+foo", options: [{number: false}]},
        {code: "1*foo", options: [{number: false}]},
        {code: "\"\"+foo", options: [{string: false}]}
    ],
    invalid: [
        {code: "!!foo", errors: [{message: "use `Boolean(foo)` instead.", type: "UnaryExpression"}]},
        {code: "!!(foo + bar)", errors: [{message: "use `Boolean(foo + bar)` instead.", type: "UnaryExpression"}]},
        {code: "~foo.indexOf(1)", errors: [{message: "use `foo.indexOf(1) !== -1` instead.", type: "UnaryExpression"}]},
        {code: "~foo.bar.indexOf(2)", errors: [{message: "use `foo.bar.indexOf(2) !== -1` instead.", type: "UnaryExpression"}]},
        {code: "+foo", errors: [{message: "use `Number(foo)` instead.", type: "UnaryExpression"}]},
        {code: "+foo.bar", errors: [{message: "use `Number(foo.bar)` instead.", type: "UnaryExpression"}]},
        {code: "1*foo", errors: [{message: "use `Number(foo)` instead.", type: "BinaryExpression"}]},
        {code: "foo*1", errors: [{message: "use `Number(foo)` instead.", type: "BinaryExpression"}]},
        {code: "1*foo.bar", errors: [{message: "use `Number(foo.bar)` instead.", type: "BinaryExpression"}]},
        {code: "\"\"+foo", errors: [{message: "use `String(foo)` instead.", type: "BinaryExpression"}]},
        {code: "foo+\"\"", errors: [{message: "use `String(foo)` instead.", type: "BinaryExpression"}]},
        {code: "\"\"+foo.bar", errors: [{message: "use `String(foo.bar)` instead.", type: "BinaryExpression"}]}
    ]
});