Giter Site home page Giter Site logo

eslint-plugin-react-pug's Introduction

.builder/ $ npm install
.builder/ $ npm start

eslint-plugin-react-pug's People

Contributors

dependabot[bot] avatar ezhlobo avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eslint-plugin-react-pug's Issues

Handle prop types definitions

I'm starting the development of integration with prop-types in react. Here I'm going to provide updates about the progress.

Idea

When we find pug template in the code we will look up to find out where it's located. For now we are interested in functions and classes.

function Component(props) {
  return pug`= props.name`
}

const Component = props => pug`= props.name`

class Component extends React.Component {
  render() {
    return pug`= props.name`
  }
}

We will check out propTypes definitions for those functions and classes and match them with what we use inside them.

Progress

Working branch: prop-types

Items in the checklists will be updated through the time.

  • Functions
    • Grab first-level prop-types definitions
    • Find out first-level props used inside functions
    • Find out first-level props used inside pug
    • Report, when there are definitions for unused props
      • Write comprehensive testing to make sure that we returned all used prop
      • Make it work when we use props in pug templates
    • Report, when there are used props without definition
    • Grab nested prop-types definitions
    • Find out all props used inside functions
  • Anonymous Functions assigned to variable
    • Grab prop-types definitions
    • Find out all props used inside functions
    • Report, when there are used props without definition
    • Report, when there are definitions for unused props
  • Classes
    • Grab prop-types definitions
    • Find out all props used inside functions
    • Report, when there are used props without definition
    • Report, when there are definitions for unused props

possible to show no-undef ?

Not really sure if this is possible or if this is the right module, but it would be super helpful if eslint could show undefined variables used in the pug template.

v0.0.5: Cannot find module './lib/rules/uses-react'

Hi @ezhlobo,

Thanks for the response on my template literals issue. I updated to 0.0.5 and trying to lint a file with nested literals, but I got this error:

βœ– jsLinter found some errors. Please fix them and try committing again.

Cannot find module './lib/rules/uses-react'
Error: Cannot find module './lib/rules/uses-react'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/deploy/projects/node_modules/eslint-plugin-react-pug/index.js:8:17)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)

Listing the contents of eslint-plugin-react-pug/lib shows it's empty.

Lint JavaScript inside pug template

Since pug works within template literals, it's impossible for eslint to achieve JavaScript code used inside.

For the first version I'm going to apply basic set of "stylistic" rules to the code. In the future we might consider using the same config as the other codebase.

Possible API

Will keep it in mind when build the first implementation with own set of rules, probably this will come to in order to allow project-specific settings.

{
  rules: {
    'react-pug/js-rules': ['error', {
      // this is what can be moved directly to eslint config
      'object-curly-spacing': ['error', 'always'],
    }],
  },
}

Below are cases from the real projects

The idea is to combine a good amount of use cases in order to cover them with initial implementation.

  • if this.pageNo()>1
    //-------------^^^-
  • - const test = {test:true }
    //-------------^^---^^---^^-
  • Cell(value=getValueFor(item,test))
    //-------------------------^^-----
  • Cell(
      style={
        color:'red',
        textAlign: 'right'
      }
    )
  • Button(onClick=()=>test( ))
    //--------------^^^^---^^^-
  • Cell The value is #{getValueFor(item,test)}
    //----------------------------------^^-----

Moving to pug-lint

I'm considering to use pug-lint under the hood. It can give us following advantages:

  • Add/improve rules right for all pug projects
  • Keep this project smaller like a bridge between javascript and pug

Some concerns:

  • Will require several ASTs for one check
  • pug-lint is not maintained actively

I'm not using propTypes i don't want liniting for props

'title' is missing in props validation react-pug/prop-types
'items' is missing in props validation react-pug/prop-types

i'm getting error in this file collectionpreview jsx

const CollectionPreview = ({ title, items }) => (
  <div className="collection-preview">
    <h1 className="title">{title.toUpperCase()}</h1>
    <div className="preview">
      {items
        .filter((item, idx) => idx < 4)
        .map(({ id, ...OtherItemProps }) => (
          <CollectionItem key={id} {...OtherItemProps} />
        ))}
    </div>
  </div>

in shop component jsx

class ShopPage extends React.Component {
  state = {
    collections: SHOP_DATA
  };
  render() {
    const { collections } = this.state;
    return (
      <div className="shop-page">
        {collections.map(({ id, ...CollectionProps }) => (
          <CollectionPreview key={id} {...CollectionProps} />
        ))}
      </div>
    );
  }
}

It requires to have empty lines between siblings

I got the next messages on the following code (below the first button and above the second one):

Button(href="#" color="dark" size="sm") Sign In
| &#32;
Button(href="#" color="primary" size="sm") Join now
error  Need empty line for more than two siblings  react-pug/empty-lines
error  Need empty line for more than two siblings  react-pug/empty-lines

Does not parse code in attributes

import { routes } from '~/Router'

pug`
  BrowserRouter: Component(to=Object.keys(routes)[0]) Text
`

Returns:

'routes' is defined but never used. (no-unused-vars)

An in-range update of babel7 is breaking the build 🚨

Version 7.0.1 of the babel7 packages was just published.

Branch Build failing 🚨
Monorepo release group babel7
Current Version 7.0.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ ci/circleci: Your tests failed on CircleCI (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Get prop reference in anonymous functions

This is an POC about how we can get real parameter for both anonymous and named functions.

const isParameter = variable => variable.defs.some(def => def.type === 'Parameter')
const propsRefDeclarationIndex = declaredVariables.findIndex(isParameter)

const propsRefDeclaration = declaredVariables[propsRefDeclarationIndex]

push(propsRefDeclaration.name)
goThroughReferences(context, propsRefDeclaration.references, push)

Need to apply a fix.

It stops working when we define variable within template

For example this code will break eslint:

pug`
  - const test = 'test'
`

The error message is not useful, but stacktrace reveals that babel doesn't like it and doesn't want to parse:

Unexpected token (1:1)
SyntaxError: Unexpected token (1:1)
  at Parser.raise (~/node_modules/@babel/parser/lib/index.js:4028:15)
  at Parser.unexpected (~/node_modules/@babel/parser/lib/index.js:5343:16)
  at Parser.parseExprAtom (~/node_modules/@babel/parser/lib/index.js:6432:20)
  at Parser.parseExprSubscripts (~/node_modules/@babel/parser/lib/index.js:6019:21)
  at Parser.parseMaybeUnary (~/node_modules/@babel/parser/lib/index.js:5998:21)
  at Parser.parseExprOps (~/node_modules/@babel/parser/lib/index.js:5907:21)
  at Parser.parseMaybeConditional (~/node_modules/@babel/parser/lib/index.js:5879:21)
  at Parser.parseMaybeAssign (~/node_modules/@babel/parser/lib/index.js:5826:21)
  at Parser.parseParenAndDistinguishExpression (~/node_modules/@babel/parser/lib/index.js:6588:28)
  at Parser.parseExprAtom (~/node_modules/@babel/parser/lib/index.js:6388:21)

Don't use code for literals

Sometimes I see that people use this way to render strings:

div= 'Hello World'

It will be great to have a rule that forces people to use Pug syntax instead:

div Hello World

Manage empty lines

There are some patterns which we can ask to follow:

  • Empty line in the beginning

    // Bad
    pug`div`
    
    // Good
    pug`
      div`
  • Empty line in the end

    // Bad
    pug`div`
    
    // Good
    pug`div
    `
  • No more than 1 empty line

    // Bad
    pug`
      div
    
    
      div
    `
  • Add empty line when we go out from nesting

    // Bad
    pug`
      div
        div
      div
    `
    
    // Good
    pug`
      div
        div
    
      div
    `
  • Add empty line in case we write more than two siblings

    // Bad
    pug`
      div
      div
      div
    `
    
    // Good
    pug`
      div
      div
    `
    
    pug`
      div
    
      div
    
      div
    `

Prohibit code interpolation

Everything should be handled by pug, so we can avoid using interpolation in template. This is the most misused feature.

repost : pug is undefined no-undef (already tried existing issues related to this)

Sorry that I had to post an issue which has already been posted several times here.I have seen the other issues related to this, and i tried almost all the solutions provided to those issues. This is what I have done so far :

  1. Created a new react app using "create-react-app"

  2. eject the app using "npm run eject"

  3. install https://github.com/pugjs/babel-plugin-transform-react-pug. Added a .babelrc file in root director and added the following to .babelrc file :

{
    "plugins": [
      "transform-react-pug",
      "transform-react-jsx"
    ]
}
  1. installed https://github.com/ezhlobo/eslint-plugin-react-pug from the github documentation and created a .eslintrc file and added the following to the .eslintrc file
{
    "plugins": [
        "react-pug"
    ],
    "extends": [
        "plugin:react-pug/all"
    ],
    "globals": {
        "pug": "readonly"

    }
}
  1. In App.js I added the following simple pug code
import React from 'react';
class App extends React.Component {
  render() {
    return pug`
      div
        h1 My Component
        p This is my component using pug.
    `;
  }
}
export default App;
  1. After that I've uninstalled the node-modules and re-installed it. And then run npm-start. But no matter what I am getting "pug is undefined no-undef" error message in the compiler.

What went wrong here ?

no-unused-var with function variable

This plugin is an answer to our prayers!

Having one issue with calling functions declared outside of the template returning the no-unused-var error.
For instance, the following stateless component:

`
export default function SampleFile(){
const scopeFunction= link => e => {
e.preventDefault();
window.location.href = link;
};

return pugbutton( onClick='scopeFunction('/')' ) | Scope Function;
}
Returns:
2:9 error 'scopeFunction' is assigned a value but never used no-unused-vars
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.